SHARE
TWEET

Untitled

a guest May 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4.  
  5. // Update these with values suitable for your network.
  6.  
  7. const char* ssid = "Mile Panika 2";
  8. const char* password = "Radioemfdetektor55";
  9. const char* mqtt_server = "192.168.1.37";
  10.  
  11. WiFiClient espClient;
  12. PubSubClient client(espClient);
  13. long lastMsg = 0;
  14. long lastMsg2 = 0;
  15. char msg[50];
  16.  
  17.  
  18. void setup_wifi() {
  19.  
  20.   delay(10);
  21.   // We start by connecting to a WiFi network
  22.   Serial.println();
  23.   Serial.print("Connecting to ");
  24.   Serial.println(ssid);
  25.  
  26.   WiFi.begin(ssid, password);
  27.  
  28.   while (WiFi.status() != WL_CONNECTED) {
  29.     delay(500);
  30.     Serial.print(".");
  31.   }
  32.  
  33.   randomSeed(micros());
  34.  
  35.   Serial.println("");
  36.   Serial.println("WiFi connected");
  37.   Serial.println("IP address: ");
  38.   Serial.println(WiFi.localIP());
  39. }
  40.  
  41. void callback(char* topic, byte* message, unsigned int length) {
  42.  Serial.print("Message arrived on topic: ");
  43.   Serial.print(topic);
  44.   Serial.print(". Message: ");
  45.   String messageTemp;
  46.  
  47.   for (int i = 0; i < length; i++) {
  48.     Serial.print((char)message[i]);
  49.     messageTemp += (char)message[i];
  50.   }
  51.   Serial.println();
  52.  
  53.   if (String(topic) == "Mjolnir001/D0") {
  54.       if(messageTemp =="1"){
  55.         digitalWrite(D0, HIGH);
  56.       client.publish("Mjolnir001/status/D0","HIGH");
  57.  
  58.       }
  59.       else if(messageTemp == "0"){
  60.         digitalWrite(D0, LOW);
  61.       client.publish("Mjolnir001/status/D0","LOW");
  62.  
  63.       }
  64.   }
  65.   if (String(topic) == "Mjolnir001/D1") {
  66.       if(messageTemp =="1"){
  67.         digitalWrite(D1, HIGH);
  68.       client.publish("Mjolnir001/status/D1","HIGH");
  69.  
  70.       }
  71.       else if(messageTemp == "0"){
  72.         digitalWrite(D1, LOW);
  73.       client.publish("Mjolnir001/status/D1","LOW");
  74.  
  75.       }
  76.      
  77.   }
  78.   if (String(topic) == "Mjolnir001/D2") {
  79.       if(messageTemp =="1"){
  80.         digitalWrite(D2, HIGH);
  81.       client.publish("Mjolnir001/status/D2","HIGH");
  82.  
  83.       }
  84.       else if(messageTemp == "0"){
  85.         digitalWrite(D2, LOW);
  86.       client.publish("Mjolnir001/status/D2","LOW");
  87.  
  88.       }
  89.      
  90.       }
  91.   if (String(topic) == "Mjolnir001/D3") {
  92.       if(messageTemp =="1"){
  93.         digitalWrite(D3, HIGH);
  94.       client.publish("Mjolnir001/status/D3","HIGH");
  95.  
  96.       }
  97.       else if(messageTemp == "0"){
  98.         digitalWrite(D3, LOW);
  99.       client.publish("Mjolnir001/status/D3","LOW");
  100.  
  101.       }
  102.      
  103.   }
  104.   if (String(topic) == "Mjolnir001/D4") {
  105.       if(messageTemp =="1"){
  106.         digitalWrite(D4, HIGH);
  107.       client.publish("Mjolnir001/status/D4","HIGH");
  108.  
  109.       }
  110.       else if(messageTemp == "0"){
  111.         digitalWrite(D4, LOW);
  112.       client.publish("Mjolnir001/status/D4","LOW");
  113.  
  114.       }
  115.      
  116.   }
  117.   if (String(topic) == "Mjolnir001/D5") {
  118.       if(messageTemp =="1"){
  119.         digitalWrite(D5, HIGH);
  120.       client.publish("Mjolnir001/status/D5","HIGH");
  121.  
  122.       }
  123.       else if(messageTemp == "0"){
  124.         digitalWrite(D5, LOW);
  125.       client.publish("Mjolnir001/status/D5","LOW");
  126.  
  127.       }
  128.      
  129.   }
  130.   if (String(topic) == "Mjolnir001/D6") {
  131.       if(messageTemp =="1"){
  132.         digitalWrite(D6, HIGH);
  133.       client.publish("Mjolnir001/status/D6","HIGH");
  134.  
  135.       }
  136.       else if(messageTemp == "0"){
  137.         digitalWrite(D6, LOW);
  138.       client.publish("Mjolnir001/status/D6","LOW");
  139.  
  140.       }
  141.      
  142.   }
  143.   if (String(topic) == "Mjolnir001/D7") {
  144.       if(messageTemp =="1"){
  145.         digitalWrite(D7, HIGH);
  146.       client.publish("Mjolnir001/status/D7","HIGH");
  147.  
  148.       }
  149.       else if(messageTemp == "0"){
  150.         digitalWrite(D7, LOW);
  151.       client.publish("Mjolnir001/status/D7","LOW");
  152.  
  153.       }
  154.      
  155.   }
  156.   if (String(topic) == "Mjolnir001/D8") {
  157.       if(messageTemp =="1"){
  158.         digitalWrite(D8, HIGH);
  159.       client.publish("Mjolnir001/status/D8","HIGH");
  160.  
  161.       }
  162.       else if(messageTemp == "0"){
  163.         digitalWrite(D8, LOW);
  164.       client.publish("Mjolnir001/status/D8","LOW");
  165.  
  166.       }
  167.      
  168.   }
  169.   if (String(topic) == "Mjolnir001/TX") {
  170.       if(messageTemp =="1"){
  171.         digitalWrite(TX, HIGH);
  172.       client.publish("Mjolnir001/status/TX","HIGH");
  173.  
  174.       }
  175.       else if(messageTemp == "0"){
  176.         digitalWrite(TX, LOW);
  177.       client.publish("Mjolnir001/status/TX","LOW");
  178.  
  179.       }
  180.      
  181.   }
  182.   if (String(topic) == "Mjolnir001/RX") {
  183.       if(messageTemp =="1"){
  184.         digitalWrite(RX, HIGH);
  185.       client.publish("Mjolnir001/status/RX","HIGH");
  186.        
  187.       }
  188.       else if(messageTemp == "0"){
  189.         digitalWrite(RX, LOW);
  190.       client.publish("Mjolnir001/status/RX","LOW");
  191.  
  192.       }
  193.      
  194.   }
  195.  
  196. }
  197. void reconnect() {
  198.   // Loop until we're reconnected
  199.   while (!client.connected()) {
  200.     Serial.print("Attempting MQTT connection...");
  201.     // Create a random client ID
  202.     String clientId = "ESP8266Client-";
  203.     clientId += String(random(0xffff), HEX);
  204.     // Attempt to connect
  205.     if (client.connect(clientId.c_str())) {
  206.       Serial.println("connected");
  207.        digitalWrite(BUILTIN_LED, LOW);
  208.       client.subscribe("#"); //  YourEmailId/Light // in my case email id is "satyasankar.09@gmail.com/Light" --> This is my topic Name
  209.     } else {
  210.       Serial.print("failed, rc=");
  211.       Serial.print(client.state());
  212.       Serial.println(" try again in 5 seconds");
  213.       // Wait 5 seconds before retrying
  214.       delay(5000);
  215.     }
  216.   }
  217. }
  218.  
  219. void setup() {
  220.   pinMode(LED_BUILTIN, OUTPUT);     // on board led for mqtt status indication.
  221.   pinMode(D0, OUTPUT);
  222.   pinMode(D1, OUTPUT);
  223.   pinMode(D2, OUTPUT);
  224.   pinMode(D3, OUTPUT);
  225.   pinMode(D4, OUTPUT);
  226.   pinMode(D5, OUTPUT);
  227.   pinMode(D6, OUTPUT);
  228.   pinMode(D7, OUTPUT);
  229.   pinMode(D8, OUTPUT);
  230.   pinMode(RX, OUTPUT);
  231.   pinMode(TX, OUTPUT);
  232.  
  233.   Serial.begin(115200);
  234.   setup_wifi();
  235.   client.setServer(mqtt_server, 1883);
  236.   client.setCallback(callback);
  237.  
  238. }
  239.  
  240. void loop() {
  241.  
  242.   if (!client.connected()) {
  243.      digitalWrite(BUILTIN_LED, HIGH);
  244.     reconnect();
  245.   }
  246.   client.loop();
  247.  
  248.   long now=millis();
  249.   long now2=millis();
  250.  
  251.  
  252. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top