diff --git a/de/esp8266/modify_sketch.md b/de/esp8266/modify_sketch.md index ead2f3f..1c0dffd 100644 --- a/de/esp8266/modify_sketch.md +++ b/de/esp8266/modify_sketch.md @@ -9,31 +9,29 @@ Wichtig ist dass man seine senseBox-ID und Sensor-IDs kennt. > *Diese kann [hier](https://github.com/finitespace/BME280/archive/Release_Version_1.0.02.zip) heruntergeladen werden.* > *Die Installation erfolgt analog zu unseren Bibliotheken, wie [hier](../software_installation.md#arduino-bibliotheken-installieren) beschrieben ist.* -Hier findet ihr einen komplett angepassten Sketch. +Hier findet ihr einen komplett angepassten Sketch, um Messwerte des BME280 Sensors mit Hilfe eines ESP8266 auf die ([openSenseMap](https://opensensemap.org)) hochzuladen. Dort müssen nur noch WiFi-Name und -Passwort, senseBox-ID und Sensor-IDs ersetzt werden. ### Kompletter Sketch ```arduino /* senseBox Home - Citizen Sensingplatform - Version: 2.4 - Date: 2017-Mar-06 + Version: 2.5 + Date: 2019-Apr-04 Homepage: https://www.sensebox.de https://www.opensensemap.org Author: Institute for Geoinformatics, University of Muenster - Note: Sketch for senseBox:home + Note: Sketch for ESP8266 Email: support@sensebox.de */ #include -#include "HDC100X.h" #include -#include #include #include // Wifi Configuration -const char* server = "ingress.opensensemap.org"; WiFiClient client; +const char* server = "ingress.opensensemap.org"; const char* ssid = "dein-wifi-name"; const char* password = "dein-wifi-passwort"; @@ -44,7 +42,7 @@ typedef struct sensor { uint8_t sensorsIndex = 0; // Number of sensors -static const uint8_t NUM_SENSORS = 5; +static const uint8_t NUM_SENSORS = 3; // senseBox ID and sensor IDs const uint8_t SENSEBOX_ID[12] = { 0x58, 0xd9, 0x1a, 0x6f, 0x68, 0x91, 0xfd, 0x00, 0x0f, 0x8a, 0xf7, 0xed }; @@ -57,65 +55,52 @@ const sensor sensors[NUM_SENSORS] = { { 0x58, 0xd9, 0x1a, 0x6f, 0x68, 0x91, 0xfd, 0x00, 0x0f, 0x8a, 0xf7, 0xef }, // Luftdruck { 0x58, 0xd9, 0x1a, 0x6f, 0x68, 0x91, 0xfd, 0x00, 0x0f, 0x8a, 0xf7, 0xf0 }, - // Beleuchtungsstärke - { 0x58, 0xd9, 0x1a, 0x6f, 0x68, 0x91, 0xfd, 0x00, 0x0f, 0x8a, 0xf7, 0xf1 }, - // UV-Intensität - { 0x58, 0xd9, 0x1a, 0x6f, 0x68, 0x91, 0xfd, 0x00, 0x0f, 0x8a, 0xf7, 0xf2 } }; uint8_t contentLength = 0; float values[NUM_SENSORS]; -// Load sensors -Makerblog_TSL45315 TSL = Makerblog_TSL45315(TSL45315_TIME_M4); -HDC100X HDC(0x43); -BME280 BME; +// Load sensor +BME280 bme; uint8_t pressureUnit(1); // hPa -#define UV_ADDR 0x38 -#define IT_1 0x1 const unsigned int postingInterval = 60000; void setup() { sleep(2000); Serial.begin(9600); - // start the Wifi connection: - Serial.println("SenseBox Home software version 2.1"); - Serial.println(); + Serial.println("SenseBox Home software version 2.5"); + // Wifi connection: Serial.print("Starting wifi connection..."); WiFi.begin(ssid, password); - while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("done!"); - sleep(1000); + delay(500); + + // sensors Serial.print("Initializing sensors..."); - Wire.begin(); - Wire.beginTransmission(UV_ADDR); - Wire.write((IT_1 << 2) | 0x02); - Wire.endTransmission(); + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + sleep(1000); + } sleep(500); - HDC.begin(HDC100X_TEMP_HUMI, HDC100X_14BIT, HDC100X_14BIT, DISABLE); - TSL.begin(); - BME.begin(); Serial.println("done!"); - HDC.getTemp(); Serial.println("Starting loop.\n"); } void loop() { - addValue(HDC.getTemp()); - sleep(200); - addValue(HDC.getHumi()); - addValue(BME.ReadPressure(pressureUnit)); - addValue(TSL.readLux()); - addValue(getUV()); + float temp(NAN), hum(NAN), pres(NAN); + bme.ReadData(pres, temp, hum, true, pressureUnit); + addValue(temp); + addValue(hum); + addValue(pres); submitValues(); - - sleep(postingInterval); + delay(postingInterval); } void addValue(const float& value) { @@ -123,24 +108,7 @@ void addValue(const float& value) { sensorsIndex = sensorsIndex + 1; } -uint16_t getUV() { - byte msb = 0, lsb = 0; - uint16_t uvValue; - Wire.requestFrom(UV_ADDR + 1, 1); // MSB - sleep(1); - if (Wire.available()) - msb = Wire.read(); - Wire.requestFrom(UV_ADDR + 0, 1); // LSB - sleep(1); - if (Wire.available()) - lsb = Wire.read(); - uvValue = (msb << 8) | lsb; - return uvValue * 5; -} - -int printHexToStream(const uint8_t* data, - uint8_t length, - Print& stream) // prints 8-bit data in hex +int printHexToStream(const uint8_t* data, uint8_t length, Print& stream) // prints 8-bit data in hex { byte first; int j = 0; @@ -164,7 +132,8 @@ int printHexToStream(const uint8_t* data, return j; } -int printCsvToStream(Print& stream) { +int printCsvToStream(Print& stream) +{ int len = 0; for (uint8_t i = 0; i < sensorsIndex; i++) { if (!isnan(values[i])) { @@ -180,8 +149,9 @@ int printCsvToStream(Print& stream) { return len; } -// millis() rollover fix - -// http://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover +// millis() rollover fix - http://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover +// Be careful with the use of sleep function! +// In conjunction with long times, the esp watchdog will step in and perform a soft WDT reset. void sleep(unsigned long ms) { // ms: duration unsigned long start = millis(); // start: timestamp for (;;) { @@ -194,7 +164,7 @@ void sleep(unsigned long ms) { // ms: duration void waitForResponse() { // if there are incoming bytes from the server, read and print them - sleep(100); + delay(100); String response = ""; char c; boolean repeat = true; @@ -204,15 +174,17 @@ void waitForResponse() { else repeat = false; response += c; + if (response == "HTTP/1.1 ") response = ""; if (c == '\n') repeat = false; + yield(); } while (repeat); Serial.print("Server Response: "); - Serial.print(response); - + Serial.println(response); + client.flush(); client.stop(); } @@ -221,9 +193,10 @@ void submitValues() { // close any connection before send a new request. // This will free the socket on the WiFi shield Serial.println("__________________________\n"); - if (client.connected()) { + if (client.connected()) + { client.stop(); - sleep(1000); + delay(500); } // if there's a successful connection: if (client.connect(server, 80)) { @@ -238,14 +211,13 @@ void submitValues() { // !!!!! NICHT LÖSCHEN !!!!! // print once to Serial to get the content-length int contentLen = printCsvToStream(Serial); - // !!!!! DO NOT REMOVE !!!!! // !!!!! NICHT LÖSCHEN !!!!! + // !!!!! DO NOT REMOVE !!!!! // Send the required header parameters client.print(F("Host: ")); client.println(server); - client.print( - F("Content-Type: text/csv\nConnection: close\nContent-Length: ")); + client.print(F("Content-Type: text/csv\nConnection: close\nContent-Length: ")); client.println(contentLen); client.println(); printCsvToStream(client); @@ -253,13 +225,11 @@ void submitValues() { Serial.println("done!"); waitForResponse(); - - // reset index sensorsIndex = 0; } else { // if you couldn't make a connection: Serial.println("connection failed. Restarting System."); - sleep(5000); + delay(1000); ESP.restart(); } }