Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created March 25, 2024 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/94de9dfde2c326783ffa6070a49e2be9 to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/94de9dfde2c326783ffa6070a49e2be9 to your computer and use it in GitHub Desktop.
water_level.ino
#include <Wire.h>
#include <VL53L0X.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* serverAddress = "http://yourserver.com/store_data.php";
VL53L0X sensor;
WiFiClient client;
void setup() {
Serial.begin(115200);
// Initialize the sensor
Wire.begin();
sensor.setTimeout(500);
if (!sensor.init()) {
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
sensor.startContinuous();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read water level from sensor
uint16_t waterLevel = sensor.readRangeContinuousMillimeters();
// Send data to server
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverAddress);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String postData = "water_level=" + String(waterLevel);
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.println("Data sent successfully");
} else {
Serial.println("Error sending data");
}
http.end();
} else {
Serial.println("WiFi Disconnected. Unable to send data.");
}
// Wait for some time before next reading
delay(5000); // Adjust delay as per requirement
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment