|
|
|
@ -21,24 +21,80 @@ bool storeMeasurement(float lat, float lng, float value, const char* timeStamp,
|
|
|
|
|
m.value = value;
|
|
|
|
|
strcpy(m.timeStamp, timeStamp);
|
|
|
|
|
strcpy(m.sensorID, sensorID);
|
|
|
|
|
|
|
|
|
|
return storage.add(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool uploadMeasurements(const uint16_t maxUploads = 5) {
|
|
|
|
|
uint16_t uploadCount = 0;
|
|
|
|
|
String measure;
|
|
|
|
|
// only upload limited measures per cycle, to avoid long gaps in measurements
|
|
|
|
|
while (storage.size() && uploadCount++ < maxUploads) {
|
|
|
|
|
measure = storage.pop();
|
|
|
|
|
DEBUG_OUT << "Uploading measurement for " << measure;
|
|
|
|
|
if(!api.postMeasurement(measure.substring(API_KEY_LENGTH + 1), measure.substring(0, API_KEY_LENGTH))) {
|
|
|
|
|
DEBUG_OUT << "upload failed!" << EOL;
|
|
|
|
|
return false;
|
|
|
|
|
void measure(WifiState& wifiState) {
|
|
|
|
|
char dateString[20];
|
|
|
|
|
|
|
|
|
|
// measure WiFi
|
|
|
|
|
wifiState = wifi.scan(WIFI_SSID);
|
|
|
|
|
|
|
|
|
|
// update gps position
|
|
|
|
|
// TODO: how to handle lost GPS fix?
|
|
|
|
|
// TODO: check if is data is valid?
|
|
|
|
|
if(!gps.updateLocation() || !gps.updateTime()) {
|
|
|
|
|
DEBUG_OUT << "GPS timed out" << EOL;
|
|
|
|
|
digitalWrite(BUILTIN_LED, LOW);
|
|
|
|
|
} else {
|
|
|
|
|
digitalWrite(BUILTIN_LED, HIGH);
|
|
|
|
|
}
|
|
|
|
|
TinyGPSLocation loc = gps.getLocation();
|
|
|
|
|
gps.getISODate(dateString);
|
|
|
|
|
|
|
|
|
|
// print state
|
|
|
|
|
DEBUG_OUT << "homeAvailable: " << wifiState.homeAvailable << EOL;
|
|
|
|
|
DEBUG_OUT << "numAPs: " << wifiState.numAccessPoints << EOL;
|
|
|
|
|
DEBUG_OUT << "numNetworks: " << wifiState.numNetworks << EOL;
|
|
|
|
|
DEBUG_OUT << "numUnencrypted: " << wifiState.numUnencrypted << EOL;
|
|
|
|
|
DEBUG_OUT.print("lat: ");
|
|
|
|
|
DEBUG_OUT.print(loc.lat(), 6);
|
|
|
|
|
DEBUG_OUT.print(" lng: ");
|
|
|
|
|
DEBUG_OUT.println(loc.lng(), 6);
|
|
|
|
|
DEBUG_OUT << dateString << EOL;
|
|
|
|
|
|
|
|
|
|
// IDEA: write location update to separate file?
|
|
|
|
|
|
|
|
|
|
// write measurements to file
|
|
|
|
|
if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numAccessPoints, dateString, ID_SENSOR_WIFI_APS))
|
|
|
|
|
DEBUG_OUT << "measurement (wifi aps) store failed!" << EOL;
|
|
|
|
|
|
|
|
|
|
if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numNetworks, dateString, ID_SENSOR_WIFI_NET))
|
|
|
|
|
DEBUG_OUT << "measurement (wifi nets) store failed!" << EOL;
|
|
|
|
|
|
|
|
|
|
if (!storeMeasurement(loc.lat(), loc.lng(), wifiState.numUnencrypted, dateString, ID_SENSOR_WIFI_OPEN))
|
|
|
|
|
DEBUG_OUT << "measurement (wifi open) store failed!" << EOL;
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT << EOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void upload(WifiState& wifiState) {
|
|
|
|
|
size_t numMeasures = storage.size();
|
|
|
|
|
DEBUG_OUT << numMeasures << " measurements stored." << EOL;
|
|
|
|
|
if (!numMeasures) return;
|
|
|
|
|
|
|
|
|
|
// in case we are not measuring, scan manually to detect the home network
|
|
|
|
|
if (digitalRead(PIN_MEASURE_MODE) == LOW)
|
|
|
|
|
wifiState = wifi.scan(WIFI_SSID);
|
|
|
|
|
|
|
|
|
|
// connect to wifi, if available & not connected yet
|
|
|
|
|
// once we are connected, upload (max) 5 stored measurements & free the storage
|
|
|
|
|
if (wifi.isConnected() || (wifiState.homeAvailable && wifi.connect(WIFI_SSID, WIFI_PASS)) ) {
|
|
|
|
|
uint16_t uploadCount = 0;
|
|
|
|
|
String measure;
|
|
|
|
|
// only upload limited measures per cycle, to avoid long gaps in measurements
|
|
|
|
|
while (storage.size() && uploadCount++ < MAX_UPLOADS_PER_CYCLE) {
|
|
|
|
|
measure = storage.pop();
|
|
|
|
|
DEBUG_OUT << "Uploading measurement for " << measure;
|
|
|
|
|
if (api.postMeasurement(measure.substring(API_KEY_LENGTH + 1), measure.substring(0, API_KEY_LENGTH)))
|
|
|
|
|
DEBUG_OUT << "success!" << EOL;
|
|
|
|
|
else
|
|
|
|
|
DEBUG_OUT << "upload failed!" << EOL;
|
|
|
|
|
}
|
|
|
|
|
DEBUG_OUT << "success!" << EOL;
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_OUT << "wifi connection to " << WIFI_SSID << " failed" << EOL;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
DEBUG_OUT << EOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delay for a given duration (ms), rollover-safe implementation
|
|
|
|
@ -54,13 +110,18 @@ void adaptiveDelay(unsigned long ms, unsigned long offset = 0) {
|
|
|
|
|
|
|
|
|
|
unsigned long now = millis();
|
|
|
|
|
unsigned long elapsed = now - start + offset;
|
|
|
|
|
//DEBUG_OUT << elapsed << EOL;
|
|
|
|
|
if (elapsed >= ms) return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MAIN ENTRY POINTS */
|
|
|
|
|
void setup() {
|
|
|
|
|
pinMode(BUILTIN_LED, OUTPUT); // status indicator LED: on = no GPS fix
|
|
|
|
|
digitalWrite(BUILTIN_LED, LOW);
|
|
|
|
|
pinMode(PIN_MEASURE_MODE, INPUT_PULLUP); // switch for measurements (pull it down to disable)
|
|
|
|
|
pinMode(PIN_UPLOAD_MODE, INPUT_PULLUP); // switch for API uploads (pull it down to disable)
|
|
|
|
|
|
|
|
|
|
size_t bytesFree = storage.begin();
|
|
|
|
|
gps.begin();
|
|
|
|
|
wifi.begin();
|
|
|
|
@ -68,89 +129,41 @@ void setup() {
|
|
|
|
|
// DEBUG: just for connection to telnet printer
|
|
|
|
|
wifi.connect(WIFI_SSID, WIFI_PASS);
|
|
|
|
|
DEBUG_OUT.begin();
|
|
|
|
|
delay(5000);
|
|
|
|
|
delay(3000);
|
|
|
|
|
telnet.pollClients();
|
|
|
|
|
|
|
|
|
|
// wait until we got a first fix from GPS, and thus an initial time
|
|
|
|
|
DEBUG_OUT.print("Getting GPS fix..");
|
|
|
|
|
while (!gps.updateLocation()) { DEBUG_OUT.print("."); }
|
|
|
|
|
DEBUG_OUT.println(" done! ");
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT.println("WiFi MAC WiFi IP");
|
|
|
|
|
DEBUG_OUT.print(WiFi.macAddress());
|
|
|
|
|
DEBUG_OUT.print(" ");
|
|
|
|
|
DEBUG_OUT.println(WiFi.localIP());
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT.print("SPIFF bytes free: ");
|
|
|
|
|
DEBUG_OUT.println(bytesFree);
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT.println("Setup done!\n");
|
|
|
|
|
DEBUG_OUT << "Getting GPS fix..";
|
|
|
|
|
while (!gps.updateLocation()) { DEBUG_OUT << "."; }
|
|
|
|
|
DEBUG_OUT << " done!" << EOL;
|
|
|
|
|
digitalWrite(BUILTIN_LED, HIGH);
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT << "Setup done!" << EOL;
|
|
|
|
|
DEBUG_OUT << "WiFi MAC WiFi IP" << EOL;
|
|
|
|
|
DEBUG_OUT << WiFi.macAddress() << " " << WiFi.localIP() << EOL;
|
|
|
|
|
DEBUG_OUT << "SPIFF bytes free: " << bytesFree << EOL << EOL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long cycleStart;
|
|
|
|
|
TinyGPSLocation loc;
|
|
|
|
|
char dateString[20];
|
|
|
|
|
WifiState wifiState; // global, as both measure and upload need the state
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
cycleStart = millis();
|
|
|
|
|
|
|
|
|
|
//gps.pollGPS();
|
|
|
|
|
//telnet.pollClients();
|
|
|
|
|
|
|
|
|
|
// measure WiFi
|
|
|
|
|
WifiState wifiState = wifi.scan(WIFI_SSID);
|
|
|
|
|
|
|
|
|
|
// measure GPS
|
|
|
|
|
if(!gps.updateLocation()) DEBUG_OUT.println("GPS timed out (location)");
|
|
|
|
|
if(!gps.updateTime()) DEBUG_OUT.println("GPS timed out (time)");
|
|
|
|
|
loc = gps.getLocation();
|
|
|
|
|
gps.getISODate(dateString);
|
|
|
|
|
|
|
|
|
|
// print state
|
|
|
|
|
DEBUG_OUT << "homeAvailable: " << wifiState.homeAvailable << EOL;
|
|
|
|
|
DEBUG_OUT << "numAPs: " << wifiState.numAccessPoints << EOL;
|
|
|
|
|
DEBUG_OUT << "numNetworks: " << wifiState.numNetworks << EOL;
|
|
|
|
|
DEBUG_OUT << "numUnencrypted: " << wifiState.numUnencrypted << EOL;
|
|
|
|
|
DEBUG_OUT.print("lat: ");
|
|
|
|
|
DEBUG_OUT.print(loc.lat(), 6);
|
|
|
|
|
DEBUG_OUT.print(" lng: ");
|
|
|
|
|
DEBUG_OUT.println(loc.lng(), 6);
|
|
|
|
|
DEBUG_OUT << dateString << EOL;
|
|
|
|
|
|
|
|
|
|
// TODO. write location update to file
|
|
|
|
|
if (digitalRead(PIN_MEASURE_MODE) == HIGH)
|
|
|
|
|
measure(wifiState);
|
|
|
|
|
|
|
|
|
|
// write measurements to file
|
|
|
|
|
if (storeMeasurement(loc.lat(), loc.lng(), wifiState.numAccessPoints, dateString, ID_SENSOR_WIFI_APS)) {
|
|
|
|
|
DEBUG_OUT.print("measurement (wifi aps) stored! storage size: ");
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_OUT.print("measurement (wifi aps) store failed! storage size: ");
|
|
|
|
|
}
|
|
|
|
|
DEBUG_OUT.println(storage.size());
|
|
|
|
|
if (storeMeasurement(loc.lat(), loc.lng(), wifiState.numNetworks, dateString, ID_SENSOR_WIFI_NET)) {
|
|
|
|
|
DEBUG_OUT.print("measurement (wifi nets) stored! storage size: ");
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_OUT.print("measurement (wifi nets) store failed! storage size: ");
|
|
|
|
|
}
|
|
|
|
|
DEBUG_OUT.println(storage.size());
|
|
|
|
|
if (storeMeasurement(loc.lat(), loc.lng(), wifiState.numUnencrypted, dateString, ID_SENSOR_WIFI_OPEN)) {
|
|
|
|
|
DEBUG_OUT.print("measurement (wifi open) stored! storage size: ");
|
|
|
|
|
} else {
|
|
|
|
|
DEBUG_OUT.print("measurement (wifi open) store failed! storage size: ");
|
|
|
|
|
}
|
|
|
|
|
DEBUG_OUT.println(storage.size());
|
|
|
|
|
if (digitalRead(PIN_UPLOAD_MODE) == HIGH)
|
|
|
|
|
upload(wifiState);
|
|
|
|
|
|
|
|
|
|
// connect to wifi, if available & not connected yet
|
|
|
|
|
// once we are connected, upload (max) 4 stored measurements & free the storage
|
|
|
|
|
if (wifi.isConnected() || (wifiState.homeAvailable && wifi.connect(WIFI_SSID, WIFI_PASS)) ) {
|
|
|
|
|
uploadMeasurements(4);
|
|
|
|
|
if (digitalRead(PIN_MEASURE_MODE) == HIGH) {
|
|
|
|
|
// run the measurements in a fixed interval, using an adaptive delay
|
|
|
|
|
// IDEA: dont use time but distance interval? -> TinyGPSPlus::distanceBetween()
|
|
|
|
|
adaptiveDelay(MEASUREMENT_INTERVAL, millis() - cycleStart);
|
|
|
|
|
} else {
|
|
|
|
|
// run as fast as possible when not measuring.
|
|
|
|
|
// adaptiveDelay has to be called anyway, as some polling functions are run within
|
|
|
|
|
adaptiveDelay(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEBUG_OUT << EOL;
|
|
|
|
|
|
|
|
|
|
// the measurement & upload cycle takes ~4 seconds, so lets we wait measure
|
|
|
|
|
// every 10secs in total
|
|
|
|
|
// run the loop every 10secs. using an adaptive delay
|
|
|
|
|
// IDEA: dont use time but distance interval? -> TinyGPSPlus::distanceBetween()
|
|
|
|
|
adaptiveDelay(MEASUREMENT_INTERVAL, millis() - cycleStart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|