Last active
January 3, 2021 08:24
-
-
Save 6LYTH3/48bd1f7f7422da7698bb250ac0e7763d to your computer and use it in GitHub Desktop.
Connect MQTT with NodeMCU 8266
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 = "ssid_name"; | |
const char* password = "ssid_password"; | |
const char* mqtt_server = "broker.emqx.io"; | |
const char mqtt_username[50] = ""; | |
const char mqtt_password[50] = ""; | |
const int mqtt_port = 1883; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
char msg[10]; // 0 off, 1 on | |
int value = 0; | |
int cnt = 0; | |
void setup_wifi() { | |
delay(10); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
randomSeed(micros()); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
if ((char)payload[0] == '1') { | |
digitalWrite(BUILTIN_LED, LOW); | |
value = 1; | |
} else { | |
value = 0; | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection…"); | |
String clientId = "ESP8266Client - "; | |
clientId += String(random(0xffff), HEX); | |
// client.connect(clientId.c_str(), mqtt_username, mqtt_password) | |
if (client.connect(clientId.c_str())) { | |
Serial.println("connected"); | |
client.publish("mychn1234/in", "Start Programe"); | |
client.subscribe("mychn1234/out"); | |
} else { | |
Serial.print("failed, rc = "); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
pinMode(BUILTIN_LED, OUTPUT); | |
digitalWrite(BUILTIN_LED, HIGH); | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
} | |
void loop() { | |
delay(5000); | |
if (!client.connected()) { | |
Serial.println("Cannot connect to MQTT"); | |
reconnect(); | |
delay(5000); | |
} | |
client.loop(); | |
++cnt; | |
if (cnt % 10 == 0) { | |
value = 0; | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
Serial.print("Value from LOOP"); | |
Serial.println(value); | |
snprintf (msg, 2, "%ld", value); | |
Serial.print("Publish message: "); | |
Serial.println(msg); | |
client.publish("mychn1234/in", msg); | |
} |
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
// สำหรับส่งสัญญาณจากการ detect ระดับน้ำจากแทงค์ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#define D1 5 | |
#define button D1 | |
const char* ssid = "ssid_name"; | |
const char* password = "password"; | |
const char* mqtt_server = "broker.emqx.io"; | |
const char mqtt_username[50] = ""; | |
const char mqtt_password[50] = ""; | |
const int mqtt_port = 1883; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
char msg[10]; // 0 off, 1 on | |
int value = 0; | |
int cnt = 0; | |
void setup_wifi() { | |
delay(10); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
randomSeed(micros()); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
if ((char)payload[0] == '1') { | |
digitalWrite(BUILTIN_LED, LOW); | |
value = 1; | |
} else { | |
value = 0; | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection…"); | |
String clientId = "ESP8266Client - "; | |
clientId += String(random(0xffff), HEX); | |
// client.connect(clientId.c_str(), mqtt_username, mqtt_password | |
if (client.connect(clientId.c_str())) { //ทำการ Connect โดยใช้ Username&Password | |
Serial.println("connected"); | |
client.publish("mychn77882/out", "hello world from 2"); | |
} else { | |
Serial.print("failed, rc = "); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
pinMode(button, INPUT_PULLUP); | |
pinMode(BUILTIN_LED, OUTPUT); | |
digitalWrite(BUILTIN_LED, HIGH); | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
} | |
void loop() { | |
delay(5000); | |
if (!client.connected()) { | |
Serial.println("Cannot connect to MQTT"); | |
reconnect(); | |
delay(5000); | |
} | |
client.loop(); | |
bool readSwitch = digitalRead(button); | |
value = 0; | |
if (readSwitch == LOW) { | |
value = 1; | |
Serial.println("Pressed Switch"); | |
delay(500); | |
} | |
Serial.print("Value from LOOP"); | |
Serial.println(value); | |
snprintf (msg, 2, "%ld", value); | |
Serial.print("Publish message: "); | |
Serial.println(msg); | |
client.publish("mychn77882/out", msg); | |
} |
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
// สำหรับต่อกับ switch ใช้สั่งงาน on / off | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
const char* ssid = "ssid_name"; | |
const char* password = "password"; | |
const char* mqtt_server = "broker.emqx.io"; | |
const char mqtt_username[50] = ""; | |
const char mqtt_password[50] = ""; | |
const int mqtt_port = 1883; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
char msg[10]; // 0 off, 1 on | |
int value = 0; | |
int cnt = 0; | |
void setup_wifi() { | |
delay(10); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
randomSeed(micros()); | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
if ((char)payload[0] == '1') { | |
digitalWrite(BUILTIN_LED, LOW); | |
value = 1; | |
} else { | |
value = 0; | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection…"); | |
String clientId = "ESP8266Client - "; | |
clientId += String(random(0xffff), HEX); | |
// client.connect(clientId.c_str(), mqtt_username, mqtt_password | |
if (client.connect(clientId.c_str())) { //ทำการ Connect โดยใช้ Username&Password | |
Serial.println("connected"); | |
client.subscribe("mychn77882/out"); | |
} else { | |
Serial.print("failed, rc = "); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
pinMode(BUILTIN_LED, OUTPUT); | |
digitalWrite(BUILTIN_LED, HIGH); | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, mqtt_port); | |
client.setCallback(callback); | |
} | |
void loop() { | |
delay(5000); | |
if (!client.connected()) { | |
Serial.println("Cannot connect to MQTT"); | |
reconnect(); | |
delay(5000); | |
} | |
client.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment