Created
May 31, 2019 01:34
-
-
Save iberotecno/237f49285efcf86bf6a73144b332d710 to your computer and use it in GitHub Desktop.
Iberotecno. Water sensor with telegram warning system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************* | |
Iberotecno | |
Water sensor with telegram warning system | |
*******************************************************************/ | |
#include <WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <UniversalTelegramBot.h> | |
#define CHAT_ID "782590438" | |
//OLED | |
#include "Arduino.h" | |
#include <Wire.h> | |
#include "SSD1306.h" | |
#define Vext 21 | |
#define SDA 4 //OLED SDA pin | |
#define SCL 15 //OLED SCL pin | |
#define RST 16 //OLED nRST pin | |
#define Fbattery 3700 //The default battery is 3700mv when the battery is fully charged. | |
float XS = 0.00225; //The returned reading is multiplied by this XS to get the battery voltage. | |
uint16_t MUL = 1000; | |
uint16_t MMUL = 100; | |
SSD1306 display(0x3c, SDA, SCL, RST); | |
// Initialize Wifi connection to the router | |
char ssid[] = "Viurex"; // your network SSID (name) | |
char password[] = "Anemona12@."; // your network key | |
// Initialize Telegram BOT | |
#define BOTtoken "640232033:AAEqe4BlVkjMivcuxJ16rdfA6g8EMujJiLc" // your Bot Token (Get from Botfather) | |
WiFiClientSecure client; | |
UniversalTelegramBot bot(BOTtoken, client); | |
int Bot_mtbs = 1000; //mean time between scan messages | |
long Bot_lasttime; //last time messages' scan has been done | |
const int pinBuzzer = 27; | |
const int waterSensor = 33; | |
int valor = 0; | |
int acu = 0; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("inicio"); | |
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); | |
pinMode(Vext, OUTPUT); | |
digitalWrite(Vext, LOW); | |
delay(1000); | |
display.init(); | |
display.flipScreenVertically(); | |
display.setFont(ArialMT_Plain_10); | |
display.drawString(0, 10, "Iniciando dispositivo..."); | |
display.display(); | |
delay(1000); | |
display.clear(); | |
adcAttachPin(13); | |
analogSetClockDiv(255); // 1338mS | |
} | |
void loop() { | |
int valor = analogRead(waterSensor); | |
if (valor >= 5) { | |
// Attempt to connect to Wifi network: | |
Serial.print("Connecting Wifi: "); | |
Serial.println(ssid); | |
display.drawString(0, 10, "Conectando a Wifi..."); | |
display.drawString(0, 20, ssid); | |
display.display(); | |
// Set WiFi to station mode and disconnect from an AP if it was Previously | |
// connected | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
acu++; | |
display.drawString(acu, 30, ". "); | |
display.display(); | |
if (acu == 10) { | |
display.clear(); | |
display.drawString(0, 10, "Iniciando hibernacion"); | |
display.display(); | |
delay(1000); | |
Serial.println("Going to sleep now"); | |
esp_deep_sleep_start(); | |
Serial.println("This will never be printed"); | |
acu = 0; | |
} | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
String mi_ip = WiFi.localIP().toString(); | |
display.clear(); | |
display.drawString(0, 10, "CONECTADO!"); | |
display.drawString(0, 20, "Direccion IP:"); | |
display.drawString(0, 30, mi_ip); | |
display.display(); | |
delay(1000); | |
String message = "Agua detectada"; | |
if (bot.sendMessage(CHAT_ID, message, "Markdown")) { | |
Serial.println("TELEGRAM Successfully sent"); | |
display.clear(); | |
display.drawString(0, 10, "Mensaje enviado!"); | |
display.display(); | |
delay(1000); | |
for ( int i = 15; i != 0; i--) { | |
String myString = String(i); | |
display.clear(); | |
display.drawString(0, 20, "Esperando "); //PONER UN FOR Y CUENTA ATRAS | |
display.drawString(55, 20, myString); //PONER UN FOR Y CUENTA ATRAS | |
display.drawString(70, 20, " segundos"); //PONER UN FOR Y CUENTA ATRAS | |
display.drawString(0, 30, "Antes de nuevo chequeo"); | |
display.display(); | |
delay(1000); | |
} | |
display.clear(); | |
display.drawString(0, 10, "Iniciando hibernacion"); | |
display.display(); | |
delay(1000); | |
Serial.println("Going to sleep now"); //REVISAR QUE ENVIE REPTITIVAS VECES | |
esp_deep_sleep_start(); | |
Serial.println("This will never be printed"); | |
} | |
else { | |
Serial.println("Fallo TELEGRAM"); | |
} | |
} | |
else { | |
display.clear(); | |
display.drawString(0, 10, "Iniciando hibernacion"); | |
display.display(); | |
delay(1000); | |
Serial.println("Going to sleep now"); | |
esp_deep_sleep_start(); | |
Serial.println("This will never be printed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment