void setup() {
00:11:31.280 ->
00:11:31.280 -> ets Jan 8 2013,rst cause:4, boot mode:(3,6)
00:11:31.280 ->
00:11:31.280 -> wdt reset
00:11:31.280 -> load 0x4010f000, len 3460, room 16
00:11:31.280 -> tail 4
00:11:31.280 -> chksum 0xcc
00:11:31.280 -> load 0x3fff20b8, len 40, room 4
00:11:31.280 -> tail 4
00:11:31.280 -> chksum 0xc9
00:11:31.280 -> csum 0xc9
00:11:31.280 -> v00048310
00:11:31.280 -> ~ld
00:11:39.756 ->
The source code is:
#include "Wire.h" //I2C library
#include "SHT31.h" //I2C sensor library
#include <ESP8266WiFi.h>
#include <Ticker.h> // non-blocking delay library
#include <AsyncMqttClient.h>
#define WIFI_SSID "********"
#define WIFI_PASSWORD "***********"
// Raspberri Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, ***, ***)
#define MQTT_PORT 1883
String hostname = "BathroomHumidTemp";
// Temperature & humidity MQTT Topics
#define MQTT_PUB_TEMP "esp/dht/b/temp"
#define MQTT_PUB_HUM "esp/dht/b/humid"
// I2C & DHT
uint32_t start;
uint32_t stop;
// Initialize DHT sensor
SHT31 sht;
// pin assignments:
const int BUTTON_PIN = 7; // the number of the pushbutton pin
const int RED_LED_PIN = 5; // the number of the RED LED pin
const int GREEN_LED_PIN = 6; // the number of the GREEN LED pin
int buttonState = 0; // variable for reading the pushbutton status
// Variables to hold sensor readings
float temp;
float hum;
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;
unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.setHostname(hostname.c_str());
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
Serial.println("Connected to Wi-Fi.");
connectToMqtt();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Serial.println("Disconnected from Wi-Fi.");
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttPublish(uint16_t packetId) {
Serial.print("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup() {
// // initialize the LED pins as an output:
// pinMode(RED_LED_PIN, OUTPUT);
// pinMode(GREEN_LED_PIN, OUTPUT);
// // initialize the pushbutton pin as an pull-up input:
// // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
// pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println();
Wire.begin();
sht.begin(0x44); //Sensor I2C Address
Wire.setClock(100000);
uint16_t stat = sht.readStatus();
Serial.print(stat, HEX);
Serial.println();
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
//mqttClient.onSubscribe(onMqttSubscribe);
//mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
// If your broker requires authentication (username and password), set them below
//mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
connectToWifi();
}
void loop() {
// // read the state of the pushbutton value:
// buttonState = digitalRead(BUTTON_PIN);
// // control LED according to the state of button
// if(buttonState == HIGH) // If button is pressing
// digitalWrite(RED_LED_PIN, HIGH); // turn on LED
// else // otherwise, button is not pressing
// digitalWrite(RED_LED_PIN, LOW); // turn off LED
unsigned long currentMillis = millis();
// Every X number of seconds (interval = 10 seconds)
// it publishes a new MQTT message
if (currentMillis - previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
sht.read();
// New DHT sensor readings
hum = sht.getHumidity();
// Read temperature as Celsius (the default)
temp = sht.getTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
//temp = dht.readTemperature(true);
// Publish an MQTT message on topic esp/dht/b/temp
uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
Serial.printf("Message: %.2f \n", temp);
// Publish an MQTT message on topic esp/dht/b/humid
uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2);
Serial.printf("Message: %.2f \n", hum);
}
}