// Salt depth with 4 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 #define SLEEP_TIME 4200000000ULL // Sleep about 70 mins #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); #include #define WIFI_NAME "********" #define PASSWORD "********" #define TIMEOUT 5000 // Timeout for server response. #define THING_SPEAK_ADDRESS "api.thingspeak.com" String writeAPIKey="****************"; // Change this to the write API key for your channel. WiFiClient client; void setup() { Serial.begin( 74880 ); 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); lox.begin(); connectWifi(); } void loop() { HTTPPost(); delay( 1000 ); ESP.deepSleep(SLEEP_TIME, WAKE_RF_DISABLED); delay(100); } int connectWifi() // Connect to WiFi { WiFi.mode(WIFI_STA); WiFi.persistent(false); // Turn off confusing helful features WiFi.setAutoConnect(false); WiFi.setAutoReconnect(false); 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 VL53L0X_RangingMeasurementData_t measure; lox.rangingTest(&measure, false); if (client.connect( THING_SPEAK_ADDRESS , 80 )){ // Build the postData string. String postData= "api_key=" + writeAPIKey ; postData += "&field1="; postData += String(measure.RangeMilliMeter); postData += "&field2="; postData += String(WiFi.RSSI()); // 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(WiFi.RSSI()); Serial.println(measure.RangeMilliMeter); } 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; }