Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't connect to my hiveq server from arduino using ESP8266 #1061

Open
mnasirbaloch opened this issue Jul 3, 2024 · 0 comments
Open

Can't connect to my hiveq server from arduino using ESP8266 #1061

mnasirbaloch opened this issue Jul 3, 2024 · 0 comments

Comments

@mnasirbaloch
Copy link

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());
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant