Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- // Initialize Wifi connection to the router
- char ssid[] = "hooshmandsazan3"; // your network SSID (name)
- char password[] = "!q@w#e4r5t6y"; // your network key
- // Initialize Telegram BOT
- #define BOTtoken "339475786:AAH7IjXu51xPom_sMx8IRnvQbcTD5FfO2Wc" // 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
- bool Start = false;
- const int ledPin = 2;
- int ledStatus = 0;
- void handleNewMessages(int numNewMessages) {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
- for (int i=0; i<numNewMessages; i++) {
- String chat_id = String(bot.messages[i].chat_id);
- String text = bot.messages[i].text;
- String from_name = bot.messages[i].from_name;
- if (from_name == "") from_name = "Guest";
- if (text == "/ledon") {
- digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
- ledStatus = 1;
- bot.sendMessage(chat_id, "Led is ON", "");
- }
- if (text == "/ledoff") {
- ledStatus = 0;
- digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
- bot.sendMessage(chat_id, "Led is OFF", "");
- }
- if (text == "/status") {
- if(ledStatus){
- bot.sendMessage(chat_id, "Led is ON", "");
- } else {
- bot.sendMessage(chat_id, "Led is OFF", "");
- }
- }
- if (text == "/start") {
- String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
- welcome += "This is Flash Led Bot example.\n\n";
- welcome += "/ledon : to switch the Led ON\n";
- welcome += "/ledoff : to switch the Led OFF\n";
- welcome += "/status : Returns current status of LED\n";
- bot.sendMessage(chat_id, welcome, "Markdown");
- }
- }
- }
- // Use your own API key by signing up for a free developer account.
- // http://www.wunderground.com/weather/api/
- #define WU_API_KEY "c0c5192e71ab0291"
- // Specify your favorite location one of these ways.
- #define WU_LOCATION "IR/esfahan"
- // US ZIP code
- //#define WU_LOCATION ""
- //#define WU_LOCATION "90210"
- // Country and city
- //#define WU_LOCATION "Australia/Sydney"
- // 5 minutes between update checks. The free developer account has a limit
- // on the number of calls so don't go wild.
- #define DELAY_NORMAL (5*60*1000)
- // 20 minute delay between updates after an error
- #define DELAY_ERROR (20*60*1000)
- #define WUNDERGROUND "api.wunderground.com"
- // HTTP request
- const char WUNDERGROUND_REQ[] =
- "GET /api/" WU_API_KEY "/forecast/q/" WU_LOCATION ".json HTTP/1.1\r\n"
- "User-Agent: ESP8266/0.1\r\n"
- "Accept: */*\r\n"
- "Host: " WUNDERGROUND "\r\n"
- "Connection: close\r\n"
- "\r\n";
- String webpage, city, country, date_time, observationtime, wx_forecast,
- DWDay0, DMon0, DDateDa0, DDateMo0, DDateYr0, Dicon0, DHtemp0, DLtemp0, DHumi0, DPop0, DRain0, DW_spd0, DW_dir0, DW_dir_deg0, Condi0,
- DWDay1, DMon1, DDateDa1, DDateMo1, DDateYr1, Dicon1, DHtemp1, DLtemp1, DHumi1, DPop1, DRain1, DW_spd1, DW_dir1, DW_dir_deg1, Condi1,
- DWDay2, DMon2, DDateDa2, DDateMo2, DDateYr2, Dicon2, DHtemp2, DLtemp2, DHumi2, DPop2, DRain2, DW_spd2, DW_dir2, DW_dir_deg2, Condi2,
- DWDay3, DMon3, DDateDa3, DDateMo3, DDateYr3, Dicon3, DHtemp3, DLtemp3, DHumi3, DPop3, DRain3, DW_spd3, DW_dir3, DW_dir_deg3, Condi3;
- // Astronomy
- //String DphaseofMoon, Sunrise, Sunset, Moonrise, Moonset, Moon_image, Moon_light;
- bool Centred = true, notCentred = false;
- bool Metric = true; // Set to false for Imperial units
- bool Hybrid = true; // Set to false to show 'kph' wind speed instead of 'mph' and rain in 'mm' instead of 'in'
- void setup()
- {
- Serial.begin(115200);
- // Set WiFi to station mode and disconnect from an AP if it was Previously
- // connected
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- delay(100);
- // attempt to connect to Wifi network:
- Serial.print("Connecting Wifi: ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
- delay(10);
- digitalWrite(ledPin, LOW); // initialize pin as off
- }
- static char respBuf[10000];
- void loop()
- {
- // TODO check for disconnect from AP
- // Open socket to WU server port 80
- Serial.print(F("Connecting to "));
- Serial.println(WUNDERGROUND);
- // Use WiFiClient class to create TCP connections
- WiFiClient httpclient;
- const int httpPort = 80;
- if (!httpclient.connect(WUNDERGROUND, httpPort)) {
- Serial.println(F("connection failed"));
- delay(DELAY_ERROR);
- return;
- }
- // This will send the http request to the server
- Serial.print(WUNDERGROUND_REQ);
- httpclient.print(WUNDERGROUND_REQ);
- httpclient.flush();
- // Collect http response headers and content from Weather Underground
- // HTTP headers are discarded.
- // The content is formatted in JSON and is left in respBuf.
- int respLen = 0;
- bool skip_headers = true;
- while (httpclient.connected() || httpclient.available()) {
- if (skip_headers) {
- String aLine = httpclient.readStringUntil('\n');
- //Serial.println(aLine);
- // Blank line denotes end of headers
- if (aLine.length() <= 1) {
- skip_headers = false;
- }
- }
- else {
- int bytesIn;
- bytesIn = httpclient.read((uint8_t *)&respBuf[respLen], sizeof(respBuf) - respLen);
- //Serial.print(F("bytesIn ")); Serial.println(bytesIn);
- if (bytesIn > 0) {
- respLen += bytesIn;
- if (respLen > sizeof(respBuf)) respLen = sizeof(respBuf);
- }
- else if (bytesIn < 0) {
- Serial.print(F("read error "));
- Serial.println(bytesIn);
- }
- }
- delay(1);
- }
- httpclient.stop();
- if (respLen >= sizeof(respBuf)) {
- Serial.print(F("respBuf overflow "));
- Serial.println(respLen);
- delay(DELAY_ERROR);
- return;
- }
- // Terminate the C string
- respBuf[respLen++] = '\0';
- Serial.print(F("respLen "));
- Serial.println(respLen);
- //Serial.println(respBuf);
- if (showWeather_forecast(respBuf)) {
- delay(10000);
- Serial.println(F("ok"));
- }
- else {
- delay(10000);
- Serial.println(F("oooo"));
- }
- if (millis() > Bot_lasttime + Bot_mtbs) {
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- while(numNewMessages) {
- Serial.println("got response");
- handleNewMessages(numNewMessages);
- numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- }
- Bot_lasttime = millis();
- }
- }
- bool showWeather_forecast(char *json) {
- //StaticJsonBuffer<8*1024> jsonBuffer;
- // Serial.println("JSON data missing");
- char *jsonstart = strchr(json, '{');
- //Serial.print(F("jsonstart ")); Serial.println(jsonstart);
- if (jsonstart == NULL) {
- Serial.println(F("JSON data missing"));
- return false;
- }
- json = jsonstart;
- DynamicJsonBuffer jsonBuffer1(10000);
- // Parse JSON
- JsonObject& root = jsonBuffer1.parseObject(json);
- if (!root.success()) {
- Serial.println(F("jsonBuffer.parseObject() failed"));
- return false;
- }
- // Extract weather info from parsed JSON
- JsonObject& wxforecast = root["forecast"]["txt_forecast"];
- if (Metric) {String fcttext0 = wxforecast["forecastday"][0]["fcttext_metric"]; wx_forecast = fcttext0;}
- else {String fcttext0 = wxforecast["forecastday"][0]["fcttext"]; wx_forecast = fcttext0;}
- JsonObject& forecast = root["forecast"]["simpleforecast"];
- //# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- String Temp_mon = forecast["forecastday"][0]["date"]["monthname"];
- String Mon0 = forecast["forecastday"][0]["date"]["monthname_short"]; DMon0 = Mon0;
- int DateYr0 = forecast["forecastday"][0]["date"]["year"]; DDateYr0 = String(DateYr0).substring(2);
- int DateDa0 = forecast["forecastday"][0]["date"]["day"]; DDateDa0 = DateDa0<10?"0"+String(DateDa0):String(DateDa0);
- String WDay0 = forecast["forecastday"][0]["date"]["weekday"]; DWDay0 = WDay0;
- String TempHr = forecast["forecastday"][0]["date"]["hour"]; observationtime = TempHr+":";
- String TempMin = forecast["forecastday"][0]["date"]["min"]; observationtime += TempMin+"Hr on ";
- observationtime += DDateDa0+" "+DMon0+" "+DDateYr0;
- if (Metric) {
- int Htemp0 = forecast["forecastday"][0]["high"]["celsius"]; DHtemp0 = String(Htemp0);
- int Ltemp0 = forecast["forecastday"][0]["low"]["celsius"]; DLtemp0 = String(Ltemp0);
- int rain0 = forecast["forecastday"][0]["qpf_allday"]["mm"]; DRain0 = String(rain0);
- int w_kph0 = forecast["forecastday"][0]["avewind"]["kph"]; DW_spd0 = String(w_kph0);
- }
- else
- {
- int Htemp0 = forecast["forecastday"][0]["high"]["fahrenheit"]; DHtemp0 = String(Htemp0);
- int Ltemp0 = forecast["forecastday"][0]["low"]["fahrenheit"]; DLtemp0 = String(Ltemp0);
- int rain0 = forecast["forecastday"][0]["qpf_allday"]["in"]; DRain0 = String(rain0);
- int w_mph0 = forecast["forecastday"][0]["avewind"]["mph"]; DW_spd0 = String(w_mph0);
- }
- String icon_url0 = forecast["forecastday"][0]["icon"]; Dicon0 = icon_url0;
- String pop0 = forecast["forecastday"][0]["pop"]; DPop0 = String(pop0);
- String cond0 = forecast["forecastday"][0]["conditions"]; Condi0 = cond0;
- if (Hybrid && Metric)
- {
- int w_mph0 = forecast["forecastday"][0]["avewind"]["mph"]; DW_spd0 = String(w_mph0);
- }
- String w_dir0 = forecast["forecastday"][0]["avewind"]["dir"]; DW_dir0 = String(w_dir0);
- String w_dir_deg0 = forecast["forecastday"][0]["avewind"]["degrees"]; DW_dir_deg0 = String(w_dir_deg0);
- int humi0 = forecast["forecastday"][0]["avehumidity"]; DHumi0 = String(humi0);
- //# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- String WDay1 = forecast["forecastday"][1]["date"]["weekday"]; DWDay1 = WDay1;
- String Mon1 = forecast["forecastday"][1]["date"]["monthname_short"]; DMon1 = Mon1;
- int DateDa1 = forecast["forecastday"][1]["date"]["day"]; DDateDa1 = DateDa1<10?"0"+String(DateDa1):String(DateDa1);
- int DateYr1 = forecast["forecastday"][1]["date"]["year"]; DDateYr1 = String(DateYr1).substring(2);
- if (Metric) {
- int Htemp1 = forecast["forecastday"][1]["high"]["celsius"]; DHtemp1 = String(Htemp1);
- int Ltemp1 = forecast["forecastday"][1]["low"]["celsius"]; DLtemp1 = String(Ltemp1);
- int rain1 = forecast["forecastday"][1]["qpf_allday"]["mm"]; DRain1 = String(rain1);
- int w_kph1 = forecast["forecastday"][1]["avewind"]["kph"]; DW_spd1 = String(w_kph1);
- }
- else
- {
- int Htemp1 = forecast["forecastday"][1]["high"]["fahrenheit"]; DHtemp1 = String(Htemp1);
- int Ltemp1 = forecast["forecastday"][1]["low"]["fahrenheit"]; DLtemp1 = String(Ltemp1);
- int rain1 = forecast["forecastday"][1]["qpf_allday"]["in"]; DRain1 = String(rain1);
- int w_mph1 = forecast["forecastday"][1]["avewind"]["mph"]; DW_spd1 = String(w_mph1);
- }
- String icon_url1 = forecast["forecastday"][1]["icon"]; Dicon1 = icon_url1;
- String pop1 = forecast["forecastday"][1]["pop"]; DPop1 = String(pop1);
- String cond1 = forecast["forecastday"][1]["conditions"]; Condi1 = cond1;
- if (Hybrid && Metric)
- {
- int w_mph1 = forecast["forecastday"][1]["avewind"]["mph"]; DW_spd1 = String(w_mph1);
- }
- String w_dir1 = forecast["forecastday"][1]["avewind"]["dir"]; DW_dir1 = String(w_dir1);
- String w_dir_deg1 = forecast["forecastday"][1]["avewind"]["degrees"]; DW_dir_deg1 = String(w_dir_deg1);
- int humi1 = forecast["forecastday"][1]["avehumidity"]; DHumi1 = String(humi1);
- //# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- String WDay2 = forecast["forecastday"][2]["date"]["weekday"]; DWDay2 = WDay2;
- String Mon2 = forecast["forecastday"][2]["date"]["monthname_short"]; DMon2 = Mon2;
- int DateDa2 = forecast["forecastday"][2]["date"]["day"]; DDateDa2 = DateDa2<10?"0"+String(DateDa2):String(DateDa2);
- int DateYr2 = forecast["forecastday"][2]["date"]["year"]; DDateYr2 = String(DateYr2).substring(2);
- if (Metric) {
- int Htemp2 = forecast["forecastday"][2]["high"]["celsius"]; DHtemp2 = String(Htemp2);
- int Ltemp2 = forecast["forecastday"][2]["low"]["celsius"]; DLtemp2 = String(Ltemp2);
- int rain2 = forecast["forecastday"][2]["qpf_allday"]["mm"]; DRain2 = String(rain2);
- int w_kph2 = forecast["forecastday"][2]["avewind"]["kph"]; DW_spd2 = String(w_kph2);
- }
- else
- {
- int Htemp2 = forecast["forecastday"][2]["high"]["fahrenheit"]; DHtemp2 = String(Htemp2);
- int Ltemp2 = forecast["forecastday"][2]["low"]["fahrenheit"]; DLtemp2 = String(Ltemp2);
- int rain2 = forecast["forecastday"][2]["qpf_allday"]["in"]; DRain2 = String(rain2);
- int w_mph2 = forecast["forecastday"][2]["avewind"]["mph"]; DW_spd2 = String(w_mph2);
- }
- String icon_url2 = forecast["forecastday"][2]["icon"]; Dicon2 = icon_url2;
- String pop2 = forecast["forecastday"][2]["pop"]; DPop2 = String(pop2);
- String cond2 = forecast["forecastday"][2]["conditions"]; Condi2 = cond2;
- if (Hybrid && Metric)
- {
- int w_mph2 = forecast["forecastday"][2]["avewind"]["mph"]; DW_spd2 = String(w_mph2);
- }
- String w_dir2 = forecast["forecastday"][2]["avewind"]["dir"]; DW_dir2 = String(w_dir2);
- String w_dir_deg2 = forecast["forecastday"][2]["avewind"]["degrees"]; DW_dir_deg2 = String(w_dir_deg2);
- int humi2 = forecast["forecastday"][2]["avehumidity"]; DHumi2 = String(humi2);
- //# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
- String WDay3 = forecast["forecastday"][3]["date"]["weekday"]; DWDay3 = WDay3;
- String Mon3 = forecast["forecastday"][3]["date"]["monthname_short"]; DMon3 = Mon3;
- int DateDa3 = forecast["forecastday"][3]["date"]["day"]; DDateDa3 = DateDa3<10?"0"+String(DateDa3):String(DateDa3);
- int DateYr3 = forecast["forecastday"][3]["date"]["year"]; DDateYr3 = String(DateYr3).substring(2);
- if (Metric) {
- int Htemp3 = forecast["forecastday"][3]["high"]["celsius"]; DHtemp3 = String(Htemp3);
- int Ltemp3 = forecast["forecastday"][3]["low"]["celsius"]; DLtemp3 = String(Ltemp3);
- int rain3 = forecast["forecastday"][3]["qpf_allday"]["mm"]; DRain3 = String(rain3);
- int w_kph3 = forecast["forecastday"][3]["avewind"]["kph"]; DW_spd3 = String(w_kph3);
- }
- else
- {
- int Htemp3 = forecast["forecastday"][3]["high"]["fahrenheit"]; DHtemp3 = String(Htemp3);
- int Ltemp3 = forecast["forecastday"][3]["low"]["fahrenheit"]; DLtemp3 = String(Ltemp3);
- int rain3 = forecast["forecastday"][3]["qpf_allday"]["in"]; DRain3 = String(rain3);
- int w_mph3 = forecast["forecastday"][3]["avewind"]["mph"]; DW_spd3 = String(w_mph3);
- }
- String icon_url3 = forecast["forecastday"][3]["icon"]; Dicon3 = icon_url3;
- String pop3 = forecast["forecastday"][3]["pop"]; DPop3 = String(pop3);
- String cond3 = forecast["forecastday"][3]["conditions"]; Condi3 = cond3;
- if (Hybrid && Metric)
- {
- int w_mph3 = forecast["forecastday"][3]["avewind"]["mph"]; DW_spd3 = String(w_mph3);
- }
- String w_dir3 = forecast["forecastday"][3]["avewind"]["dir"]; DW_dir3 = String(w_dir3);
- String w_dir_deg3 = forecast["forecastday"][3]["avewind"]["degrees"]; DW_dir_deg3 = String(w_dir_deg3);
- int humi3 = forecast["forecastday"][3]["avehumidity"]; DHumi3 = String(humi3);
- Serial.println(F(""));
- Serial.print(F("Emruz: "));
- Serial.println(DWDay0);
- Serial.print(F("Bishtarin Dama: "));
- Serial.println(DHtemp0);
- Serial.print(F("Kamtarin Dama: "));
- Serial.println(DLtemp0);
- Serial.print(F("vaziyate hava: "));
- Serial.println(Condi0);
- Serial.print(F("rotubate: "));
- Serial.println(DHumi0);
- Serial.print(F("sorate bad: "));
- Serial.println(DW_spd0);
- Serial.print(F("jahate bad: "));
- Serial.println(DW_dir0);Serial.println(F(""));
- Serial.print(F("Emruz: "));
- Serial.println(DWDay1);
- Serial.print(F("Bishtarin Dama: "));
- Serial.println(DHtemp1);
- Serial.print(F("Kamtarin Dama: "));
- Serial.println(DLtemp1);
- Serial.print(F("vaziyate hava: "));
- Serial.println(Dicon3);
- Serial.print(F("rotubate: "));
- Serial.println(DHumi1);
- Serial.print(F("sorate bad: "));
- Serial.println(DW_spd1);
- Serial.print(F("jahate bad: "));
- Serial.println(DW_dir1);Serial.println(F(""));
- Serial.print(F("Emruz: "));
- Serial.println(DWDay2);
- Serial.print(F("Bishtarin Dama: "));
- Serial.println(DHtemp2);
- Serial.print(F("Kamtarin Dama: "));
- Serial.println(DLtemp2);
- Serial.print(F("vaziyate hava: "));
- Serial.println(Dicon2);
- Serial.print(F("rotubate: "));
- Serial.println(DHumi2);
- Serial.print(F("sorate bad: "));
- Serial.println(DW_spd2);
- Serial.print(F("jahate bad: "));
- Serial.println(DW_dir2);Serial.println(F(""));
- Serial.print(F("Emruz: "));
- Serial.println(DWDay3);
- Serial.print(F("Bishtarin Dama: "));
- Serial.println(DHtemp3);
- Serial.print(F("Kamtarin Dama: "));
- Serial.println(DLtemp3);
- Serial.print(F("vaziyate hava: "));
- Serial.println(Dicon3);
- Serial.print(F("rotubate: "));
- Serial.println(DHumi3);
- Serial.print(F("sorate bad: "));
- Serial.println(DW_spd3);
- Serial.print(F("jahate bad: "));
- Serial.println(DW_dir3);Serial.println(F(""));
- /*
- Serial.println(DHtemp1);
- Serial.println(DLtemp1);
- Serial.println(DHtemp2);
- Serial.println(DLtemp2);
- Serial.println(DHtemp3);
- Serial.println(DLtemp3);*/
- // JsonObject& current = root["forecast"]["simpleforecast"];
- // int DateDa = current["forecastday"][0]["date"]["day"]; Serial.print(String(DateDa)+"/");
- // int DateMo = current["forecastday"][0]["date"]["month"]; Serial.print((DateMo<10?"0":"")+String(DateMo)+"/");
- // int DateYr = current["forecastday"][0]["date"]["year"]; Serial.print(String(DateYr)+" ");
- // int Htemp = current["forecastday"][0]["high"]["celsius"]; Serial.print("High temp :"+String(Htemp));
- // int Ltemp = current["forecastday"][0]["low"]["celsius"]; Serial.println(" Low temp :"+String(Ltemp));
- //jsonBuffer1.clear();
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment