#include <WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
// Wi-Fi settings
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT broker settings
const char* mqttServer = "ucg.cognosfera.net";
const int mqttPort = 1993; // MQTT over SSL/TLS default port
// MQTT authentication
const char* mqttUsername = "ronel.nevarez";
const char* mqttPassword = "casagrande.edu.ec";
// Sensor pin
const int sensorPin = A0;
// Create an instance of WiFiClientSecure
WiFiClientSecure wifiClient;
// Create an instance of PubSubClient
PubSubClient mqttClient(wifiClient);
// Root CA certificate
const char* rootCACertificate = R"(
-----BEGIN CERTIFICATE-----
MIIDxzCCAq+gAwIBAgIUCfMUIq9tai5p1v51sLyTEpxGB2wwDQYJKoZIhvcNAQEL
BQAwczELMAkGA1UEBhMCRUMxEjAQBgNVBAgMCUd1YXlhcXVpbDESMBAGA1UEBwwJ
R3VheWFxdWlsMQwwCgYDVQQKDANVQ0cxHTAbBgNVBAsMFE1hc3RlckNpYmVyc2Vn
dXJpZGFkMQ8wDQYDVQQDDAZDQS1VQ0cwHhcNMjQwNjA5MTY0NTQ3WhcNMjUwNjA5
MTY0NTQ3WjBzMQswCQYDVQQGEwJFQzESMBAGA1UECAwJR3VheWFxdWlsMRIwEAYD
VQQHDAlHdWF5YXF1aWwxDDAKBgNVBAoMA1VDRzEdMBsGA1UECwwUTWFzdGVyQ2li
ZXJzZWd1cmlkYWQxDzANBgNVBAMMBkNBLVVDRzCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBAK424pKN8VVcZ+lkfuOPdr+gFgyoXQzkyjX98SK3t3txRmZi
RI4F84+foGyGIn+LhSx/2vx/LP397zfGOX1xKmeVW6m1/4RnGDg/7P3Dwek1tDbe
lLaK/T408UmqKo2jt6kbKrfVTtncsZLs8onwvrDVIE+2k9VNii3zf0kGmHmEP7VX
OoR0v76NstY4qnt6LqTmlSBdYx14z4bkaIGkQoNWLTNr1aVhod4L7vXRnsJEzqmX
AcP2YuVh3Nfv0neIER8szgRzs0OmIFg/Wh+rlxwsbtD9ZB7dUAPRTgeh2bXntEIW
bL72dI6v5Sk4DwX1QL+SBjN5KLpBhF12tK6KYCcCAwEAAaNTMFEwHQYDVR0OBBYE
FJuZXev0DWqsjeEtrwdM4W9X61hJMB8GA1UdIwQYMBaAFJuZXev0DWqsjeEtrwdM
4W9X61hJMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAAe+aXoN
X8dFYfc3s9BqCvBJKT2blmH9BCSgcD1AmZ+cWAYS4iNQq1bt+xcycoaD4o4+bl+e
vwjJnKW06JcyJuAXWaGUYPvpCAuecWKP1zp1242qIQOMAPoXAzTGbfbKmjMead8j
2DmuI+8gNqQcsLZvGGwWF1EfG6S+6RoLt2SA5ZJXCOatPhDqEBoL9yTloJBG0NAf
m/+QuHc69CALks1WsMRhahG5xeezt/zgaF1x+oNMEtSrVIC2HqLcPUAVIOrADWdh
sTQQlS9AyfWmneBo4Fd/yRL8iehVt/oXQPDmGTnxsA8X1fCSTo1tFtvQD0CUVDcn
JW1rmY1NFOBKsH4=
-----END CERTIFICATE-----
)";
void setup() {
Serial.begin(9600);
delay(10);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Configure MQTT server and set SSL/TLS options
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
// Load root CA certificate into WiFiClientSecure object
//wifiClient.setTrustAnchors(new BearSSL::X509List(rootCACertificate));
wifiClient.setCACert(rootCACertificate);
wifiClient.setInsecure();
// Connect to MQTT broker
connectToMqtt();
}
void loop() {
if (!mqttClient.connected()) {
// If MQTT connection is lost, try to reconnect
reconnect();
}
// Read sensor data
int sensorValue = 22;
// Convert sensor value to a string
String payload = String(sensorValue);
// Publish the sensor data to the MQTT topic
mqttClient.publish("sensor_data_topic", payload.c_str());
// Wait for some time before publishing the next sensor data
delay(5000);
}
void connectToMqtt() {
// Loop until connected to MQTT broker
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT server...");
if (mqttClient.connect("ESP8266Client", mqttUsername, mqttPassword)) {
Serial.println("Connected to MQTT server!");
// Subscribe to MQTT topics, if needed
// mqttClient.subscribe("topic_name");
} else {
Serial.print("Failed to connect to MQTT server. Retrying in 5 seconds...");
delay(5000);
}
}
}
void reconnect() {
// Disconnect if already connected
if (mqttClient.connected()) {
mqttClient.disconnect();
}
// Attempt to reconnect
connectToMqtt();
}
void callback(char* topic, byte* payload, unsigned int length) {
// Handle MQTT subscription messages, if needed
// ...
}