latinet:unicaes:workshops:communication-23
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
latinet:unicaes:workshops:communication-23 [2023/08/30 20:35] – jan.sonntag | latinet:unicaes:workshops:communication-23 [2023/10/22 22:19] – [3. Our first MQTT Publish] rolf.becker | ||
---|---|---|---|
Line 84: | Line 84: | ||
===== 3. Our first MQTT Publish ===== | ===== 3. Our first MQTT Publish ===== | ||
Stepping ahead, you'll have the chance to implement your first MQTT publish from the ESP8266. While the following example provides a static demonstration, | Stepping ahead, you'll have the chance to implement your first MQTT publish from the ESP8266. While the following example provides a static demonstration, | ||
+ | |||
+ | PubSubClient Library: [[https:// | ||
+ | |||
+ | <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 < | ||
+ | |||
+ | #define MSG_BUFFER_SIZE (50) | ||
+ | |||
+ | // 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; | ||
+ | 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 reconnect() { | ||
+ | // Loop until we're reconnected | ||
+ | while (!client.connected()) { | ||
+ | Serial.print(" | ||
+ | // Create a random client ID | ||
+ | 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, | ||
+ | Serial.begin(115200); | ||
+ | setup_wifi(); | ||
+ | client.setServer(mqtt_server, | ||
+ | } | ||
+ | |||
+ | 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 ===== | ===== 4. Subscribe ===== | ||
Line 109: | Line 217: | ||
// Update these with values suitable for your network and mqtt broker | // Update these with values suitable for your network and mqtt broker | ||
- | const char* ssid = "iotlab"; | + | const char* ssid = ""; |
- | const char* password = "iotlab18"; | + | const char* password = ""; |
const char* mqtt_server = " | const char* mqtt_server = " | ||
latinet/unicaes/workshops/communication-23.txt · Last modified: 2024/03/23 19:02 by 47.128.112.114