Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <PubSubClient.h>
  5. #include <Nextion.h>
  6.  
  7. const char* mqtt_server = "";
  8. const char* ssid = "";
  9. const char* password = "";
  10. //const char* mqtt_user = "1";
  11. //const char* mqtt_password = "3";
  12.  
  13.  
  14. //String temp = "";
  15. //String luft = "";
  16. //String raumtemp = "";
  17.  
  18. WiFiClient net;
  19. PubSubClient client(net);
  20.  
  21. NexButton switch0 = NexButton(1, 3, "switch0");
  22. //NexButton heizung = NexButton(0, 4, "heizung"); // Button On Off
  23. //NexButton heizungZuruck = NexButton(1, 1, "heizungZuruck");
  24. //NexButton r0 = NexButton(2, 3, "r0");
  25. //NexButton r50 = NexButton(2, 2, "r50");
  26. //NexButton r100 = NexButton(2, 1, "r100");
  27.  
  28. NexTouch *nex_listen_list[] = {
  29.   &switch0,
  30. //  &heizung,
  31. //  &heizungZuruck,
  32. //  &r0,
  33. //  &r50,
  34. //  &r100,
  35.   NULL // String terminated
  36. };
  37.  
  38.  
  39. void setup() {
  40.   pinMode(BUILTIN_LED, OUTPUT);
  41.   pinMode(D1, OUTPUT);
  42.  
  43.   Serial.begin(9600);
  44.  
  45.   switch0.attachPush(switch0PushCallback);  // Button press
  46.   //lichtonoff.attachPop(lichtonoffPopCallback);
  47.   //heizung.attachPush(heizungPushCallback);
  48.   //heizungZuruck.attachPush(heizungZuruckPushCallback); // Button release
  49.   //r0.attachPush(r0PushCallback);
  50.   //r50.attachPush(r50PushCallback);
  51.   //r100.attachPush(r100PushCallback);
  52.   //(500);
  53.   connect();
  54. }
  55.  
  56. void loop() {
  57.   if (!client.connected()) {
  58.     reconnect();
  59.   }
  60.   client.loop();
  61.  
  62.   nexLoop(nex_listen_list);  // Check touch events
  63.     sendDataToDisplay();
  64.     client.loop();
  65.     refreshDisplay();
  66.   delay(500);
  67. }
  68.  
  69. void refreshDisplay(){
  70.  
  71.  
  72.  
  73. }
  74.  
  75.  
  76. void connect() {
  77.   Serial.println("Connecting Wifi");
  78.  
  79.   WiFi.mode(WIFI_AP_STA);
  80.   WiFi.begin(ssid, password);
  81.  
  82.   // WiFi
  83.   while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  84.     WiFi.begin(ssid, password);
  85.   }
  86.  
  87.   Serial.println("Connecting MQTT");
  88.  
  89.   // MQTT
  90.   client.setServer(mqtt_server, 1885);
  91.   client.setCallback(callback);
  92. }
  93.  
  94. void callback(char* topic, byte* payload, unsigned int length) {
  95.   String sTopic = String(topic);
  96.  
  97.   if (sTopic == "NextionSwitch/Wohnzimmer/Eingabe") {
  98.       // Workaround to get int from payload
  99.       temp = "";
  100.       Serial.print("");
  101.       for (int i = 0; i < length; i++) {
  102.       temp += (char)payload[i];  }
  103.       if(temp.indexOf("luft") == 0) {
  104.         luft = temp;
  105.       } else if(temp.indexOf("temp") == 0) {
  106.         raumtemp = temp;
  107.       } else {
  108.         Serial.print(temp);
  109.         for(int i = 0; i < 3;i++) {Serial.write(0xff);}
  110.       }
  111.  
  112.       sendDataToDisplay();
  113.     }
  114.      else {
  115.       Serial.print("Dieses Topic kenne ich nicht: ");
  116.       Serial.println(sTopic);
  117.     }
  118. }
  119.  
  120. void sendDataToDisplay() {
  121.   if(raumtemp != "") {
  122.     Serial.print(raumtemp);
  123.     for(int i = 0; i < 3;i++) {Serial.write(0xff);}
  124.   }
  125.  
  126.   if(luft != "") {
  127.     Serial.print(luft);
  128.     for(int i = 0; i < 3;i++) {Serial.write(0xff);}
  129.   }
  130. }
  131.  
  132. void reconnect() {
  133.   while (!client.connected()) {
  134.     String clientId = "NextionSwitch-";
  135.     clientId += String(random(0xffff), HEX);
  136.  
  137.     if (client.connect(clientId.c_str())) {
  138.       client.subscribe("NextionSwitch/Wohnzimmer/Eingabe");
  139.     } else {
  140.       Serial.print("failed, rc=");
  141.       Serial.print(client.state());
  142.       Serial.println(" try again in 5 seconds");
  143.  
  144.       delay(5000);
  145.     }
  146.   }
  147. }
  148.  
  149.  
  150. //void heizungZuruckPushCallback(void *ptr) {
  151. //  digitalWrite(BUILTIN_LED, HIGH);  // Turn ON internal LED
  152. //   client.publish("Wohnung/kinderZimmer/display", "heizungZuruck");
  153. //}
  154.  
  155.  
  156. //void heizungPushCallback(void *ptr) {
  157. //  digitalWrite(BUILTIN_LED, HIGH);  // Turn ON internal LED
  158. //   client.publish("Wohnung/kinderZimmer/display", "heizung");
  159. //}
  160.  
  161. void switch0PushCallback(void *ptr) {
  162. if (digitalRead(D1)==LOW) {
  163.   // Anweisungsblock für wahr
  164.   digitalWrite(D1,HIGH);
  165.   client.publish("NextionSwitch/Wohnzimmer/Power", "licht an");
  166.   } else {
  167.   // Anweisungsblock für falsch
  168.   digitalWrite(D1,LOW);
  169.   client.publish("NextionSwitch/Wohnzimmer/Power", "licht aus");
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement