Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino Water Level sensor with battery check
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define trigPin 14 // d5
#define echoPin 12 // d6
#define SERVER_IP "10.0.0.55"
const int analogInPin = A0;
float vOUT = 0.0;
float vIN = 0.0;
float R2 = 7500.0;
float R1 = 30000.0;
// WiFi credentials.
const char* WIFI_SSID = "wifi-name";
const char* WIFI_PASS = "wifi-password";
WiFiClientSecure wifiClient;
void connect() {
// Connect to Wifi.
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// WiFi fix: https://github.com/esp8266/Arduino/issues/2186
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long wifiConnectStart = millis();
while (WiFi.status() != WL_CONNECTED) {
// Check to see if
if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.println("Failed to connect to WiFi. Please verify credentials: ");
delay(10000);
}
delay(500);
Serial.println("...");
// Only try for 5 seconds.
if (millis() - wifiConnectStart > 15000) {
Serial.println("Failed to connect to WiFi");
return;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
}
void sendData(int distance, float voltage) {
Serial.print("Sending data to collector...");
HTTPClient http;
http.begin("http://" SERVER_IP "/home/store");
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
DynamicJsonDocument root(1024);
root["distance"] = distance;
root["voltage"] = voltage;
String json;
serializeJson(root, json);
Serial.println(json);
int httpCode = http.POST(json);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_CREATED) {
Serial.println("Return code is ok");
// Read response
Serial.print(http.getString());
} else {
Serial.printf("Return code %d\n", httpCode);
if (httpCode == 400) {
Serial.println("Validation error: The device ID, access key, or access secret is not in the proper format.");
} else {
Serial.println("Unknown response from API");
}
return;
}
} else {
return;
}
http.end();
}
// liczymy napiecie z wartosci
float calcVoltage(int value) {
float vOUT;
float vIN;
vOUT = (value * 3.3) / 1024.0;
vIN = vOUT / (R2 / (R1 + R2));
return vIN;
}
// liczy odleglosc
int sonar() {
long duration;
int distance;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
return distance;
}
void setup(void) {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
digitalWrite(LED_BUILTIN, 0);
Serial.begin(115200);
//Serial.setTimeout(2000);
Serial.println("");
// while(!Serial) { }
connect();
int distance;
int value;
float voltage;
distance = sonar();
value = analogRead(analogInPin);
voltage = calcVoltage(value);
sendData(distance, voltage, value);
Serial.println("sleep");
ESP.deepSleep(30e6);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.