#include #include #include #include #include #include "certificate.h" #include "secrets.h" #define CERT mqtt_broker_cert #define MSG_BUFFER_SIZE (50) //-------------------------------------- // config (edit here before compiling) //-------------------------------------- // #define MQTT_TLS // uncomment this define to enable TLS transport // #define MQTT_TLS_VERIFY // uncomment this define to enable broker certificate verification const char* ssid = VITRINE_SSID; const char* password = VITRINE_WIFI_PASS; const char* mqtt_server = "mqtt.klank.school"; // eg. your-demo.cedalo.cloud or 192.168.1.11 const uint16_t mqtt_server_port = 7000; // or 8883 most common for tls transport const char* mqttUser = MQTT_ARDUINO_USERNAME; const char* mqttPassword = MQTT_ARDUINO_PASS; const char* mqttTopicIn = "esp-8266-in"; const char* mqttTopicOut = "esp-8266-out"; const char* rootCACertificate = R"( -----BEGIN CERTIFICATE----- MIIDvTCCAqWgAwIBAgIUIjMIFwrRWBr9z0lTWa7CQ0GcKV0wDQYJKoZIhvcNAQEL BQAwdDELMAkGA1UEBhMCTkwxFTATBgNVBAgMDFp1aWQtSG9sbGFuZDESMBAGA1UE BwwJUm90dGVyZGFtMRQwEgYDVQQKDAtLbGFua3NjaG9vbDENMAsGA1UECwwETVFU VDEVMBMGA1UEAwwMa2xhbmsuc2Nob29sMB4XDTI0MTAyODExMjUwMFoXDTI1MTAy ODExMjUwMFoweTELMAkGA1UEBhMCTkwxFTATBgNVBAgMDFp1aWQtSG9sbGFuZDES MBAGA1UEBwwJUm90dGVyZGFtMRQwEgYDVQQKDAtLbGFua3NjaG9vbDENMAsGA1UE CwwETVFUVDEaMBgGA1UEAwwRbXF0dC5rbGFuay5zY2hvb2wwggEiMA0GCSqGSIb3 DQEBAQUAA4IBDwAwggEKAoIBAQDHiV7tLQdZg9qztSXYujw1dqL9lpqy5nDCkm8/ Sd9wOh6SBMm65LvcJb3YBpoPfI6hwwOI+XcWsGiTTuOiCM3EemVYXkcc0VCjbgzn FZ3Ld4s1jpXy+EgDaNhXXfFpREzStDOcRrmnIm5iHRiKdWAWeJTmYas915jzTCrk ibhW9Qd9WZstxdccpjBWnB4X6uSMOeGdunAifTY8vVLQtaBlAqyoPbZk1ELbqKaz zBso34euvKoPCPs8nkFR/RpLNStuOTVC8g+uCQ8nIgNlXpftvjMHvBsoq7ZdICRI jx9uW4U0y2kEF7d3P9eLl07uJjxHhurB/J/kJHm5qc4tVcOXAgMBAAGjQjBAMB0G A1UdDgQWBBRjQ4b5y5mEeidokSMz0pRetgNuRTAfBgNVHSMEGDAWgBR0e8d1l8aw EGSdl1/vBUgQcmlV7jANBgkqhkiG9w0BAQsFAAOCAQEAaK8rrSeqLJIAr3v+GAdJ yGqWK9qpJgXEbxYiuIj0pSejpDX3609qvyy7YEDUqLTu44ulZmrgGfrWAXg6Boms CpBGybNNd7oYhcGJdSCURt2EgjkEP0eCVXo02l4JDrnaNvsBt9OhEVvNSFRxblyE UmUTWeMh27T1/ioHeQpLIBf2BPOJZrMGeQGNfYujbtd8F4VQmGcOcfGsPhbvsaFR dUut/VtKBnifKmZ/6afbmHHBUW6IjxTXBm8J0PrY9H5SR/Y7hEvbshlETFO5djMZ 6mmitx2Bxd2s9/nPWPXk5YZryCvWvEHof23dE81OfVLaGYGdzL4ys5UiEWd/X8jP 1Q== -----END CERTIFICATE----- )"; //-------------------------------------- // globals //-------------------------------------- #ifdef MQTT_TLS WiFiClientSecure wifiClient; #else WiFiClient wifiClient; #endif WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); PubSubClient mqttClient(wifiClient); //-------------------------------------- // function setup_wifi called once //-------------------------------------- void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } timeClient.begin(); #ifdef MQTT_TLS #ifdef MQTT_TLS_VERIFY // X509List *cert = new X509List(CERT); // wifiClient.setTrustAnchors(cert); wifiClient.setTrustAnchors(new BearSSL::X509List(rootCACertificate)); #else wifiClient.setInsecure(); #endif #endif Serial.println("WiFi connected"); } //-------------------------------------- // function callback called everytime // if a mqtt message arrives from the broker //-------------------------------------- void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived on topic: '"); Serial.print(topic); Serial.print("' with payload: "); for (unsigned int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); String myCurrentTime = timeClient.getFormattedTime(); mqttClient.publish(mqttTopicOut,("ESP8266: Cedalo Mosquitto is awesome. ESP8266-Time: " + myCurrentTime).c_str()); } //-------------------------------------- // function connect called to (re)connect // to the broker //-------------------------------------- void connect() { while (!mqttClient.connected()) { // Serial.println("The last error was"); // Serial.println(wifiClient.getLastSSLError(NULL)); Serial.print("Attempting MQTT connection..."); String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); if (mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) { Serial.println("connected"); mqttClient.subscribe(mqttTopicIn); } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" will try again in 5 seconds"); delay(5000); } } } //-------------------------------------- // main arduino setup fuction called once //-------------------------------------- void setup() { Serial.begin(115200); setup_wifi(); mqttClient.setServer(mqtt_server, mqtt_server_port); mqttClient.setCallback(callback); } //-------------------------------------- // main arduino loop fuction called periodically //-------------------------------------- void loop() { if (!mqttClient.connected()) { connect(); } mqttClient.loop(); timeClient.update(); }