// #include <AVision_ESP8266.h>
// Water Softener Salt Level Monitor
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
#include <ESP8266WiFi.h>
#include "ESP8266.h"
// Network information.
#define WIFI_NAME "Getsome"
#define PASSWORD "123XXXX"
IPAddress ip(192, 168, 1, 110); // Choose your ip address
IPAddress gateway(192, 168, 1, 1); // Your gateway
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1); // Same as your gateway
// Hardware information.
#define TIMEOUT 5000 // Timeout for server response.
// ThingSpeak information.
#define THING_SPEAK_ADDRESS "api.thingspeak.com"
String writeAPIKey = "15GROHOPXYJKR0L5"; // Change this to the write API key for your channel.
WiFiClient client;
// Connect to the local Wi-Fi network
void connectWifi() {
WiFi.mode(WIFI_STA);
WiFi.persistent(false);
WiFi.setAutoConnect(false);
WiFi.setAutoReconnect(false);
WiFi.config(ip, gateway, subnet, dns);
WiFi.begin(WIFI_NAME, PASSWORD);
// Wait until the WiFi is connected
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi.");
}
// This function builds the data string for posting to ThingSpeak
int HTTPPost() {
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);
if (client.connect(THING_SPEAK_ADDRESS, 80)) {
String postData = "api_key=" + writeAPIKey;
postData += "&field1=" + String(measure.RangeMilliMeter);
postData += "&field2=" + String(WiFi.RSSI());
Serial.println("Connecting to ThingSpeak for update...");
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);
String answer = getResponse();
Serial.println(answer);
Serial.println(WiFi.RSSI());
Serial.println(measure.RangeMilliMeter);
}
else {
Serial.println("Connection Failed");
}
client.stop(); // Ensure client resources are released
return 0;
}
// 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;
}
// Put your setup code here, to run once:
void setup() {
Serial.begin(115200); // Adjust as needed to match your Serial Monitor
if (!lox.begin()) {
Serial.println("Failed to boot VL53L0X sensor!");
while (1); // Halt program execution if the sensor fails to initialize
}
connectWifi();
}
void loop() {
HTTPPost();
delay( 1000 );
Serial.println( "Goodnight for an hour ");
ESP.deepSleep( 4200000000ULL ); // unsigned 32 bit integer in microseconds
delay(100);
// If you disable sleep mode, add delay so you don't post to ThingSpeak too often.
// delay( 20000 );
}