From 2130084d843cbcaa7238390b132429962b8d9257 Mon Sep 17 00:00:00 2001 From: vitrinekast Date: Sat, 26 Oct 2024 11:56:48 +0200 Subject: [PATCH] include MQTT --- arduino/MQTT/MQTT.ino | 111 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 arduino/MQTT/MQTT.ino diff --git a/arduino/MQTT/MQTT.ino b/arduino/MQTT/MQTT.ino new file mode 100644 index 0000000..8ae83c3 --- /dev/null +++ b/arduino/MQTT/MQTT.ino @@ -0,0 +1,111 @@ +// Board: WiFiduino + +#include +// #include +#include +#include + +char ssid[] = "xxx"; // your network SSID +char password[] = "xxx"; // your network password + +WiFiClient espClient; +MqttClient mqttClient(espClient); +PubSubClient mqtt_client(espClient); + + +const char broker[] = "mqtt.klank.school"; +int port = 8883; +const char topic[] = "real_unique_topic"; +const char topic2[] = "real_unique_topic_2"; +const char topic3[] = "real_unique_topic_3"; +const char *mqtt_topic = "emqx/esp8266/led"; // +const char *mqtt_username = "xxx"; +const char *mqtt_password = "xxx"; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + // attempt to connect to Wifi network: + Serial.print("Attempting to connect to SSID: "); + // WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.println(". stil not"); + } + + Serial.println("You're connected to the network"); + Serial.println(); + + Serial.print("Attempting to connect to the MQTT broker: "); + Serial.println(broker); + + while (!mqtt_client.connected()) { + String client_id = "esp8266-client-" + String(WiFi.macAddress()); + Serial.printf("Connecting to MQTT Broker as %s.....\n", client_id.c_str()); + if (mqtt_client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { + Serial.println("Connected to MQTT broker"); + mqtt_client.subscribe(mqtt_topic); + // Publish message upon successful connection + mqtt_client.publish(mqtt_topic, "Hi EMQX I'm ESP8266 ^^"); + } else { + Serial.print("Failed to connect to MQTT broker, rc="); + Serial.print(mqtt_client.state()); + Serial.println(" try again in 5 seconds"); + delay(5000); + } + } + + Serial.println("You're connected to the MQTT broker!"); + Serial.println(); + + // set the message receive callback + mqttClient.onMessage(onMqttMessage); + + Serial.print("Subscribing to topic: "); + Serial.println(topic); + Serial.println(); + + // subscribe to a topic + mqttClient.subscribe(topic); + mqttClient.subscribe(topic2); + mqttClient.subscribe(topic3); + + // topics can be unsubscribed using: + // mqttClient.unsubscribe(topic); + + Serial.print("Topic: "); + Serial.println(topic); + Serial.print("Topic: "); + Serial.println(topic2); + Serial.print("Topic: "); + Serial.println(topic3); + + Serial.println(); +} + +void loop() { + // call poll() regularly to allow the library to receive MQTT messages and + // send MQTT keep alive which avoids being disconnected by the broker + mqttClient.poll(); +} + +void onMqttMessage(int messageSize) { + // we received a message, print out the topic and contents + Serial.println("Received a message with topic '"); + Serial.print(mqttClient.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + + // use the Stream interface to print the contents + while (mqttClient.available()) { + Serial.print((char)mqttClient.read()); + } + Serial.println(); + Serial.println(); +} \ No newline at end of file