c4ta:iot-workshop:mqtt
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
c4ta:iot-workshop:mqtt [2024/09/11 00:38] – created harley.lara | c4ta:iot-workshop:mqtt [2024/09/13 18:10] (current) – [3. Our first MQTT Publish] harley.lara | ||
---|---|---|---|
Line 88: | Line 88: | ||
<file c++ mqtt-publish.ino> | <file c++ mqtt-publish.ino> | ||
- | /* | ||
- | Basic ESP8266 MQTT example | ||
- | This sketch demonstrates the capabilities of the pubsub library in combination | ||
- | with the ESP8266 board/ | ||
- | It connects to an MQTT server then: | ||
- | - publishes "hello world" to the topic defined as outTopic every two seconds | ||
- | It will reconnect to the server if the connection is lost using a blocking | ||
- | | ||
- | | ||
- | */ | ||
- | |||
#include < | #include < | ||
+ | //#include < | ||
+ | #include < | ||
#include < | #include < | ||
+ | #include < | ||
+ | |||
+ | const uint16_t HTTP_PORT = 80; // Use 443 for HTTPS and 80 for HTTP | ||
- | #define MSG_BUFFER_SIZE (50) | + | // Replace with your WiFi credentials |
+ | const char* SSID = " | ||
+ | const char* PASSWORD = " | ||
- | // Update these with values suitable for your network and mqtt broker | + | // Create an instance of WiFiClientSecure |
+ | WiFiClient wifi_client; | ||
- | const char* ssid = ""; | + | // =================== |
- | const char* password | + | // MQTT |
- | const char* mqtt_server | + | // =================== |
- | const char* outTopic = " | + | #define MQTT_MSG_BUFFER_SIZE 50 // [USER INPUT] |
- | const char* inTopic = " | + | StaticJsonDocument< |
- | // Set up of some needed variables | + | const uint8_t PERIOD_MINUTES = 1; |
- | WiFiClient espClient; | + | const char* MQTT_SERVER = " |
- | PubSubClient | + | const uint16_t MQTT_PORT = 1883; // TLS TCP PORT for HTTPS connections, |
+ | const char* MQTT_OUTPUT_TOPIC = " | ||
+ | const char* MQTT_INPUT_TOPIC = " | ||
+ | |||
+ | PubSubClient | ||
unsigned long lastMsg = 0; | unsigned long lastMsg = 0; | ||
- | char msg[MSG_BUFFER_SIZE]; | + | char msg[MQTT_MSG_BUFFER_SIZE]; |
- | int value = 0; | + | |
- | void setup_wifi() { | + | // count the number of msg |
+ | uint value = 0; | ||
- | delay(10); | + | void setup_wifi() { |
- | // We start by connecting | + | // Connect |
- | | + | |
- | Serial.print(" | + | |
- | Serial.println(ssid); | + | |
- | | + | |
- | | + | |
while (WiFi.status() != WL_CONNECTED) { | while (WiFi.status() != WL_CONNECTED) { | ||
- | delay(500); | + | delay(1000); |
Serial.print(" | Serial.print(" | ||
} | } | ||
- | | + | Serial.println(" |
- | + | Serial.print("IP address: "); | |
- | Serial.println("" | + | |
- | | + | |
- | Serial.println("IP address: "); | + | |
Serial.println(WiFi.localIP()); | Serial.println(WiFi.localIP()); | ||
- | } | ||
- | void reconnect() { | + | |
- | | + | //wifi_client.setInsecure(); |
- | | + | |
- | Serial.print(" | + | |
- | | + | |
- | String clientId = " | + | |
- | clientId += String(random(0xffff), | + | |
- | // Attempt to connect | + | |
- | if (client.connect(clientId.c_str())) { | + | |
- | Serial.println(" | + | |
- | // Once connected, publish an announcement... | + | |
- | client.publish(outTopic, | + | |
- | } else { | + | |
- | Serial.print(" | + | |
- | Serial.print(client.state()); | + | |
- | Serial.println(" | + | |
- | // Wait 5 seconds before retrying | + | |
- | delay(5000); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | void setup() { | + | |
- | pinMode(BUILTIN_LED, | + | delay(1000); |
- | Serial.begin(115200); | + | |
- | setup_wifi(); | + | |
- | | + | |
- | } | + | |
- | + | ||
- | void loop() { | + | |
- | + | ||
- | if (!client.connected()) { | + | |
- | reconnect(); | + | |
- | } | + | |
- | client.loop(); | + | |
- | + | ||
- | unsigned long now = millis(); | + | |
- | if (now - lastMsg > 2000) { | + | |
- | lastMsg = now; | + | |
- | ++value; | + | |
- | snprintf (msg, MSG_BUFFER_SIZE, | + | |
- | Serial.print(" | + | |
- | Serial.println(msg); | + | |
- | client.publish(outTopic, | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== 4. Subscribe ===== | + | |
- | Now, let's move on to an essential aspect of our workshop. We'll guide you through the process of subscribing to an MQTT topic. This step is a practical gateway | + | |
- | + | ||
- | <file c++ mqtt-subscribe-example.ino> | + | |
- | /* | + | |
- | Basic ESP8266 MQTT example | + | |
- | This sketch demonstrates | + | |
- | with the ESP8266 board/ | + | |
- | It connects to an MQTT server then: | + | |
- | - publishes "hello world" to the topic defined as outTopic every two seconds | + | |
- | - subscribes to the topic defined as inTopic, printing out any messages | + | |
- | it receives. NB - it assumes the received payloads are strings not binary | + | |
- | - If the first character of the topic defined as inTopic is an 1, switch ON the ESP Led, | + | |
- | else switch it off | + | |
- | It will reconnect to the server if the connection is lost using a blocking | + | |
- | | + | |
- | | + | |
- | */ | + | |
- | + | ||
- | #include < | + | |
- | #include < | + | |
- | + | ||
- | // Update these with values suitable for your network and mqtt broker | + | |
- | + | ||
- | const char* ssid = ""; | + | |
- | const char* password = ""; | + | |
- | const char* mqtt_server = " | + | |
- | + | ||
- | const char* outTopic = " | + | |
- | const char* inTopic = " | + | |
- | + | ||
- | // Set up of some needed variables | + | |
- | + | ||
- | WiFiClient espClient; | + | |
- | PubSubClient client(espClient); | + | |
- | unsigned long lastMsg = 0; | + | |
- | #define MSG_BUFFER_SIZE (50) | + | |
- | char msg[MSG_BUFFER_SIZE]; | + | |
- | int value = 0; | + | |
- | + | ||
- | void setup_wifi() { | + | |
- | + | ||
- | delay(10); | + | |
- | // We start by connecting to a WiFi network | + | |
- | Serial.println(); | + | |
- | Serial.print(" | + | |
- | Serial.println(ssid); | + | |
- | + | ||
- | WiFi.mode(WIFI_STA); | + | |
- | WiFi.begin(ssid, | + | |
- | + | ||
- | while (WiFi.status() != WL_CONNECTED) { | + | |
- | delay(500); | + | |
- | Serial.print(" | + | |
- | } | + | |
- | + | ||
- | randomSeed(micros()); | + | |
- | + | ||
- | Serial.println("" | + | |
- | Serial.println(" | + | |
- | Serial.println(" | + | |
- | Serial.println(WiFi.localIP()); | + | |
} | } | ||
void callback(char* topic, byte* payload, unsigned int length) { | void callback(char* topic, byte* payload, unsigned int length) { | ||
+ | // RAW payload | ||
Serial.print(" | Serial.print(" | ||
Serial.print(topic); | Serial.print(topic); | ||
Line 266: | Line 157: | ||
Serial.println(); | Serial.println(); | ||
- | // Switch on the LED if an 1 was received as first character | + | // JSON formated payload |
- | | + | |
- | digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level | + | //serializeJson(doc, Serial); |
- | // but actually the LED is on; this is because | + | |
- | // it is active low on the ESP-01) | + | |
- | } else { | + | |
- | digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH | + | |
- | } | + | |
} | } | ||
- | void reconnect() { | + | void mqtt_reconnect() { |
- | // Loop until we're reconnected | + | while (!mqtt_client.connected()){ |
- | while (!client.connected()) { | + | Serial.println(" |
- | Serial.print(" | + | |
// Create a random client ID | // Create a random client ID | ||
- | String clientId = "ESP8266Client-"; | + | String clientId = "ESP8266-"; |
clientId += String(random(0xffff), | clientId += String(random(0xffff), | ||
+ | |||
// Attempt to connect | // Attempt to connect | ||
- | if (client.connect(clientId.c_str())) { | + | if (mqtt_client.connect(clientId.c_str())) { |
- | Serial.println(" | + | Serial.println(" |
// Once connected, publish an announcement... | // Once connected, publish an announcement... | ||
- | | + | |
- | | + | doc[" |
- | | + | doc[" |
+ | serializeJson(doc, | ||
+ | | ||
+ | | ||
} else { | } else { | ||
Serial.print(" | Serial.print(" | ||
- | Serial.print(client.state()); | + | Serial.print(mqtt_client.state()); |
- | Serial.println(" | + | Serial.println(" |
- | // Wait 5 seconds | + | // Wait before retrying |
- | delay(5000); | + | delay(10000); |
} | } | ||
} | } | ||
Line 302: | Line 191: | ||
void setup() { | void setup() { | ||
- | pinMode(BUILTIN_LED, | ||
Serial.begin(115200); | Serial.begin(115200); | ||
+ | |||
setup_wifi(); | setup_wifi(); | ||
- | | + | |
- | | + | // to generate a random |
+ | // during reconnection with the | ||
+ | // mqtt server connection | ||
+ | randomSeed(micros()); | ||
+ | mqtt_client.setServer(MQTT_SERVER, MQTT_PORT); | ||
+ | | ||
} | } | ||
void loop() { | void loop() { | ||
- | + | | |
- | | + | |
- | | + | |
} | } | ||
- | client.loop(); | + | |
+ | mqtt_client.loop(); | ||
unsigned long now = millis(); | unsigned long now = millis(); | ||
- | | + | |
+ | | ||
lastMsg = now; | lastMsg = now; | ||
++value; | ++value; | ||
- | | + | |
- | | + | doc["msg"] = "hello world"; |
- | | + | |
- | | + | |
+ | | ||
} | } | ||
- | } | ||
+ | |||
+ | } | ||
</ | </ | ||
+ | |||
+ | ===== 4. Subscribe ===== | ||
+ | Now, let's move on to an essential aspect of our workshop. We'll guide you through the process of subscribing to an MQTT topic. This step is a practical gateway to interactive IoT applications. You'll learn how to exchange MQTT messages between devices, potentially triggering actions like LED reactions or serial outputs. While we'll provide initial guidance, feel free to explore further and experiment with this dynamic feature. | ||
+ | |||
+ | TODO. | ||
===== Recording ===== | ===== Recording ===== |
c4ta/iot-workshop/mqtt.1726007892.txt.gz · Last modified: 2024/09/11 00:38 by harley.lara