client.ino (2699B)
1 #include "include/config.h" 2 #include "include/interface.h" 3 #include "include/led.h" 4 #include "include/macro.h" 5 6 #include <Sodaq_LSM303AGR.h> 7 #include <Sodaq_UBlox_GPS.h> 8 #include <Wire.h> 9 10 static interface client; 11 static Sodaq_LSM303AGR accel; 12 static bool next_scan, scan; 13 14 void (*reset)() = 0; 15 16 17 void setup() { 18 pinMode(ledRed, OUTPUT); 19 pinMode(ledGreen, OUTPUT); 20 pinMode(ledBlue, OUTPUT); 21 pinMode(trapPin, INPUT_PULLUP); 22 pinMode(batteryPin, INPUT); 23 pinMode(chargerPin, INPUT); 24 25 config.open(); 26 client.begin(); 27 28 if (!config.valid) 29 config = config_default; 30 31 writeLED(COLOR_RED); 32 while (!usbSerial) 33 ; 34 35 do { 36 delay(1000); 37 client.request["token"] = config.token; 38 client.request["domain"] = config.domain; 39 } while (!client.send(interface::METHOD_POST, "/api/hello")); 40 writeLED(COLOR_WHITE); 41 42 next_scan = scan = (bool) client.request["location_search"]; 43 44 bool save = false; 45 if (client.response.hasOwnProperty("token")) { 46 strcpy(config.token, (const char*) client.response["token"]), save = true; 47 client.sendToken(); 48 } 49 if (client.response.hasOwnProperty("domain")) 50 strcpy(config.domain, (const char*) client.response["domain"]), save = true; 51 52 if (save) 53 config.save(); 54 55 Wire.begin(); 56 delay(1000); 57 sodaq_gps.init(GPS_ENABLE); 58 59 accel.rebootAccelerometer(); 60 delay(1000); 61 62 // Enable the Accelerometer 63 accel.enableAccelerometer(); 64 } 65 66 void loop() { 67 static int last = 0; 68 int now = millis(); 69 70 if (now - last > statusInterval * 1000) { 71 if (scan && sodaq_gps.scan(next_scan, gpsTimeout * 1000)) { 72 client.request["latitude"] = sodaq_gps.getLat(); 73 client.request["longitude"] = sodaq_gps.getLon(); 74 client.request["accuracy"] = getAccuracy(); 75 } else { 76 client.request["latitude"] = 0; 77 client.request["longitude"] = 0; 78 client.request["accuracy"] = 0; 79 } 80 scan = next_scan; 81 82 client.request["token"] = config.token; 83 client.request["battery"] = batteryVoltage(); 84 client.request["temperature"] = accel.getTemperature(); 85 client.request["charging"] = getCharging(); 86 client.request["trap"] = getTrapStatus(); 87 client.request["satellites"] = sodaq_gps.getNumberOfSatellites(); 88 client.request["searching"] = scan; 89 90 if (client.send(interface::METHOD_POST, "/api/update")) { 91 next_scan = (bool) client.response["location_search"]; 92 } 93 last = now; 94 } 95 delay(loopDelay * 1000); 96 } 97 98 int batteryVoltage() { 99 return batteryFactor * analogRead(batteryPin); 100 } 101 102 double getAccuracy() { // -> 100% the best, 0% the worst 103 double hdop = sodaq_gps.getHDOP(); 104 if (hdop > 1) 105 hdop = 1.0 / hdop; 106 return hdop * 100; 107 } 108 109 bool getTrapStatus() { 110 return digitalRead(trapPin); 111 } 112 113 bool getCharging() { 114 return digitalRead(chargerPin); 115 }