// Oil Tank monitor with 7 hours between measurements extern "C" { #include "user_interface.h" // this is for the RTC memory read/write functions } uint32_t counter = 0; uint32_t sleeps = 6; // Number of sleeps between measurements, 6 is ok #define SLEEP_TIME 4200000000ULL // Sleep about 70 mins //#define SLEEP_TIME 10000000ULL // Sleep 10 secs for testing #include #include VL53L1X sensor; #include #define WIFI_NAME "SSID" #define PASSWORD "PASSWORD" #define TIMEOUT 5000 // Timeout for server response. #define THING_SPEAK_ADDRESS "api.thingspeak.com" String writeAPIKey="API KEY"; // Change this to the write API key for your channel. WiFiClient client; void setup() { Serial.begin( 74880 ); // This speed displays bootup code system_rtc_mem_read(65, &counter, 4); // read RTC memory Serial.println(counter); if (counter >= sleeps) counter=0; // reset counter when reached maximum sleeps counter++; system_rtc_mem_write(65, &counter, 4); // increment and save RTC memory if (counter == (sleeps)) ESP.deepSleep(SLEEP_TIME); if (counter != 1) ESP.deepSleep(SLEEP_TIME, WAKE_RF_DISABLED); pinMode(15,OUTPUT); digitalWrite(15,HIGH); // Switch on sensor Wire.begin(); Wire.setClock(400000); // use 400 kHz I2C sensor.setTimeout(500); if (!sensor.init()) { Serial.println("Failed to detect and initialize sensor!"); while (1); } sensor.setDistanceMode(VL53L1X::Long); sensor.setMeasurementTimingBudget(200000); // Budget in us sensor.startContinuous(10000); // Measurement rate ms connectWifi(); } void loop() { HTTPPost(); delay( 1000 ); ESP.deepSleep(SLEEP_TIME, WAKE_RF_DISABLED); delay(100); } int connectWifi() // Connect to WiFi { // WiFi.mode(WIFI_STA); while (WiFi.status() != WL_CONNECTED) { WiFi.begin( WIFI_NAME , PASSWORD ); Serial.println( "Connecting to Wi-Fi" ); delay( 5000 ); } Serial.println( "Connected" ); // Inform the serial monitor. } int HTTPPost(){ // Measure and post data to ThingSpeak if (client.connect( THING_SPEAK_ADDRESS , 80 )){ // Build the postData string. int tank = sensor.read(); int strength = WiFi.RSSI(); digitalWrite(15,LOW); // Turn off VL53L1X String postData= "api_key=" + writeAPIKey ; postData += "&field1="; postData += String(tank); postData += "&field2="; postData += String(strength); // POST data via HTTP. Serial.println( "Connecting to ThingSpeak for update..." ); Serial.println(); client.println( "POST /update HTTP/1.1" ); client.println( "Host: api.thingspeak.com" ); client.println( "Connection: close" ); client.println( "Content-Type: application/x-www-form-urlencoded" ); client.println( "Content-Length: " + String( postData.length() ) ); client.println(); client.println( postData ); Serial.println( postData ); String answer=getResponse(); Serial.println( answer ); Serial.println(tank); Serial.println(strength); // Serial.println(WiFi.RSSI()); // Serial.println(tank); } else { Serial.println ( "Connection Failed" ); } } // Collect the response and build it into a string. String getResponse(){ String response; long startTime = millis(); delay( 200 ); while ( client.available() < 1 && (( millis() - startTime ) < TIMEOUT ) ){ delay( 5 ); } if( client.available() > 0 ){ // Get response from server. char charIn; do { charIn = client.read(); // Read a char from the buffer. response += charIn; // Append the char to the string response. } while ( client.available() > 0 ); } client.stop(); return response; }