Advertisement
dario92vv

Untitled

Jun 13th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1.  
  2.  
  3. #include "CTBot.h"
  4. CTBot myBot;
  5.  
  6. String ssid = "S10+Dario"; // REPLACE mySSID WITH YOUR WIFI SSID
  7. String pass = "1234567890"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
  8. String token = "xx"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
  9. uint8_t led = 2; // the onboard ESP8266 LED.
  10. // If you have a NodeMCU you can use the BUILTIN_LED pin
  11. // (replace 2 with BUILTIN_LED)
  12.  
  13. char ciao [9];
  14. int num;
  15.  
  16. void setup() {
  17. // initialize the Serial
  18. Serial.begin(9600);
  19. Serial.println("Starting TelegramBot...");
  20.  
  21. // connect the ESP8266 to the desired access point
  22. myBot.wifiConnect(ssid, pass);
  23.  
  24. // set the telegram bot token
  25. myBot.setTelegramToken(token);
  26.  
  27. // check if all things are ok
  28. if (myBot.testConnection())
  29. Serial.println("\ntestConnection OK");
  30. else
  31. Serial.println("\ntestConnection NOK");
  32.  
  33. // set the pin connected to the LED to act as output pin
  34. pinMode(led, OUTPUT);
  35. digitalWrite(led, HIGH); // turn off the led (inverted logic!)
  36.  
  37. }
  38.  
  39. void loop() {
  40. // a variable to store telegram message data
  41. TBMessage msg;
  42. while (Serial.available()==false){
  43. int a=Serial.readBytes(ciao,9);
  44. num=atoi(ciao);
  45. // stringa = String(a);
  46. Serial.print(num);
  47.  
  48. // if there is an incoming message...
  49. if (myBot.getNewMessage(msg)) {
  50.  
  51. if (msg.text.equalsIgnoreCase("LIGHT ON")) { // if the received message is "LIGHT ON"...
  52.  
  53. //Serial.readBytes(stringa,20);
  54. //Serial.println(stringa);
  55. digitalWrite(led, LOW); // turn on the LED (inverted logic!)
  56. myBot.sendMessage(msg.sender.id, num); // notify the sender
  57. }
  58. else if (msg.text.equalsIgnoreCase("LIGHT OFF")) { // if the received message is "LIGHT OFF"...
  59. digitalWrite(led, HIGH); // turn off the led (inverted logic!)
  60. myBot.sendMessage(msg.sender.id, "Light is now OFF"); // notify the sender
  61. }
  62. else { // otherwise...
  63. // generate the message for the sender
  64. String reply;
  65. reply = (String)"Welcome " + msg.sender.username + (String)". Try LIGHT ON or LIGHT OFF.";
  66. myBot.sendMessage(msg.sender.id, reply); // and send it
  67. }
  68. }}
  69. // wait 500 milliseconds
  70. delay(500);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement