refactor code into classes
This commit is contained in:
parent
26e8175d6b
commit
e3581f3088
4 changed files with 177 additions and 136 deletions
23
api.h
23
api.h
|
@ -2,26 +2,31 @@
|
||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
WiFiClientSecure api;
|
class OsemApi {
|
||||||
|
protected:
|
||||||
|
WiFiClientSecure client;
|
||||||
|
|
||||||
bool postMeasurement(String m, String sensorID) {
|
public:
|
||||||
|
bool postMeasurement(String m, String sensorID) {
|
||||||
//telnet.print("Connecting to API.. ");
|
//telnet.print("Connecting to API.. ");
|
||||||
if (!api.connect(API_ENDPOINT, 443)) {
|
if (!client.connect(API_ENDPOINT, 443)) {
|
||||||
//telnet.println("connection failed");
|
//telnet.println("connection failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (api.verify(API_FINGERPRINT, API_ENDPOINT)) {
|
|
||||||
//telnet.println("certificate matches");
|
if (!client.verify(API_FINGERPRINT, API_ENDPOINT)) {
|
||||||
} else {
|
Serial.println("certificate doesn't match");
|
||||||
//telnet.println("certificate doesn't match");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = "/boxes/" + String(ID_BOX) + "/" + sensorID;
|
String url = "/boxes/" + String(ID_BOX) + "/" + sensorID;
|
||||||
api.print(String("POST ") + url + " HTTP/1.1\r\n" +
|
// TODO: add actual measurement to post
|
||||||
|
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
|
||||||
"Host: " + API_ENDPOINT + "\r\n" +
|
"Host: " + API_ENDPOINT + "\r\n" +
|
||||||
"User-Agent: mobile-sensebox-esp8266\r\n" +
|
"User-Agent: mobile-sensebox-esp8266\r\n" +
|
||||||
"Connection: close\r\n\r\n");
|
"Connection: close\r\n\r\n");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
52
gps.h
52
gps.h
|
@ -3,17 +3,29 @@
|
||||||
#include <Time.h>
|
#include <Time.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
/* GPS */
|
class Gps {
|
||||||
TinyGPSPlus gps;
|
protected:
|
||||||
// allocate the maximum buffer size, so we can reduce the Serial polling interval to a minimum
|
TinyGPSPlus gps;
|
||||||
//SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN, false, 27*49);
|
|
||||||
|
|
||||||
void pollGPS() {
|
public:
|
||||||
|
void begin() {
|
||||||
|
Serial.begin(GPS_BAUD);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* parse all available bytes from the Serial input
|
||||||
|
* should be called frequently to avoid buffer overflows
|
||||||
|
*/
|
||||||
|
void pollGPS() {
|
||||||
while (Serial.available() > 0)
|
while (Serial.available() > 0)
|
||||||
gps.encode(Serial.read());
|
gps.encode(Serial.read());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool updateLocation() {
|
/**
|
||||||
|
* poll until we have a valid location.
|
||||||
|
* retries at most once, returns success state
|
||||||
|
*/
|
||||||
|
bool updateLocation() {
|
||||||
// abort if the GPS device does not push valid data within one update cycle
|
// abort if the GPS device does not push valid data within one update cycle
|
||||||
static const unsigned int timeout = 2 * GPS_INTERVAL;
|
static const unsigned int timeout = 2 * GPS_INTERVAL;
|
||||||
unsigned long start = millis();
|
unsigned long start = millis();
|
||||||
|
@ -21,33 +33,41 @@ bool updateLocation() {
|
||||||
do {
|
do {
|
||||||
pollGPS();
|
pollGPS();
|
||||||
if (millis() - start > timeout) return false;
|
if (millis() - start > timeout) return false;
|
||||||
} while (!gps.location.isUpdated());
|
} while (!gps.location.isUpdated()); // TODO: check if is valid?
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool updateTime() {
|
/**
|
||||||
|
* poll until we have a valid date & time.
|
||||||
|
* retries at most once, returns success state
|
||||||
|
*/
|
||||||
|
bool updateTime() {
|
||||||
// abort if the GPS device does not push valid data within one update cycle
|
// abort if the GPS device does not push valid data within one update cycle
|
||||||
static const unsigned int timeout = 2 * GPS_INTERVAL;
|
static const unsigned int timeout = 2 * GPS_INTERVAL;
|
||||||
unsigned long start = millis();
|
unsigned long start = millis();
|
||||||
|
|
||||||
|
// TODO: check if is valid?
|
||||||
do {
|
do {
|
||||||
pollGPS();
|
pollGPS();
|
||||||
if (millis() - start > timeout) return false;
|
if (millis() - start > timeout) return false;
|
||||||
} while ( !(gps.date.isUpdated() && gps.time.isUpdated()) );
|
} while ( !(gps.date.isUpdated() && gps.time.isUpdated()) );
|
||||||
|
|
||||||
// in case we didnt timeout, resync the local clock
|
// in case we didnt timeout, resync the local clock (Time.h)
|
||||||
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(),
|
setTime(gps.time.hour(), gps.time.minute(), gps.time.second(),
|
||||||
gps.date.day(), gps.date.month(), gps.date.year());
|
gps.date.day(), gps.date.month(), gps.date.year());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/* UTILS */
|
* return an iso8266 formatted datestring with the current time
|
||||||
static char* getISODate() {
|
*/
|
||||||
|
static char* getISODate() {
|
||||||
// TODO: check why we need to allocate that much storage for a 20 character string for Serial printing??
|
// TODO: check why we need to allocate that much storage for a 20 character string for Serial printing??
|
||||||
char result[100] = { 0 };
|
char result[100] = { 0 };
|
||||||
sprintf(result, "%04d-%02d-%02d-T%02d:%02d:%02dZ",
|
sprintf(result, "%04d-%02d-%02d-T%02d:%02d:%02dZ",
|
||||||
year(), month(), day(), hour(), minute(), second());
|
year(), month(), day(), hour(), minute(), second());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
@ -9,41 +9,57 @@
|
||||||
|
|
||||||
//TelnetPrint telnet = TelnetPrint();
|
//TelnetPrint telnet = TelnetPrint();
|
||||||
Storage storage = Storage();
|
Storage storage = Storage();
|
||||||
|
Wifi wifi = Wifi();
|
||||||
|
OsemApi api = OsemApi();
|
||||||
|
Gps gps = Gps();
|
||||||
|
|
||||||
/* UTILS */
|
/* UTILS */
|
||||||
void printState(WifiState wifi) {
|
void printState(WifiState wifiState) {
|
||||||
DEBUG_OUT.print("homeAvailable: ");
|
DEBUG_OUT.print("homeAvailable: ");
|
||||||
DEBUG_OUT.println(wifi.homeAvailable);
|
DEBUG_OUT.println(wifiState.homeAvailable);
|
||||||
DEBUG_OUT.print("numNetworks: ");
|
DEBUG_OUT.print("numNetworks: ");
|
||||||
DEBUG_OUT.println(wifi.numNetworks);
|
DEBUG_OUT.println(wifiState.numNetworks);
|
||||||
DEBUG_OUT.print("numUnencrypted: ");
|
DEBUG_OUT.print("numUnencrypted: ");
|
||||||
DEBUG_OUT.println(wifi.numUnencrypted);
|
DEBUG_OUT.println(wifiState.numUnencrypted);
|
||||||
|
|
||||||
DEBUG_OUT.print("lat: ");
|
DEBUG_OUT.print("lat: ");
|
||||||
//DEBUG_OUT.print(gps.location.lat(), 6);
|
//DEBUG_OUT.print(gps.location.lat(), 6);
|
||||||
DEBUG_OUT.print(" lng: ");
|
DEBUG_OUT.print(" lng: ");
|
||||||
//DEBUG_OUT.println(gps.location.lng(), 6);
|
//DEBUG_OUT.println(gps.location.lng(), 6);
|
||||||
|
|
||||||
DEBUG_OUT.println(getISODate());
|
DEBUG_OUT.println(gps.getISODate());
|
||||||
DEBUG_OUT.println("");
|
DEBUG_OUT.println("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool storeMeasurement(float lat, float lng, float value, char* timeStamp, char* sensorID) {
|
||||||
|
Measurement m;
|
||||||
|
m.lat = lat;
|
||||||
|
m.lng = lng;
|
||||||
|
m.value = value;
|
||||||
|
strcpy(m.timeStamp, timeStamp);
|
||||||
|
strcpy(m.sensorID, sensorID);
|
||||||
|
|
||||||
|
return storage.add(m);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* MAIN ENTRY POINTS */
|
/* MAIN ENTRY POINTS */
|
||||||
void setup() {
|
void setup() {
|
||||||
//Serial.begin(9600); // GPS reciever
|
|
||||||
DEBUG_OUT.begin(115200);
|
DEBUG_OUT.begin(115200);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
size_t bytesFree = storage.begin();
|
||||||
|
//gps.begin();
|
||||||
|
wifi.begin();
|
||||||
|
|
||||||
//connectWifi(WIFI_SSID, WIFI_PASS);
|
//connectWifi(WIFI_SSID, WIFI_PASS);
|
||||||
|
|
||||||
//delay(5000); // DEBUG: oportunity to connect to network logger
|
//delay(5000); // DEBUG oportunity to connect to network logger
|
||||||
|
|
||||||
// wait until we got a first fix from GPS, and thus an initial time
|
// wait until we got a first fix from GPS, and thus an initial time
|
||||||
/*DEBUG_OUT.print("Getting GPS fix..");
|
/*DEBUG_OUT.print("Getting GPS fix..");
|
||||||
while (!updateLocation()) { DEBUG_OUT.print("."); }
|
while (!gps.updateLocation()) { DEBUG_OUT.print("."); }
|
||||||
DEBUG_OUT.print(" done! ");*/
|
DEBUG_OUT.print(" done! ");*/
|
||||||
DEBUG_OUT.println(getISODate());
|
DEBUG_OUT.println(gps.getISODate());
|
||||||
|
|
||||||
DEBUG_OUT.println("Setup done!\n");
|
DEBUG_OUT.println("Setup done!\n");
|
||||||
DEBUG_OUT.println("WiFi MAC WiFi IP");
|
DEBUG_OUT.println("WiFi MAC WiFi IP");
|
||||||
|
@ -52,7 +68,7 @@ void setup() {
|
||||||
DEBUG_OUT.println(WiFi.localIP());
|
DEBUG_OUT.println(WiFi.localIP());
|
||||||
|
|
||||||
DEBUG_OUT.print("SPIFF bytes free: ");
|
DEBUG_OUT.print("SPIFF bytes free: ");
|
||||||
DEBUG_OUT.println(storage.begin());
|
DEBUG_OUT.println(bytesFree);
|
||||||
|
|
||||||
digitalWrite(D9, HIGH); // DEBUG: integrated led? doesnt work
|
digitalWrite(D9, HIGH); // DEBUG: integrated led? doesnt work
|
||||||
}
|
}
|
||||||
|
@ -60,42 +76,35 @@ void setup() {
|
||||||
void loop() {
|
void loop() {
|
||||||
//pollGPS();
|
//pollGPS();
|
||||||
//DEBUG_OUT.pollClients();
|
//DEBUG_OUT.pollClients();
|
||||||
WifiState wifi = scanWifi(WIFI_SSID);
|
WifiState wifiState = wifi.scanWifi(WIFI_SSID);
|
||||||
|
char* dateString = gps.getISODate();
|
||||||
|
|
||||||
// TODO: take other measurements (average them?)
|
// TODO: take other measurements (average them?)
|
||||||
/*
|
/*
|
||||||
if(!updateLocation()) DEBUG_OUT.println("GPS timed out (location)");
|
if(!gps.updateLocation()) DEBUG_OUT.println("GPS timed out (location)");
|
||||||
if(!updateTime()) DEBUG_OUT.println("GPS timed out (time)");
|
if(!gps.updateTime()) DEBUG_OUT.println("GPS timed out (time)");
|
||||||
*/
|
*/
|
||||||
// TODO: write measurements to file
|
|
||||||
|
|
||||||
// TODO: connect to wifi, if available & not connected yet
|
// write measurements to file
|
||||||
// then upload local data, clean up
|
if (storeMeasurement(51.2, 7.89, wifiState.numNetworks, dateString, "12341234123412341234123412341234")) {
|
||||||
|
|
||||||
printState(wifi);
|
|
||||||
|
|
||||||
// recall all previous measurements
|
|
||||||
while (storage.size()) {
|
|
||||||
String measure = storage.pop();
|
|
||||||
DEBUG_OUT.println("popped a measurement: ");
|
|
||||||
DEBUG_OUT.println(measure.substring(0, 31)); // size of sensorID
|
|
||||||
DEBUG_OUT.println(measure.substring(32)); // skip the newline char
|
|
||||||
}
|
|
||||||
|
|
||||||
// store new measurement
|
|
||||||
Measurement testMeasure;
|
|
||||||
testMeasure.lat = 51.2;
|
|
||||||
testMeasure.lng = 7.89;
|
|
||||||
testMeasure.value = wifi.numNetworks;
|
|
||||||
strcpy(testMeasure.timeStamp, getISODate());
|
|
||||||
strcpy(testMeasure.sensorID, "123457812345678123456781234567");
|
|
||||||
|
|
||||||
if (storage.add(testMeasure)) {
|
|
||||||
DEBUG_OUT.print("measurement stored! storage size: ");
|
DEBUG_OUT.print("measurement stored! storage size: ");
|
||||||
} else {
|
} else {
|
||||||
DEBUG_OUT.print("measurement store failed! storage size: ");
|
DEBUG_OUT.print("measurement store failed! storage size: ");
|
||||||
}
|
}
|
||||||
DEBUG_OUT.println(storage.size());
|
DEBUG_OUT.println(storage.size());
|
||||||
|
|
||||||
delay(2000);
|
// TODO: connect to wifi, if available & not connected yet
|
||||||
|
// then upload local data, remove from storage
|
||||||
|
|
||||||
|
// DEBUG: recall all previous measurements
|
||||||
|
while (storage.size()) {
|
||||||
|
String measure = storage.pop();
|
||||||
|
//api.postMeasurement(measure.substring(0, 31), measure.substring(32));
|
||||||
|
DEBUG_OUT.println("popped a measurement: ");
|
||||||
|
DEBUG_OUT.println(measure.substring(0, 31)); // size of sensorID
|
||||||
|
DEBUG_OUT.println(measure.substring(32)); // skip the newline char
|
||||||
|
}
|
||||||
|
|
||||||
|
printState(wifiState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
wifi.h
19
wifi.h
|
@ -2,15 +2,20 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
/* WIFI */
|
|
||||||
struct WifiState {
|
struct WifiState {
|
||||||
bool homeAvailable;
|
bool homeAvailable;
|
||||||
unsigned int numNetworks;
|
unsigned int numNetworks;
|
||||||
unsigned int numUnencrypted;
|
unsigned int numUnencrypted;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: filter duplicate SSIDs!
|
class Wifi {
|
||||||
WifiState scanWifi(String homeSSID) {
|
public:
|
||||||
|
void begin() {
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
}
|
||||||
|
|
||||||
|
WifiState scanWifi(String homeSSID) {
|
||||||
|
// TODO: filter duplicate SSIDs!
|
||||||
WifiState state;
|
WifiState state;
|
||||||
state.homeAvailable = false;
|
state.homeAvailable = false;
|
||||||
state.numNetworks = WiFi.scanNetworks(false, false);
|
state.numNetworks = WiFi.scanNetworks(false, false);
|
||||||
|
@ -25,9 +30,9 @@ WifiState scanWifi(String homeSSID) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool connectWifi(const char* ssid, const char* pass) {
|
bool connectWifi(const char* ssid, const char* pass) {
|
||||||
static const unsigned int timeout = 10000; // abort after 10 secs
|
static const unsigned int timeout = 10000; // abort after 10 secs
|
||||||
unsigned long start = millis();
|
unsigned long start = millis();
|
||||||
|
|
||||||
|
@ -44,4 +49,6 @@ bool connectWifi(const char* ssid, const char* pass) {
|
||||||
}
|
}
|
||||||
//telnet.println(" timeout");
|
//telnet.println(" timeout");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue