#include #include #include #include #include #include #include //#include "ntpServer.h" #include // for ntp time #include // for ntp time const char *ssid = "WiFi ssid"; const char *password = "WiFi password"; // local #define emoncms_host "xxx.xxx.xxx.xxx"; const int emoncms_httpPort = 80; String emoncms_apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; String node = "STEM_1_IndoorEnviroment"; // create a timer with default settings auto timer = timer_create_default(); Adafruit_BME280 bme; // Prototypes void SetupOTA(); void setup(); void postEmoncms(); bool wiggleHeartBeat(void *); bool OTA_Routine(void *); bool ServiceIndoor(void *); bool endOfDayEvent(void *); WiFiClient client; long rssi = 0; #define HBEAT 16 int upDays = 0; float h, tc, tf, p; float dailyTempAcc = 0.0, dailyCounter = 0; unsigned long msgnum = 0; WiFiUDP ntpUDP; //-sdr- const long utcOffsetInSeconds = -18000; // -sdr- For UTC -5.00 : -5 * 60 * 60 : -18000 NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds); //-sdr- int hours; //-sdr- int midnightFlag = 0; //-sdr- String url = ""; void loop() { timer.tick(); // tick the timer ArduinoOTA.handle(); } void setup() { Serial.begin(115200); delay(10); Serial.println("Start up - Emon serial input to emoncms via bridge"); // set up timer tasks timer.every(987L, wiggleHeartBeat); timer.every(15L, OTA_Routine); // timer.every(60000L, ServiceIndoor); timer.every(10000L, ServiceIndoor); timer.every(1140000L, endOfDayEvent); // timer.every(60000L, endOfDayEvent); // BME280 SENSOR if (!bme.begin()) { while (1) { digitalWrite(HBEAT, HIGH); delay(200); digitalWrite(HBEAT, LOW); delay(200); } } // setup WiFi WiFi.begin(ssid, password); Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to IP Address: "); Serial.println(WiFi.localIP()); SetupOTA(); pinMode(HBEAT, OUTPUT); // for heart beat msgnum = 0; h = tc = tf = p = 0.0; dailyTempAcc = 0.0; dailyCounter = 0; midnightFlag = 0; // send out date and time timeClient.update(); Serial.print("Present time: "); Serial.println(timeClient.getFormattedTime()); delay(5000); url = "GET /emoncms/input/post.json?apikey=" + emoncms_apikey + "&node=" + node + "&json={Temperature:" + "0" + ",Humidity:" + "0" + ",Pressure:" + "0" + ",YesAveTemp:" + "0" + ",DaysRunning:" + "0" + ",MsgCounter:" + "0" + ",RSSI:" + "0"; postEmoncms(); //while (1) //{ // digitalWrite(HBEAT, HIGH); // delay(200); // digitalWrite(HBEAT, LOW); // delay(200); //} delay(5000); } void postEmoncms() { // run only if the ESP is connected to a network if (WiFi.status() == WL_CONNECTED) { if (!client.connect(emoncms_host, emoncms_httpPort)) { Serial.println("Post to emoncms failed"); return; } url = url + ",MsgCounter:" + msgnum + ""; msgnum++; Serial.println(url); // let's have a look at what we are sending out client.println(url); client.print("Host: "); client.println(emoncms_host); client.println("Connection: close"); client.println(); client.stop(); // Stopping client Serial.println("Post to emoncms : success"); } else { Serial.println("Post to emoncms : not connected to internet"); } } void SetupOTA() { ArduinoOTA.onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else // U_SPIFFS type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() Serial.println("Start updating " + type); }); ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); // ArduinoOTA.setHostname(TPC_ID); ArduinoOTA.begin(); } // Set up OTA bool wiggleHeartBeat(void *) { //digitalWrite(HBEAT, HIGH); // digitalWrite(HBEAT, LOW); digitalWrite(HBEAT, !bool(digitalRead(HBEAT))); return true; // repeat? true } bool endOfDayEvent(void *) { float dailyAveTemp = 0.0; int hours; timeClient.update(); hours = timeClient.getHours(); // hours = timeClient.getSeconds(); Serial.println(hours); // test for midnight if ((hours == 00) && (midnightFlag == 0)) { midnightFlag = 1; upDays++; dailyAveTemp = dailyTempAcc / dailyCounter; Serial.print(dailyAveTemp); Serial.print("\t"); Serial.print(dailyTempAcc); Serial.print("\t"); Serial.println(dailyCounter); // send to EmonCMS url = "GET /emoncms/input/post.json?apikey=" + emoncms_apikey + "&node=" + node + "&json={DaysRunning:" + upDays + ",YesAveTemp:" + dailyAveTemp + ",DailyTempAcc:" + dailyTempAcc + ",DailyCounter:" + dailyCounter; postEmoncms(); dailyTempAcc = 0.0; dailyCounter = 0; } // test for 5 am if ((hours == 05) && (midnightFlag == 1)) { midnightFlag = 0; } return true; } bool OTA_Routine(void *) { ArduinoOTA.handle(); // OTA return true; // repeat? true } bool ServiceIndoor(void *) { h = bme.readHumidity(); tc = bme.readTemperature(); tf = tc * 1.8 + 32.0; p = bme.readPressure() / 1000.0F; rssi = WiFi.RSSI(); // collect for daily average temperature dailyTempAcc = dailyTempAcc + tc; dailyCounter++; // send to EmonCMS url = "GET /emoncms/input/post.json?apikey=" + emoncms_apikey + "&node=" + node + "&json={Temperature:" + tc + ",Humidity:" + h + ",Pressure:" + p + ",RSSI:" + rssi + ""; postEmoncms(); return true; }