Hiitsu

Untitled

Jan 18th, 2021
954
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <AutoConnect.h>
  4. #include <ESP8266WebServer.h>
  5.  
  6. ESP8266WebServer Server;          // Replace with WebServer for ESP32
  7. AutoConnect Portal(Server);
  8.  
  9. const char* mqtt_server = "your mqtt broker ip";
  10. String temat;
  11.  
  12. int Relay1 =5;
  13. int Relay2 =4;
  14. int Relay3 =0;
  15. int Relay4 =2;
  16. int Relay5 =14;
  17. int Relay6 =12;
  18. int Relay7 =13;
  19. int Relay8 =15;
  20.  
  21. WiFiClient espClient;
  22. PubSubClient client(espClient);
  23. unsigned long lastMsg = 0;
  24. #define MSG_BUFFER_SIZE  (50)
  25. char msg[MSG_BUFFER_SIZE];
  26. int value = 0;
  27.  
  28. void rootPage() {
  29.   char content[] = "Hello, world";
  30.   Server.send(200, "text/plain", content);
  31. }
  32.  
  33. void setup_wifi(){
  34.   /*delay(10);
  35.   Serial.println();
  36.   Serial.print("Connecting to ");
  37.   Serial.println(ssid);
  38.  
  39.   WiFi.mode(WIFI_STA);
  40.   WiFi.begin(ssid, password);
  41.   */
  42.   while(WiFi.status() != WL_CONNECTED){
  43.     delay(500);
  44.     Serial.print(".");
  45.   }
  46.  
  47.  
  48.   randomSeed(micros());
  49.  
  50.   Serial.println("");
  51.   Serial.println("WiFi connected");
  52.   Serial.println("IP address: ");
  53.   Serial.println(WiFi.localIP());
  54. }
  55. void callback(char* topic, byte* payload, unsigned int length) {
  56.   Serial.print("Message arrived [");
  57.   Serial.print(topic);
  58.   char* c;
  59.   temat.toCharArray(c,sizeof(c));
  60.   topic = c;
  61.   Serial.print("] ");
  62.   for (int i = 0; i < length; i++) {
  63.     Serial.print((char)payload[i]);
  64.   }
  65.   Serial.println();
  66.  
  67.   if((char)payload[0] == '0'){
  68.       digitalWrite(Relay1, HIGH);
  69.       Serial.println("mam wiadomosc szefie : 1");
  70.    }
  71.    else if((char)payload[0] == '1'){
  72.       digitalWrite(Relay1, LOW);
  73.       Serial.println("mam wiadomosc szefie : 2");
  74.    }
  75.    else if((char)payload[0] == '2'){
  76.       digitalWrite(Relay2, HIGH);
  77.       Serial.println("mam wiadomosc szefie : 3");
  78.    }
  79.    else if((char)payload[0] == '3'){
  80.       digitalWrite(Relay2, LOW);
  81.       Serial.println("mam wiadomosc szefie : 4");
  82.    }
  83.    else if((char)payload[0] == '4'){
  84.       digitalWrite(Relay3, HIGH);
  85.       Serial.println("mam wiadomosc szefie : 5");
  86.    }
  87.    else if((char)payload[0] == '5'){
  88.       digitalWrite(Relay3, LOW);
  89.       Serial.println("mam wiadomosc szefie : 6");
  90.    }
  91.    else if((char)payload[0] == '6'){
  92.       digitalWrite(Relay4, HIGH);
  93.       Serial.println("mam wiadomosc szefie : 7");
  94.    }
  95.    else if((char)payload[0] == '7'){
  96.       digitalWrite(Relay4, LOW);
  97.       Serial.println("mam wiadomosc szefie : 8");
  98.    }
  99.    else if((char)payload[0] == '8'){
  100.       digitalWrite(Relay5, HIGH);
  101.       Serial.println("mam wiadomosc szefie : 9");
  102.    }
  103.    else if(((char)payload[0] == '9')){
  104.       digitalWrite(Relay5, LOW);
  105.       Serial.println("mam wiadomosc szefie : 0");
  106.    }
  107.    else if((char)payload[0] == 'a'){
  108.       digitalWrite(Relay6, HIGH);
  109.       Serial.println("mam wiadomosc szefie : a");
  110.    }
  111.    else if((char)payload[0] == 'b'){
  112.       digitalWrite(Relay6, LOW);
  113.       Serial.println("mam wiadomosc szefie : b");
  114.    }
  115.    else if((char)payload[0] == 'c'){
  116.       digitalWrite(Relay7, HIGH);
  117.       Serial.println("mam wiadomosc szefie : c");
  118.    }
  119.    else if((char)payload[0] == 'd'){
  120.       digitalWrite(Relay7, LOW);
  121.       Serial.println("mam wiadomosc szefie : d");
  122.    }
  123.    else if((char)payload[0] == 'e'){
  124.       digitalWrite(Relay8, HIGH);
  125.       Serial.println("mam wiadomosc szefie : e");
  126.    }
  127.    else if((char)payload[0] == 'f'){
  128.       digitalWrite(Relay8, LOW);
  129.       Serial.println("mam wiadomosc szefie : f");
  130.    }
  131.    else if((char)payload[0] == 'g'){
  132.       digitalWrite(Relay1, LOW);
  133.       digitalWrite(Relay2, LOW);
  134.       digitalWrite(Relay3, LOW);
  135.       digitalWrite(Relay4, LOW);
  136.       digitalWrite(Relay5, LOW);
  137.       digitalWrite(Relay6, LOW);
  138.       digitalWrite(Relay7, LOW);
  139.       digitalWrite(Relay8, LOW);
  140.       Serial.println("mam wiadomosc szefie : g");
  141.    }
  142.       else if((char)payload[0] == 'h'){
  143.       digitalWrite(Relay1, HIGH);
  144.       digitalWrite(Relay2, HIGH);
  145.       digitalWrite(Relay3, HIGH);
  146.       digitalWrite(Relay4, HIGH);
  147.       digitalWrite(Relay5, HIGH);
  148.       digitalWrite(Relay6, HIGH);
  149.       digitalWrite(Relay7, HIGH);
  150.       digitalWrite(Relay8, HIGH);
  151.       Serial.println("mam wiadomosc szefie : h");
  152.    }
  153.       else{
  154.       Serial.println("Nie rozpoznano znaku");
  155.    }
  156. }  
  157.  
  158.  
  159. void nadajTemat(){
  160.   String message = "";
  161.   if(Server.arg("temat")== ""){
  162.     message = "nie podano tematu";
  163.   }
  164.   else{
  165.     message = "znaleziono temat = ";
  166.     message += Server.arg("temat");
  167.     temat = Server.arg("temat");
  168.     Serial.println(temat);
  169.   }
  170.     Server.send(200, "text/plain", message);          //Returns the HTTP response
  171.   }
  172.  
  173.  
  174.  
  175. void reconnect(){
  176.   // Loop until we're reconnected
  177.   while (!client.connected()) {
  178.     Serial.print("Attempting MQTT connection...");
  179.     // Create a random client ID
  180.     String clientId = "ESP8266Client-";
  181.     clientId += String(random(0xffff), HEX);
  182.     // Attempt to connect
  183.     if (client.connect(clientId.c_str())) {
  184.       Serial.println("connected");
  185.       // Once connected, publish an announcement...
  186.       client.publish("outTopic", "hello world");
  187.       // ... and resubscribe
  188.       client.subscribe(temat.c_str());
  189.     } else {
  190.       Serial.print("failed, rc=");
  191.       Serial.print(client.state());
  192.       Serial.println(" try again in 5 seconds");
  193.       delay(5000);
  194.     }
  195.    
  196.   }
  197. }
  198. void setup() {
  199.   pinMode(Relay1, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  200.   pinMode(Relay2, OUTPUT);
  201.   pinMode(Relay3, OUTPUT);
  202.   pinMode(Relay4, OUTPUT);
  203.   pinMode(Relay5, OUTPUT);
  204.   pinMode(Relay6, OUTPUT);
  205.   pinMode(Relay7, OUTPUT);
  206.   pinMode(Relay8, OUTPUT);
  207.   Serial.begin(115200);
  208.     Server.on("/", rootPage);
  209.   if (Portal.begin()) {
  210.     Serial.println("WiFi connected: " + WiFi.localIP().toString());
  211.   }
  212.   Server.on("/temat", nadajTemat);
  213.   setup_wifi();
  214.   client.setServer(mqtt_server, 1883);
  215.   client.setCallback(callback);
  216.  
  217. }
  218.  
  219. void loop() {
  220.   // put your main code here, to run repeatedly:
  221.   //digitalWrite(Relay0, HIGH);
  222.   //delay(1000);
  223.   //digitalWrite(Relay0, LOW);
  224.  // delay(1000);
  225.  Portal.handleClient();
  226.   if(!client.connected()){
  227.     reconnect();
  228.   }
  229.   client.loop();
  230.    unsigned long now = millis();
  231.   if (now - lastMsg > 2000) {
  232.     lastMsg = now;
  233.     ++value;
  234.     snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
  235.     Serial.print("Publish message: ");
  236.     Serial.println(msg);
  237.     client.publish("outTopic", msg);
  238.   }
  239.  
  240.  
  241. }
RAW Paste Data