#include //#include #include #include #include const uint16_t HTTP_PORT = 80; // Use 443 for HTTPS and 80 for HTTP // Replace with your WiFi credentials const char* SSID = "iotlab-mobile"; // [USER INPUT] const char* PASSWORD = "iotlab18"; // [USER INPUT] // Create an instance of WiFiClientSecure WiFiClient wifi_client; // =================== // MQTT // =================== #define MQTT_MSG_BUFFER_SIZE 50 // [USER INPUT] StaticJsonDocument doc; const uint8_t PERIOD_MINUTES = 1; const char* MQTT_SERVER = "192.168.1.1"; // [USER INPUT] const uint16_t MQTT_PORT = 1883; // TLS TCP PORT for HTTPS connections, otherwise use 1883 const char* MQTT_OUTPUT_TOPIC = "colmayor/workshop/2024/harley"; // [USER INPUT] const char* MQTT_INPUT_TOPIC = "colmayor/workshop/2024/to-harley"; // [USER INPUT] PubSubClient mqtt_client(wifi_client); unsigned long lastMsg = 0; char msg[MQTT_MSG_BUFFER_SIZE]; // count the number of msg uint value = 0; void setup_wifi() { // Connect to Wi-Fi WiFi.begin(SSID, PASSWORD); Serial.print("\n\nConnecting to "); Serial.print(SSID); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println("\nConnected to WiFi"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Set the client to verify the server's certificate //wifi_client.setInsecure(); // Give the client a chance to perform the handshake delay(1000); } void callback(char* topic, byte* payload, unsigned int length) { // RAW payload Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); // JSON formated payload //deserializeJson(doc, (const byte*)payload, length); //serializeJson(doc, Serial); } void mqtt_reconnect() { while (!mqtt_client.connected()){ Serial.println("Attempting MQTT re-connection.."); // Create a random client ID String clientId = "ESP8266-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (mqtt_client.connect(clientId.c_str())) { Serial.println("Successful reconnection."); // Once connected, publish an announcement... //JsonDocument doc; doc["msg"] = "hello world"; doc["count"] = value; serializeJson(doc, msg); mqtt_client.publish(MQTT_OUTPUT_TOPIC, msg); mqtt_client.subscribe(MQTT_INPUT_TOPIC); } else { Serial.print("failed, rc="); Serial.print(mqtt_client.state()); Serial.println(" try again in 10 seconds"); // Wait before retrying delay(10000); } } } void setup() { Serial.begin(115200); setup_wifi(); // to generate a random client ID // during reconnection with the // mqtt server connection randomSeed(micros()); mqtt_client.setServer(MQTT_SERVER, MQTT_PORT); mqtt_client.setCallback(callback); } void loop() { if (!mqtt_client.connected()) { mqtt_reconnect(); } mqtt_client.loop(); unsigned long now = millis(); if (now - lastMsg > (PERIOD_MINUTES*(60*1000))) { lastMsg = now; ++value; // JsonDocument doc; doc["msg"] = "hello world"; doc["count"] = value; serializeJson(doc, msg); mqtt_client.publish(MQTT_OUTPUT_TOPIC, msg); } }