Skip to content

Instantly share code, notes, and snippets.

@pawl pawl/README.md
Last active Jul 11, 2019

Embed
What would you like to do?
esp32 higrow telegram bot
#define SECRET_SSID ""
#define SECRET_PASS ""
#define SECRET_BOT_TOKEN ""
#define SECRET_CHAT_ID ""
#include "arduino_secrets.h"
#include <WiFi.h>
#include <DHT.h>
#include <ESP.h>
#include <esp_deep_sleep.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
#define uS_TO_S_FACTOR 1000000LL
unsigned long now;
int DEEPSLEEP_SECONDS = 86400;
uint64_t chipid;
long timeout;
const int dhtpin = 22;
const int soilpin = 32;
const int POWER_PIN = 34;
const int LIGHT_PIN = 33;
// Initialize DHT sensor.
DHT dht(dhtpin, DHTTYPE);
// Temporary variables
static char fahrenheitTemp[7];
static char humidityTemp[7];
// Client variables
char linebuf[80];
int charcount=0;
const char* ssid = SECRET_SSID;
const char* wifiPassword = SECRET_PASS;
const char* chatID = SECRET_CHAT_ID;
const char BotToken[] = SECRET_BOT_TOKEN;
// Initialize Telegram BOT
WiFiClientSecure client;
UniversalTelegramBot bot(BotToken, client);
char deviceid[21];
void setup() {
dht.begin();
Serial.begin(115200);
while(!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
esp_deep_sleep_enable_timer_wakeup(1800 * uS_TO_S_FACTOR);
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
pinMode(16, OUTPUT);
pinMode(POWER_PIN, INPUT);
digitalWrite(16, LOW);
timeout = 0;
chipid = ESP.getEfuseMac();
sprintf(deviceid, "%" PRIu64, chipid);
Serial.print("DeviceId: ");
Serial.println(deviceid);
// attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
WiFi.begin(ssid, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
digitalWrite(16, LOW); //switched on
sendSensorData();
esp_sleep_enable_timer_wakeup(DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void sendSensorData(){
char message[1024];
//This section read sensors
timeout = millis();
int waterlevel = analogRead(soilpin);
int lightlevel = analogRead(LIGHT_PIN);
waterlevel = map(waterlevel, 0, 4095, 0, 1023);
waterlevel = constrain(waterlevel, 0, 1023);
lightlevel = map(lightlevel, 0, 4095, 0, 1023);
lightlevel = constrain(lightlevel, 0, 1023);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Fahrenheit
float temperature = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(temperature, humidity);
dtostrf(hif, 6, 2, fahrenheitTemp);
dtostrf(humidity, 6, 2, humidityTemp);
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.print(F("°F Heat index: "));
Serial.print(hif);
Serial.println(F("°F"));
String did = String(deviceid);
String water = String((int)waterlevel);
String light = String((int)lightlevel);
strcpy(message, "deviceId: ");
strcat(message, did.c_str());
strcat(message, "\nwater: ");
strcat(message, water.c_str());
strcat(message, "\nlight: ");
strcat(message, light.c_str());
strcat(message, "\nhumidity: ");
strcat(message, humidityTemp);
strcat(message, "\ntemperature: ");
strcat(message, fahrenheitTemp);
bot.sendMessage(chatID, message, "");
//if(lightlevel<100){
// DEEPSLEEP_SECONDS = 10800;
//}
}
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.