Skip to content

Instantly share code, notes, and snippets.

@moose4621 moose4621/Tank_level_sensor.ino Secret
Last active Jun 26, 2018

Embed
What would you like to do?
//************WIFI stuff*********
#include <ESP8266WiFi.h>
// replace with your channel’s thingspeak API key,
String apiKey = "B9OGI0E3H5I03CPK";
const char* ssid = "My_SSID";
const char* password = "My_Password";
const char* server = "api.thingspeak.com";
WiFiClient client;
//****************Ping sensor stuff******************
#include <NewPing.h>
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 0 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int waterAtEmpty = 162; //distance in cm of sensor to water level at empty.
int waterAtFull = 57; //distance in cm from sensor to water level at full.
int dist;
const int T1 = 4;//ping sensor power
const int T2 = 14;//voltage divider
//**********Power save***********
const int sleepTimeS = 3600;
void setup() {
Serial.begin(115200);
delay(10);
pinMode(T1, OUTPUT);
pinMode(T2, OUTPUT);
digitalWrite(T2, LOW);
pinMode(D0, WAKEUP_PULLUP);
/* WiFi.mode(WIFI_STA);
// WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
*/
// Connect to Wifi.
WiFi.begin(ssid, password);
// WiFi fix: https://github.com/esp8266/Arduino/issues/2186
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
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");
Serial.println("ESP8266 in sleep mode");
ESP.deepSleep(sleepTimeS * 1000000);
return;
}
}
/*WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
*/
//*******read water level*****
digitalWrite(T1, HIGH);//turn on ping sensor via 8050 transistor
delay(1000);//warm up ping sensor
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
delay(100);
uS = sonar.ping();
delay(100);
uS = sonar.ping();
delay(100);
digitalWrite(T1, LOW);//turn off ping sensor, save power!
dist = (uS / US_ROUNDTRIP_CM); //raw distance in cm.
int waterRange = waterAtEmpty - waterAtFull; //difference between full and empty in cm.
int adjDist = dist - waterAtFull; //water level from full in cm.
float percent = map(adjDist, waterRange, 0, 0, 100); //Map waterRange to percent.
//*******read batt voltage*******
// read the input on analog pin 0:
//digitalWrite(T2, HIGH);
//delay(100);
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.2V):
float voltage = ((analogRead(A0)) / 200.78);
//digitalWrite(T2,LOW);
//*******send to thingspeak*****
if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr += "&field3=";
postStr += String(percent);
postStr += "&field4=";
postStr += String(voltage);
postStr += "\r\n\r\n";
//postStr += "\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Tank level ");
Serial.print(percent);
Serial.print(" sensorValue ");
Serial.print(sensorValue);
Serial.print(" Batt voltage ");
Serial.print(voltage);
Serial.println(" send to Thingspeak");
}
client.stop();
delay(1000);
// Sleep
Serial.println("ESP8266 in sleep mode");
ESP.deepSleep(sleepTimeS * 1000000);
}
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.