Created
April 7, 2019 19:59
-
-
Save ultimateprogramer/9d1aabc99c5202db5513d00749db8596 to your computer and use it in GitHub Desktop.
Communicate between 2 NodeMCUs using MQTT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ESP8266WiFi.h> | |
| #include <PubSubClient.h> | |
| const char* ssid = "xxx"; | |
| const char* password = "xxx"; | |
| const char* mqttServer = "m24.cloudmqtt.com"; | |
| const int mqttPort = 15031; | |
| const char* mqttUser = "xxx"; | |
| const char* mqttPassword = "xxx"; | |
| int ledPin = 13; // GPIO13 | |
| bool isOn = false; | |
| WiFiClient espClient; | |
| PubSubClient client(espClient); | |
| void setup() { | |
| Serial.begin(9600); | |
| delay(10); | |
| pinMode(ledPin, OUTPUT); | |
| digitalWrite(ledPin, LOW); | |
| // Connect to WiFi network | |
| Serial.println(); | |
| Serial.println(); | |
| Serial.print("Connecting to "); | |
| Serial.println(ssid); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| } | |
| Serial.println(""); | |
| Serial.println("WiFi connected"); | |
| client.setServer(mqttServer, mqttPort); | |
| client.setCallback(callback); | |
| while (!client.connected()) { | |
| Serial.println("Connecting to MQTT..."); | |
| if (client.connect("ESP8266ClientB", mqttUser, mqttPassword )) { | |
| Serial.println("connected"); | |
| } else { | |
| Serial.print("failed with state "); | |
| Serial.print(client.state()); | |
| delay(2000); | |
| } | |
| } | |
| client.subscribe("esp/blinker"); | |
| } | |
| void callback(char* topic, byte* payload, unsigned int length) { | |
| Serial.println(topic); | |
| if(strcmp(topic, "esp/blinker") == 0) { | |
| // blink message | |
| char message[50]; | |
| sprintf(message, "esp/blinker sent a message: %s", (char *)payload); | |
| Serial.println(message); | |
| } | |
| } | |
| void loop() { | |
| client.loop(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ESP8266WiFi.h> | |
| #include <PubSubClient.h> | |
| const char* ssid = "xxx"; | |
| const char* password = "xxx"; | |
| const char* mqttServer = "m24.cloudmqtt.com"; | |
| const int mqttPort = 15031; | |
| const char* mqttUser = "xxx"; | |
| const char* mqttPassword = "xxx"; | |
| int ledPin = 13; // GPIO13 | |
| bool isOn = false; | |
| WiFiClient espClient; | |
| PubSubClient client(espClient); | |
| void setup() { | |
| Serial.begin(9600); | |
| delay(10); | |
| pinMode(ledPin, OUTPUT); | |
| digitalWrite(ledPin, LOW); | |
| // Connect to WiFi network | |
| Serial.println(); | |
| Serial.println(); | |
| Serial.print("Connecting to "); | |
| Serial.println(ssid); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| } | |
| Serial.println(""); | |
| Serial.println("WiFi connected"); | |
| client.setServer(mqttServer, mqttPort); | |
| while (!client.connected()) { | |
| Serial.println("Connecting to MQTT..."); | |
| if (client.connect("ESP8266ClientA", mqttUser, mqttPassword )) { | |
| Serial.println("connected"); | |
| } else { | |
| Serial.print("failed with state "); | |
| Serial.print(client.state()); | |
| delay(2000); | |
| } | |
| } | |
| client.publish("esp/test", "Hello from ESP32"); | |
| } | |
| void loop() { | |
| client.publish("esp/blinker", isOn ? "On" : "Off"); | |
| isOn = !isOn; | |
| delay(1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment