/* ESP8266 Environmental si7021s Node copyright: Jannik Beyerstedt | http://jannikbeyerstedt.de | code@jannikbeyerstedt.de license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License */ #include #include #include #include "Adafruit_Si7021.h" extern "C" { #include "user_interface.h" } #define DEBUG /* enable Serial debugging */ #define TEST /* use test config and database */ #define HUZZAH 1 /* Adafruit Feather HUZZAH */ #define LOLIN 2 /* LoLin NodeMCU */ #define ESP07S 3 /* standalone ESP07S */ #define BOARD 1 /* choose from above */ #define NODENAME "env-huzzah-01" /* IMPORTANT: CHANGE FOR EACH DEVICE */ //#define NODENAME "env-esp07-01" /* IMPORTANT: CHANGE FOR EACH DEVICE */ /* SETTINGS */ #ifdef TEST String loggerName = "esp-test"; unsigned int sendInterval_sec = 180; /* 180 sec (3 min) */ #else String loggerName = NODENAME; unsigned int sendInterval_sec = 1800; /* 1800 sec (30 min) */ #endif //IPAddress ip(192, 168, 50, 5); /* IMPORTANT: CHANGE FOR EACH DEVICE */ IPAddress gate(192, 168, 50, 1); IPAddress subn(255, 255, 255, 0); float tempOffset = -1.0; float humiOffset = -10.0; const char* host = "ursaminor.fra80"; #ifdef TEST String serviceUri = "/write?db=test"; #else String serviceUri = "/write?db=sensors"; #endif const int httpPort = 8086; #if BOARD == LOLIN #define BATT_FULL 2812 #define BATT_CUTOFF 2280 #elif BOARD == HUZZAH #define BATT_FULL 3100 #define BATT_CUTOFF 2450 /* 2500=3,0V; 3080=4,0V (Battery Voltage!!!) */ #elif BOARD == ESP07S #define BATT_FULL 3250 #define BATT_CUTOFF 2450 /* 2450=2,7V; 3200=3,5V (VCC Voltage) */ #endif /* GLOBAL VARIABLES */ const unsigned int wifiConnectTimeout = 30; /* num of 500ms intervals */ Adafruit_Si7021 si7021 = Adafruit_Si7021(); float temp; float humi; ADC_MODE(ADC_VCC); // set the ADC to read VCC #ifdef DEBUG #define PRINT(x) Serial.print(x) #define PRINTLN(x) Serial.println(x) #else #define PRINT(x) #define PRINTLN(x) #endif bool wifiConnect(void) { unsigned int cycles = 0; PRINTLN("INFO Connecting to WiFi"); WiFi.mode(WIFI_STA); WiFi.begin("WLAN-Bey-IoT", "IoT-Fra80"); //WiFi.config(ip, gate, subn); // use a static ip for faster connect while (WiFi.status() != WL_CONNECTED) { delay(500); PRINT("."); // log level: DEBUG cycles++; if (cycles > wifiConnectTimeout) { PRINTLN(" FAILED"); return false; } } PRINT(" ok, IP: "); // log level: DEBUG PRINTLN(WiFi.localIP()); return true; } void setup() { #ifdef DEBUG Serial.begin(115200); delay(100); Serial.println(""); Serial.print("Node "); Serial.print(loggerName); Serial.print(", BoardType: "); Serial.println(BOARD); #endif WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); si7021.begin(); pinMode(0, OUTPUT); /* GET VALUE SAMPLES */ humi = si7021.readHumidity() + humiOffset; temp = si7021.readTemperature() + tempOffset; #ifdef DEBUG Serial.print("> Humidity: "); Serial.print(humi, 2); Serial.print("\tTemperature: "); Serial.println(temp, 2); #endif /* WRITE VALUES TO DATABASE */ // turn wifi on again PRINTLN("DEBUG turning wifi on again"); WiFi.forceSleepWake(); if (!wifiConnect()) { return; } uint16_t battLevel = ESP.getVcc(); float battPerc = 100.0 * (battLevel - BATT_CUTOFF) / (BATT_FULL - BATT_CUTOFF); Serial.print("Bat: "); Serial.print(battLevel); Serial.print(", "); Serial.print(battPerc); Serial.println("%"); // send data to influxdb String influxDataLine = "weather,sensor=" + loggerName + " temp=" + String(temp, 2); influxDataLine += ",hum=" + String(humi, 2); influxDataLine += ",batRaw=" + String(battLevel); influxDataLine += ",bat=" + String(battPerc); PRINT("INFO connecting to "); PRINT(host); WiFiClient client; if (client.connect(host, httpPort)) { PRINTLN(" > success"); // log level: INFO String httpRequest = "POST " + serviceUri + " HTTP/1.1\r\n" + "Host: " + host + ":" + httpPort + "\r\n" + "Content-Length: " + influxDataLine.length() + "\r\n" + "Authorization: Basic c2Vuc29yczpTZW5zb3JzLXcuaW5mbHV4QGhvbWU=\r\n" + "Connection: close\r\n" + "\r\n" + influxDataLine + "\r\n"; client.print(httpRequest); delay(100); while (client.available()) { String line = client.readStringUntil('\n'); if (line.indexOf("HTTP") != -1) { if (line.indexOf("204") == -1) { // expect a HTTP 204 status code PRINTLN("ERROR: invalid http status code"); PRINTLN(line); } break; } else { ; } } } else { PRINTLN(" > FAILED"); } client.stop(); /* SAVE EVEN MORE ENERGY, WAIT IN DEEP SLEEP */ PRINTLN("DEBUG going to deep sleep now"); ESP.deepSleep(sendInterval_sec * 1000000, WAKE_RF_DEFAULT); delay(100); } void loop() { /* WIFI CONNECT FAILED, WAIT 5 MINUTES IN DEEP SLEEP */ PRINTLN("DEBUG going to deep sleep now"); if (sendInterval_sec > (5 * 60)) { ESP.deepSleep(5 * 60 * 1000000, WAKE_RF_DEFAULT); } else { ESP.deepSleep(sendInterval_sec * 1000000, WAKE_RF_DEFAULT); } delay(100); }