You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Initialize LittleFS
if (!LittleFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Load CA certificate from LittleFS
File ca = LittleFS.open("/data/certs.ar", "r"); // Adjust path to match your setup
if (!ca) {
Serial.println("Failed to open CA file");
return;
}
if (!espClient.loadCACert(ca)) {
Serial.println("Failed to load CA certificate");
}
ca.close();
// Set secure connection for MQTT
espClient.setInsecure();
client.setServer(mqtt_server, mqtt_port);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
// Generate a unique client ID
String clientId = "ESP8266Client-" + String(millis());
Error Message:"failed rc=-2"
Code:
#include <DHT_U.h> // DHT library which uses some functions from Adafruit Sensor library
#include <ESP8266WiFi.h> // Library for using ESP8266 WiFi
#include <WiFiClientSecure.h> // Library for secure WiFi connections
#include <PubSubClient.h> // MQTT library
#include <LittleFS.h> // LittleFS library for ESP8266
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// WiFi credentials
const char* ssid = "myWifiSSID";
const char* password = "myWifiCode";
// MQTT broker information
const char* mqtt_server = "myHiveQ.cloud";
const int mqtt_port = 8883;
const char* mqtt_username = "devwithmnasir2";
const char* mqtt_password = "Devwithmnasir123@";
WiFiClientSecure espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
dht.begin();
// Initialize LittleFS
if (!LittleFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Load CA certificate from LittleFS
File ca = LittleFS.open("/data/certs.ar", "r"); // Adjust path to match your setup
if (!ca) {
Serial.println("Failed to open CA file");
return;
}
if (!espClient.loadCACert(ca)) {
Serial.println("Failed to load CA certificate");
}
ca.close();
// Set secure connection for MQTT
espClient.setInsecure();
client.setServer(mqtt_server, mqtt_port);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
// Generate a unique client ID
String clientId = "ESP8266Client-" + String(millis());
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
delay(2000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Read sensor data every 2 seconds
static unsigned long lastReadTime = 0;
if (millis() - lastReadTime > 2000) {
lastReadTime = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String payload = "Temperature: " + String(t) + " C, Humidity: " + String(h) + " %";
Serial.print("Publishing data: ");
Serial.println(payload);
client.publish("temprature_hum", payload.c_str());
}
}
The text was updated successfully, but these errors were encountered: