//#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
//const char* WIFI_SSID = "SK-HOMe2";
//const char* WIFI_PASSWORD = "kurdel1504+";
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "haclientsadafasf";
const PROGMEM char* MQTT_SERVER_IP = "192.168.88.175";
//const PROGMEM char* MQTT_SERVER_IP = "172.30.32.1";
//const PROGMEM char* MQTT_SERVER_IP = "169.254.113.36";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "mosuser";
const PROGMEM char* MQTT_PASSWORD = "mosuser01+";
// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "arduino/bojler";
// sleeping time
const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 600; // 10 minutes x 60 seconds
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the temperature and the humidity
void publishData(float p_temperature, float p_humidity) {
// create a JSON object
// doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// INFO: the data must be converted into a string; a problem occurs when using floats...
root["temperature_dolni"] = (String)p_temperature;
root["temperature_stred"] = (String)p_humidity;
root.prettyPrintTo(Serial);
Serial.println("");
/*
{
"temperature": "23.20" ,
"humidity": "43.70"
}
*/
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data, true);
yield();
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
Serial.begin(115200);
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.println("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
////client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
///client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = 75.00f;
// Read temperature as Celsius (the default)
float t = 21.00f;
if (isnan(h) || isnan(t)) {
Serial.println("ERROR: Failed to read from DHT sensor!");
return;
} else {
//Serial.println(h);
//Serial.println(t);
// publishData(t, h);
}
Serial.println("INFO: Closing the MQTT connection");
client.disconnect();
Serial.println("INFO: Closing the Wifi connection");
WiFi.disconnect();
//ESP.deepSleep(SLEEPING_TIME_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
//delay(500); // wait for deep sleep to happen
}