Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WebServer.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <WakeOnLan.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- #define TELEGRAM_BOT_TOKEN "PRIVATE"
- struct targetDevice {
- byte mac[6]; //The targets MAC address
- String deviceName;
- };
- targetDevice devices[] ={
- {{ PRIVATE }, "Server"}
- };
- // Change to match how many devices are in the above array.
- int numDevices = 1;
- WiFiUDP UDP;
- /**
- * This will brodcast the Magic packet on your entire network range.
- */
- IPAddress computer_ip(255,255,255,255);
- void sendWOL();
- // This is the Wifi client that supports HTTPS
- WiFiClientSecure client;
- UniversalTelegramBot bot(TELEGRAM_BOT_TOKEN, client);
- int delayBetweenChecks = 1000;
- unsigned long lastTimeChecked; //last time messages' scan has been done
- //
- //
- // Data wire is plugged into port D2 on the ESP8266
- #define ONE_WIRE_BUS 4
- // Setup a oneWire instance to communicate with any OneWire devices
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- // variable to hold device addresses
- DeviceAddress Thermometer;
- int deviceCount;
- float tempSensor1, tempSensor2, tempSensor3, tempSensor4, tempSensor5, tempSensor6, tempSensor7;
- uint8_t sensor1[8] = { 0x28, 0x6B, 0x77, 0xBB, 0x1A, 0x19, 0x01, 0x3C };
- uint8_t sensor2[8] = { 0x28, 0x1C, 0xED, 0xCE, 0x1A, 0x19, 0x01, 0x02 };
- uint8_t sensor3[8] = { 0x28, 0x53, 0x48, 0xC5, 0x1A, 0x19, 0x01, 0x26 };
- uint8_t sensor4[8] = { 0x28, 0x4F, 0xB6, 0xCD, 0x1A, 0x19, 0x01, 0x18 };
- uint8_t sensor5[8] = { 0x28, 0x1A, 0xC3, 0xBA, 0x1A, 0x19, 0x01, 0x4C };
- uint8_t sensor6[8] = { 0x28, 0x25, 0xF2, 0xC8, 0x1A, 0x19, 0x01, 0x63 };
- uint8_t sensor7[8] = { 0x28, 0x6B, 0xA3, 0xCE, 0x1A, 0x19, 0x01, 0x54 }; //sensor 8: 0x28, 0xE5, 0x3F, 0xB9, 0x1A, 0x19, 0x01, 0x15
- /*Put your SSID & Password*/
- const char* ssid = "PRIVATE"; // Enter SSID here
- const char* password = "PRIVATE"; //Enter Password here
- ESP8266WebServer server(80);
- void setup() {
- Serial.begin(115200);
- delay(100);
- sensors.begin();
- // locate devices on the bus
- Serial.println("Locating devices...");
- Serial.print("Found ");
- deviceCount = sensors.getDeviceCount();
- Serial.print(deviceCount, DEC);
- Serial.println(" devices.");
- Serial.println("");
- Serial.println("Printing addresses...");
- for (int i = 0; i < deviceCount; i++)
- {
- Serial.print("Sensor ");
- Serial.print(i+1);
- Serial.print(" : ");
- sensors.getAddress(Thermometer, i);
- printAddress(Thermometer);
- }
- Serial.println("Connecting to ");
- Serial.println(ssid);
- //connect to your local wi-fi network
- WiFi.begin(ssid, password);
- //check wi-fi is connected to wi-fi network
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected..!");
- Serial.print("Got IP: "); Serial.println(WiFi.localIP());
- client.setInsecure();
- bot.longPoll = 60;
- UDP.begin(9);
- server.on("/", handle_OnConnect);
- server.onNotFound(handle_NotFound);
- server.begin();
- Serial.println("HTTP server started");
- }
- void handleNewMessages(int numNewMessages) {
- for (int i = 0; i < numNewMessages; i++) {
- if (bot.messages[i].chat_id.equals("PRIVATE")) {
- // If the type is a "callback_query", a inline keyboard button was pressed
- if (bot.messages[i].type == F("callback_query")) {
- String text = bot.messages[i].text;
- Serial.print("Call back button pressed with text: ");
- Serial.println(text);
- if (text.startsWith("WOL")) {
- text.replace("WOL", "");
- int index = text.toInt();
- Serial.print("Sending WOL to: ");
- Serial.println(devices[index].deviceName);
- WakeOnLan::sendWOL(computer_ip, UDP, devices[index].mac, sizeof devices[index].mac);
- }
- } else {
- String chat_id = String(bot.messages[i].chat_id);
- String text = bot.messages[i].text;
- if (text == F("/wol")) {
- // Keyboard Json is an array of arrays.
- // The size of the main array is how many row options the keyboard has
- // The size of the sub arrays is how many coloums that row has
- // "The Text" property is what shows up in the keyboard
- // The "callback_data" property is the text that gets sent when pressed
- String keyboardJson = "[";
- for(int i = 0; i< numDevices; i++)
- {
- keyboardJson += "[{ \"text\" : \"" + devices[i].deviceName + "\", \"callback_data\" : \"WOL" + String(i) + "\" }]";
- if(i + 1 < numDevices){
- keyboardJson += ",";
- }
- }
- keyboardJson += "]";
- bot.sendMessageWithInlineKeyboard(chat_id, "Send WOL to following devices:", "", keyboardJson);
- }
- // When a user first uses a bot they will send a "/start" command
- // So this is a good place to let the users know what commands are available
- if (text == F("/start")) {
- bot.sendMessage(chat_id, "/wol : returns list of devices to send WOL to\n", "Markdown");
- }
- }
- }
- }
- }
- void loop() {
- server.handleClient();
- if (millis() > lastTimeChecked + delayBetweenChecks) {
- // getUpdates returns 1 if there is a new message from Telegram
- int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- if (numNewMessages) {
- Serial.println("got response");
- handleNewMessages(numNewMessages);
- }
- lastTimeChecked = millis();
- }
- }
- void handle_OnConnect() {
- sensors.requestTemperatures();
- tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
- tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
- tempSensor3 = sensors.getTempC(sensor3); // Gets the values of the temperature
- tempSensor4 = sensors.getTempC(sensor4);
- tempSensor5 = sensors.getTempC(sensor5);
- tempSensor6 = sensors.getTempC(sensor6);
- tempSensor7 = sensors.getTempC(sensor7);
- server.send(200, "text/html", SendHTML(tempSensor1,tempSensor2,tempSensor3,tempSensor4,tempSensor5,tempSensor6,tempSensor7));
- }
- void handle_NotFound(){
- server.send(404, "text/plain", "Not found");
- }
- String SendHTML(float tempSensor1,float tempSensor2,float tempSensor3,float tempSensor4,float tempSensor5,float tempSensor6,float tempSensor7){
- String ptr = "<!DOCTYPE html> <html>\n";
- ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
- ptr +="<title>ESP8266 Temperature Monitor</title>\n";
- ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
- ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
- ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
- ptr +="</style>\n";
- ptr +="<script>\n";
- ptr +="setInterval(loadDoc,1000);\n";
- ptr +="function loadDoc() {\n";
- ptr +="var xhttp = new XMLHttpRequest();\n";
- ptr +="xhttp.onreadystatechange = function() {\n";
- ptr +="if (this.readyState == 4 && this.status == 200) {\n";
- ptr +="document.body.innerHTML =this.responseText}\n";
- ptr +="};\n";
- ptr +="xhttp.open(\"GET\", \"/\", true);\n";
- ptr +="xhttp.send();\n";
- ptr +="}\n";
- ptr +="</script>\n";
- ptr +="</head>\n";
- ptr +="<body>\n";
- ptr +="<div id=\"webpage\">\n";
- ptr +="<h1>Temperatuur</h1>\n";
- ptr +="<p>Buitentemperatuur: ";
- ptr +=tempSensor1;
- ptr +="°C</p>";
- ptr +="<p>Buffervat boven: ";
- ptr +=tempSensor2;
- ptr +="°C</p>";
- ptr +="<p>Buffervat beneden: ";
- ptr +=tempSensor3;
- ptr +="°C</p>";
- ptr +="<p>Sanitair warm water: ";
- ptr +=tempSensor4;
- ptr +="°C</p>";
- ptr +="<p>Pomp kachel: ";
- ptr +=tempSensor5;
- ptr +="°C</p>";
- ptr +="<p>Ketel: ";
- ptr +=tempSensor6;
- ptr +="°C</p>";
- ptr +="<p>Zonnecollector: ";
- ptr +=tempSensor7;
- ptr +="°C</p>";
- ptr +="</div>\n";
- ptr +="</body>\n";
- ptr +="</html>\n";
- return ptr;
- }
- void printAddress(DeviceAddress deviceAddress)
- {
- for (uint8_t i = 0; i < 8; i++)
- {
- Serial.print("0x");
- if (deviceAddress[i] < 0x10) Serial.print("0");
- Serial.print(deviceAddress[i], HEX);
- if (i < 7) Serial.print(", ");
- }
- Serial.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment