Advertisement
osromocon

MQTT_ESP

Aug 15th, 2022
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. /******************************************************************************
  2. MQTT_Light_Example.ino
  3. Example for controlling a light using MQTT
  4. by: Alex Wende, SparkFun Electronics
  5.  
  6. This sketch connects the ESP8266 to a MQTT broker and subcribes to the topic
  7. room/light. When "on" is recieved, the pin LIGHT_PIN is toggled HIGH.
  8. When "off" is recieved, the pin LIGHT_PIN is toggled LOW.
  9.  
  10.  
  11. Pobrane ze strony:
  12. https://learn.sparkfun.com/tutorials/introduction-to-mqtt/
  13. ******************************************************************************/
  14.  
  15. #include <ESP8266WiFi.h>
  16. #include <PubSubClient.h>
  17.  
  18. char *ssid =  "---";   // name of your WiFi network
  19. char *password =  "---"; // password of the WiFi network
  20.  
  21. // Home Assistant Credentials
  22. const char *HA_USER = "---";
  23. const char *HA_PASS = "---";
  24.  
  25. const byte LIGHT_PIN = 15;           // Pin to control the light with
  26. const byte SWITCH_PIN = 0;           // Pin to control the light with
  27. const char *ID = "Example_Light";  // Name of our device, must be unique
  28. const char *TOPIC = "room/light";  // Topic to subcribe to
  29. const char *STATE_TOPIC = "room/light/state";  // Topic to publish the light state to
  30.  
  31. IPAddress broker(192,168,1,164); // IP address of your MQTT broker eg. 192.168.1.50
  32. WiFiClient wclient;
  33.  
  34. PubSubClient client(wclient); // Setup MQTT client
  35. bool state=0;
  36.  
  37. // Handle incomming messages from the broker
  38. void callback(char* topic, byte* payload, unsigned int length) {
  39.   String response;
  40.  
  41.   for (int i = 0; i < length; i++) {
  42.     response += (char)payload[i];
  43.   }
  44.   Serial.print("Message arrived [");
  45.   Serial.print(topic);
  46.   Serial.print("] ");
  47.   Serial.println(response);
  48.   if(response == "on")  // Turn the light on
  49.   {
  50.     digitalWrite(LIGHT_PIN, HIGH);
  51.     client.publish(STATE_TOPIC,"on");
  52.   }
  53.   else if(response == "off")  // Turn the light off
  54.   {
  55.     digitalWrite(LIGHT_PIN, LOW);
  56.     client.publish(STATE_TOPIC,"off");
  57.   }
  58. }
  59.  
  60. // Connect to WiFi network
  61. void setup_wifi() {
  62.   Serial.print("\nConnecting to ");
  63.   Serial.println(ssid);
  64.  
  65.   WiFi.begin(ssid, password); // Connect to network
  66.  
  67.   while (WiFi.status() != WL_CONNECTED) { // Wait for connection
  68.     delay(500);
  69.     Serial.print(".");
  70.   }
  71.  
  72.   Serial.println();
  73.   Serial.println("WiFi connected");
  74.   Serial.print("IP address: ");
  75.   Serial.println(WiFi.localIP());
  76. }
  77.  
  78. // Reconnect to client
  79. void reconnect() {
  80.   // Loop until we're reconnected
  81.   while (!client.connected()) {
  82.     Serial.print("Attempting MQTT connection...");
  83.     // Attempt to connect
  84.     if (client.connect(ID,HA_USER,HA_PASS)) {
  85.       client.subscribe(TOPIC);
  86.       Serial.println("connected");
  87.       Serial.print("Subcribed to: ");
  88.       Serial.println(TOPIC);
  89.       Serial.println('\n');
  90.  
  91.     } else {
  92.       Serial.println(" try again in 5 seconds");
  93.       // Wait 5 seconds before retrying
  94.       delay(5000);
  95.     }
  96.   }
  97. }
  98.  
  99. void setup() {
  100.   Serial.begin(115200); // Start serial communication at 115200 baud
  101.   pinMode(LIGHT_PIN, OUTPUT); // Configure LIGHT_PIN as an output
  102.   pinMode(SWITCH_PIN,INPUT);  // Configure SWITCH_Pin as an input
  103.   pinMode(15, OUTPUT);
  104.   digitalWrite(SWITCH_PIN,HIGH);  // enable pull-up resistor (active low)
  105.   delay(100);
  106.   setup_wifi(); // Connect to network
  107.   client.setServer(broker, 1883);
  108.   client.setCallback(callback);// Initialize the callback routine
  109. }
  110.  
  111. void loop() {
  112.   if (!client.connected())  // Reconnect if connection is lost
  113.   {
  114.     reconnect();
  115.   }
  116.   client.loop();
  117.  
  118.  
  119.   // if the switch is being pressed
  120.   if(digitalRead(SWITCH_PIN) == 0)
  121.   {
  122.     state = !state; //toggle state
  123.     if(state == 1) // ON
  124.     {
  125.       client.publish(TOPIC, "on");
  126.       digitalWrite(15,HIGH);
  127.       Serial.println((String)TOPIC + " => on");
  128.      
  129.     }
  130.     else // OFF
  131.     {
  132.       client.publish(TOPIC, "off");
  133.       digitalWrite(15,LOW);      
  134.       Serial.println((String)TOPIC + " => off");
  135.     }
  136.  
  137.     while(digitalRead(SWITCH_PIN) == 0) // Wait for switch to be released
  138.     {
  139.       yield();
  140.       delay(20);
  141.     }
  142.   }
  143.  
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement