Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- MQTT_Light_Example.ino
- Example for controlling a light using MQTT
- by: Alex Wende, SparkFun Electronics
- This sketch connects the ESP8266 to a MQTT broker and subcribes to the topic
- room/light. When "on" is recieved, the pin LIGHT_PIN is toggled HIGH.
- When "off" is recieved, the pin LIGHT_PIN is toggled LOW.
- Pobrane ze strony:
- https://learn.sparkfun.com/tutorials/introduction-to-mqtt/
- ******************************************************************************/
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- char *ssid = "---"; // name of your WiFi network
- char *password = "---"; // password of the WiFi network
- // Home Assistant Credentials
- const char *HA_USER = "---";
- const char *HA_PASS = "---";
- const byte LIGHT_PIN = 15; // Pin to control the light with
- const byte SWITCH_PIN = 0; // Pin to control the light with
- const char *ID = "Example_Light"; // Name of our device, must be unique
- const char *TOPIC = "room/light"; // Topic to subcribe to
- const char *STATE_TOPIC = "room/light/state"; // Topic to publish the light state to
- IPAddress broker(192,168,1,164); // IP address of your MQTT broker eg. 192.168.1.50
- WiFiClient wclient;
- PubSubClient client(wclient); // Setup MQTT client
- bool state=0;
- // Handle incomming messages from the broker
- void callback(char* topic, byte* payload, unsigned int length) {
- String response;
- for (int i = 0; i < length; i++) {
- response += (char)payload[i];
- }
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- Serial.println(response);
- if(response == "on") // Turn the light on
- {
- digitalWrite(LIGHT_PIN, HIGH);
- client.publish(STATE_TOPIC,"on");
- }
- else if(response == "off") // Turn the light off
- {
- digitalWrite(LIGHT_PIN, LOW);
- client.publish(STATE_TOPIC,"off");
- }
- }
- // Connect to WiFi network
- void setup_wifi() {
- Serial.print("\nConnecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password); // Connect to network
- while (WiFi.status() != WL_CONNECTED) { // Wait for connection
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- // Reconnect to client
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(ID,HA_USER,HA_PASS)) {
- client.subscribe(TOPIC);
- Serial.println("connected");
- Serial.print("Subcribed to: ");
- Serial.println(TOPIC);
- Serial.println('\n');
- } else {
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- Serial.begin(115200); // Start serial communication at 115200 baud
- pinMode(LIGHT_PIN, OUTPUT); // Configure LIGHT_PIN as an output
- pinMode(SWITCH_PIN,INPUT); // Configure SWITCH_Pin as an input
- pinMode(15, OUTPUT);
- digitalWrite(SWITCH_PIN,HIGH); // enable pull-up resistor (active low)
- delay(100);
- setup_wifi(); // Connect to network
- client.setServer(broker, 1883);
- client.setCallback(callback);// Initialize the callback routine
- }
- void loop() {
- if (!client.connected()) // Reconnect if connection is lost
- {
- reconnect();
- }
- client.loop();
- // if the switch is being pressed
- if(digitalRead(SWITCH_PIN) == 0)
- {
- state = !state; //toggle state
- if(state == 1) // ON
- {
- client.publish(TOPIC, "on");
- digitalWrite(15,HIGH);
- Serial.println((String)TOPIC + " => on");
- }
- else // OFF
- {
- client.publish(TOPIC, "off");
- digitalWrite(15,LOW);
- Serial.println((String)TOPIC + " => off");
- }
- while(digitalRead(SWITCH_PIN) == 0) // Wait for switch to be released
- {
- yield();
- delay(20);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement