include MQTT

This commit is contained in:
vitrinekast 2024-10-26 11:56:48 +02:00
parent d351dabc3c
commit 2130084d84

111
arduino/MQTT/MQTT.ino Normal file
View File

@ -0,0 +1,111 @@
// Board: WiFiduino
#include <ArduinoMqttClient.h>
// #include <WiFiNINA.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
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();
}