nickmansrob

Untitled

Jun 18th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. #include <ESP8266WebServer.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4. #include <WakeOnLan.h>
  5. #include <UniversalTelegramBot.h>
  6. #include <ArduinoJson.h>
  7.  
  8. #define TELEGRAM_BOT_TOKEN "PRIVATE"
  9.  
  10. struct targetDevice {
  11.  byte mac[6]; //The targets MAC address
  12.  String deviceName;
  13. };
  14.  
  15. targetDevice devices[] ={
  16.  {{ PRIVATE }, "Server"}
  17. };
  18.  
  19. // Change to match how many devices are in the above array.
  20. int numDevices = 1;
  21.  
  22. WiFiUDP UDP;
  23. /**
  24. * This will brodcast the Magic packet on your entire network range.
  25. */
  26. IPAddress computer_ip(255,255,255,255);
  27.  
  28. void sendWOL();
  29.  
  30. // This is the Wifi client that supports HTTPS
  31. WiFiClientSecure client;
  32. UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
  33.  
  34. int delayBetweenChecks = 1000;
  35. unsigned long lastTimeChecked;   //last time messages' scan has been done
  36.  
  37. //
  38. //
  39.  
  40. // Data wire is plugged into port D2 on the ESP8266
  41. #define ONE_WIRE_BUS 4
  42.  
  43. // Setup a oneWire instance to communicate with any OneWire devices
  44. OneWire oneWire(ONE_WIRE_BUS);
  45.  
  46. // Pass our oneWire reference to Dallas Temperature.
  47. DallasTemperature sensors(&oneWire);
  48.  
  49. // variable to hold device addresses
  50. DeviceAddress Thermometer;
  51.  
  52. int deviceCount;
  53.  
  54. float tempSensor1, tempSensor2, tempSensor3, tempSensor4, tempSensor5, tempSensor6, tempSensor7;
  55.  
  56. uint8_t sensor1[8] = { 0x28, 0x6B, 0x77, 0xBB, 0x1A, 0x19, 0x01, 0x3C  };
  57. uint8_t sensor2[8] = { 0x28, 0x1C, 0xED, 0xCE, 0x1A, 0x19, 0x01, 0x02  };
  58. uint8_t sensor3[8] = { 0x28, 0x53, 0x48, 0xC5, 0x1A, 0x19, 0x01, 0x26  };
  59. uint8_t sensor4[8] = { 0x28, 0x4F, 0xB6, 0xCD, 0x1A, 0x19, 0x01, 0x18  };
  60. uint8_t sensor5[8] = { 0x28, 0x1A, 0xC3, 0xBA, 0x1A, 0x19, 0x01, 0x4C  };
  61. uint8_t sensor6[8] = { 0x28, 0x25, 0xF2, 0xC8, 0x1A, 0x19, 0x01, 0x63  };
  62. uint8_t sensor7[8] = { 0x28, 0x6B, 0xA3, 0xCE, 0x1A, 0x19, 0x01, 0x54  }; //sensor 8: 0x28, 0xE5, 0x3F, 0xB9, 0x1A, 0x19, 0x01, 0x15
  63.  
  64. /*Put your SSID & Password*/
  65. const char* ssid = "PRIVATE";  // Enter SSID here
  66. const char* password = "PRIVATE";  //Enter Password here
  67.  
  68. ESP8266WebServer server(80);            
  69.  
  70. void setup() {
  71.  
  72.  Serial.begin(115200);
  73.  delay(100);
  74.  
  75.  sensors.begin();
  76.  
  77.  // locate devices on the bus
  78.  Serial.println("Locating devices...");
  79.  Serial.print("Found ");
  80.  deviceCount = sensors.getDeviceCount();
  81.  Serial.print(deviceCount, DEC);
  82.  Serial.println(" devices.");
  83.  Serial.println("");
  84.  
  85.  Serial.println("Printing addresses...");
  86.  for (int i = 0;  i < deviceCount;  i++)
  87.  {
  88.    Serial.print("Sensor ");
  89.    Serial.print(i+1);
  90.    Serial.print(" : ");
  91.    sensors.getAddress(Thermometer, i);
  92.    printAddress(Thermometer);
  93.  }
  94.  
  95.  Serial.println("Connecting to ");
  96.  Serial.println(ssid);
  97.  
  98.  //connect to your local wi-fi network
  99.  WiFi.begin(ssid, password);
  100.  
  101.  //check wi-fi is connected to wi-fi network
  102.  while (WiFi.status() != WL_CONNECTED) {
  103.  delay(1000);
  104.  Serial.print(".");
  105.  }
  106.  Serial.println("");
  107.  Serial.println("WiFi connected..!");
  108.  Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  109.  
  110.  client.setInsecure();
  111.  bot.longPoll = 60;
  112.  
  113.  UDP.begin(9);
  114.  
  115.  server.on("/", handle_OnConnect);
  116.  server.onNotFound(handle_NotFound);
  117.  
  118.  server.begin();
  119.  Serial.println("HTTP server started");
  120. }
  121.  
  122. void handleNewMessages(int numNewMessages) {
  123.  
  124.  for (int i = 0; i < numNewMessages; i++) {
  125.    if (bot.messages[i].chat_id.equals("PRIVATE")) {
  126.    // If the type is a "callback_query", a inline keyboard button was pressed
  127.    if (bot.messages[i].type ==  F("callback_query")) {
  128.      String text = bot.messages[i].text;
  129.      Serial.print("Call back button pressed with text: ");
  130.      Serial.println(text);
  131.  
  132.      if (text.startsWith("WOL")) {
  133.        text.replace("WOL", "");
  134.        int index = text.toInt();
  135.        Serial.print("Sending WOL to: ");
  136.        Serial.println(devices[index].deviceName);
  137.        WakeOnLan::sendWOL(computer_ip, UDP, devices[index].mac, sizeof devices[index].mac);
  138.      }
  139.    } else {
  140.      String chat_id = String(bot.messages[i].chat_id);
  141.      String text = bot.messages[i].text;
  142.  
  143.      if (text == F("/wol")) {
  144.  
  145.        // Keyboard Json is an array of arrays.
  146.        // The size of the main array is how many row options the keyboard has
  147.        // The size of the sub arrays is how many coloums that row has
  148.  
  149.        // "The Text" property is what shows up in the keyboard
  150.        // The "callback_data" property is the text that gets sent when pressed  
  151.        String keyboardJson = "[";
  152.        for(int i = 0; i< numDevices; i++)
  153.        {
  154.          keyboardJson += "[{ \"text\" : \"" + devices[i].deviceName + "\", \"callback_data\" : \"WOL" + String(i) + "\" }]";
  155.          if(i + 1 < numDevices){
  156.            keyboardJson += ",";
  157.          }
  158.        }
  159.        keyboardJson += "]";
  160.        bot.sendMessageWithInlineKeyboard(chat_id, "Send WOL to following devices:", "", keyboardJson);
  161.      }
  162.  
  163.      // When a user first uses a bot they will send a "/start" command
  164.      // So this is a good place to let the users know what commands are available
  165.      if (text == F("/start")) {
  166.  
  167.        bot.sendMessage(chat_id, "/wol : returns list of devices to send WOL to\n", "Markdown");
  168.      }
  169.    }
  170.  }
  171. }
  172. }
  173.  
  174. void loop() {
  175.  server.handleClient();
  176.  
  177.  if (millis() > lastTimeChecked + delayBetweenChecks)  {
  178.  
  179.    // getUpdates returns 1 if there is a new message from Telegram
  180.    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  181.  
  182.    if (numNewMessages) {
  183.      Serial.println("got response");
  184.      handleNewMessages(numNewMessages);
  185.    }
  186.  
  187.    lastTimeChecked = millis();
  188.  }
  189. }
  190.  
  191. void handle_OnConnect() {
  192.  sensors.requestTemperatures();
  193.  tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
  194.  tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
  195.  tempSensor3 = sensors.getTempC(sensor3); // Gets the values of the temperature
  196.  tempSensor4 = sensors.getTempC(sensor4);
  197.  tempSensor5 = sensors.getTempC(sensor5);
  198.  tempSensor6 = sensors.getTempC(sensor6);
  199.  tempSensor7 = sensors.getTempC(sensor7);
  200.  server.send(200, "text/html", SendHTML(tempSensor1,tempSensor2,tempSensor3,tempSensor4,tempSensor5,tempSensor6,tempSensor7));
  201. }
  202.  
  203. void handle_NotFound(){
  204.  server.send(404, "text/plain", "Not found");
  205. }
  206.  
  207. String SendHTML(float tempSensor1,float tempSensor2,float tempSensor3,float tempSensor4,float tempSensor5,float tempSensor6,float tempSensor7){
  208.  String ptr = "<!DOCTYPE html> <html>\n";
  209.  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  210.  ptr +="<title>ESP8266 Temperature Monitor</title>\n";
  211.  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  212.  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  213.  ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  214.  ptr +="</style>\n";
  215.  ptr +="<script>\n";
  216.  ptr +="setInterval(loadDoc,1000);\n";
  217.  ptr +="function loadDoc() {\n";
  218.  ptr +="var xhttp = new XMLHttpRequest();\n";
  219.  ptr +="xhttp.onreadystatechange = function() {\n";
  220.  ptr +="if (this.readyState == 4 && this.status == 200) {\n";
  221.  ptr +="document.body.innerHTML =this.responseText}\n";
  222.  ptr +="};\n";
  223.  ptr +="xhttp.open(\"GET\", \"/\", true);\n";
  224.  ptr +="xhttp.send();\n";
  225.  ptr +="}\n";
  226.  ptr +="</script>\n";
  227.  ptr +="</head>\n";
  228.  ptr +="<body>\n";
  229.  ptr +="<div id=\"webpage\">\n";
  230.  ptr +="<h1>Temperatuur</h1>\n";
  231.  ptr +="<p>Buitentemperatuur: ";
  232.  ptr +=tempSensor1;
  233.  ptr +="&deg;C</p>";
  234.  ptr +="<p>Buffervat boven: ";
  235.  ptr +=tempSensor2;
  236.  ptr +="&deg;C</p>";
  237.  ptr +="<p>Buffervat beneden: ";
  238.  ptr +=tempSensor3;
  239.  ptr +="&deg;C</p>";
  240.  ptr +="<p>Sanitair warm water: ";
  241.  ptr +=tempSensor4;
  242.  ptr +="&deg;C</p>";
  243.  ptr +="<p>Pomp kachel: ";
  244.  ptr +=tempSensor5;
  245.  ptr +="&deg;C</p>";
  246.  ptr +="<p>Ketel: ";
  247.  ptr +=tempSensor6;
  248.  ptr +="&deg;C</p>";
  249.  ptr +="<p>Zonnecollector: ";
  250.  ptr +=tempSensor7;
  251.  ptr +="&deg;C</p>";
  252.  ptr +="</div>\n";
  253.  ptr +="</body>\n";
  254.  ptr +="</html>\n";
  255.  return ptr;
  256. }
  257.  
  258. void printAddress(DeviceAddress deviceAddress)
  259. {
  260.  for (uint8_t i = 0; i < 8; i++)
  261.  {
  262.    Serial.print("0x");
  263.    if (deviceAddress[i] < 0x10) Serial.print("0");
  264.    Serial.print(deviceAddress[i], HEX);
  265.    if (i < 7) Serial.print(", ");
  266.  }
  267.  Serial.println("");
  268. }
Advertisement
Add Comment
Please, Sign In to add comment