Imagine | Develop | Create
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino //needed for library #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager #include <ESP8266HTTPClient.h> #define NumberOfItems 600 #define timeBetweenLog 600 //10 mins or 600 secs #define avg false //avg on and sleeping off String date = ""; unsigned long startingTime; int count = -2; struct log {unsigned long time;int value;} logItems[NumberOfItems]; unsigned long AvgCount = 0; unsigned long Avgsum = 0; String MonitorId="defult"; void setup() { Serial.begin(115200); //ADC_MODE(ADC_VCC); Serial.println("started"); WiFiManager wifiManager; WiFiManagerParameter custom_MonitorId("MonitorNameId", "Monitors Name ID", "defult", 40); wifiManager.addParameter(&custom_MonitorId); wifiManager.autoConnect("Voltage Logger"); MonitorId = String(custom_MonitorId.getValue()); Serial.println("connected...yeey :)"); date = getDate(); startingTime = millis()/1000; } void loop() { getdata(); if(count>-1) postData(); //else if(!avg && timeBetweenLog + startingTime - millis() > 10000) //ESP.deepSleep((timeBetweenLog + startingTime - millis()-10000) * 1000); } void getdata() { if(avg) { AvgCount++; Avgsum -= Avgsum / AvgCount; Avgsum += analogRead(A0) / AvgCount; } if(count == -2 || timeBetweenLog + startingTime < millis()/1000) { count++; if(count >= NumberOfItems) { for(int i = 1; i < NumberOfItems; i++) { logItems[i-1].time = logItems[i].time; logItems[i-1].value = logItems[i].value; } count--; } if(count<0)count = 0; logItems[count].time = millis()/1000; if(avg) { logItems[count].value = AvgCount; } else { logItems[count].value = analogRead(A0); } startingTime = millis()/1000; } } void postData() { String theDate=""; String time = String(logItems[count].time); String value = String(logItems[count].value); HTTPClient http; http.begin("batterymonitor.php?monitorid=" + MonitorId + "&date=" + date + "&time=" + time + "&value=" + value); int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET... code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { count--; } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } String getDate() { String theDate=""; HTTPClient http; http.begin("batterymonitor.php?gettime=1"); //HTTP int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET... code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { theDate = http.getString(); Serial.println(theDate); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); if(theDate != "") return theDate; delay(60000); return getDate(); }