send msg mqqt nodemcu code example
Example: send msg mqqt nodemcu
/******************************************************************************MQTT_Switch_Example.inoExample for controlling a light using an MQTT switchby: Alex Wende, SparkFun ElectronicsThis sketch connects the ESP32 to a MQTT broker and subcribes to the topicroom/light. When the button is pressed, the client will toggle betweenpublishing "on" and "off".******************************************************************************/#include <WiFi.h>#include <PubSubClient.h>const char *ssid = "-----"; // name of your WiFi networkconst char *password = "-----"; // password of the WiFi networkconst byte SWITCH_PIN = 0; // Pin to control the light withconst char *ID = "Example_Switch"; // Name of our device, must be uniqueconst char *TOPIC = "room/light"; // Topic to subcribe toIPAddress broker(192,168,1,-); // IP address of your MQTT broker eg. 192.168.1.50WiFiClient wclient;PubSubClient client(wclient); // Setup MQTT clientbool state=0;// Connect to WiFi networkvoid 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 clientvoid reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(ID)) { Serial.println("connected"); Serial.print("Publishing 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(SWITCH_PIN,INPUT); // Configure SWITCH_Pin as an input digitalWrite(SWITCH_PIN,HIGH); // enable pull-up resistor (active low) delay(100); setup_wifi(); // Connect to network client.setServer(broker, 1883);}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"); Serial.println((String)TOPIC + " => on"); } else // OFF { client.publish(TOPIC, "off"); Serial.println((String)TOPIC + " => off"); } while(digitalRead(SWITCH_PIN) == 0) // Wait for switch to be released { // Let the ESP handle some behind the scenes stuff if it needs to yield(); delay(20); } }}