164 lines
4.1 KiB
C++
164 lines
4.1 KiB
C++
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
#include <Wire.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
#include <config.h>
|
|
|
|
// Default(A) SHT30 I2C address is 0x44
|
|
// B SHT30 I2C address is 0x45
|
|
#define Addr 0x44
|
|
|
|
//WIFI
|
|
const char* ssid = CONFIG_WIFI_SSID; // Enter your WiFi name
|
|
const char* password = CONFIG_WIFI_PASS; // Enter WiFi password
|
|
|
|
//MQTT
|
|
const char* mqttServer = CONFIG_MQTT_HOST;
|
|
const char* mqttUser = CONFIG_MQTT_USER;
|
|
const int mqttPort = CONFIG_MQTT_PORT;
|
|
const char* mqttPassword = CONFIG_MQTT_PASS;
|
|
const char* mqttClientId = CONFIG_MQTT_CLIENT_ID;
|
|
|
|
//Domoticz
|
|
const char* domotopic = CONFIG_MQTT_TOPIC;
|
|
const int idxcomb = CONFIG_MQTT_IDX_Comb;
|
|
const int idxtemp = CONFIG_MQTT_IDX_Temp;
|
|
const int idxhum = CONFIG_MQTT_IDX_Hum;
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
void setup() {
|
|
// Initialise I2C communication as MASTER
|
|
Wire.begin(0, 2);
|
|
// Initialise serial communication, set baud rate = 9600
|
|
Serial.begin(9600);
|
|
|
|
|
|
if (CONFIG_IP_STATIC == 1){
|
|
if (!WiFi.config(local_IP, gateway, subnet,dns)) {
|
|
Serial.println("Static IP configration error");
|
|
}
|
|
}
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.println("Connecting to WiFi..");
|
|
}
|
|
Serial.println("Connected to the WiFi");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
client.setServer(mqttServer, mqttPort);
|
|
while (!client.connected()) {
|
|
Serial.println("Connecting to MQTT...");
|
|
|
|
if (client.connect("TempOutdoor", mqttUser, mqttPassword )) {
|
|
|
|
Serial.println("Connected to MQTT");
|
|
|
|
} else {
|
|
|
|
Serial.print("failed with state ");
|
|
Serial.print(client.state());
|
|
delay(2000);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
//WiFi Check
|
|
if ( WiFi.status() != WL_CONNECTED ) {
|
|
Serial.println("WiFi disconnected");
|
|
WiFi.disconnect();
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.println("Connecting to WiFi..");
|
|
}
|
|
Serial.println("Connected to the WiFi");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
//MQTT Check
|
|
while (!client.connected()) {
|
|
client.connect("TempOutdoor", mqttUser, mqttPassword);
|
|
}
|
|
|
|
unsigned int data[6];
|
|
|
|
// Start I2C Transmission
|
|
Wire.beginTransmission(Addr);
|
|
// Send measurement command
|
|
Wire.write(0x2C);
|
|
Wire.write(0x06);
|
|
// Stop I2C transm ission
|
|
Wire.endTransmission();
|
|
delay(500);
|
|
|
|
// Request 6 bytes of data
|
|
Wire.requestFrom(Addr, 6);
|
|
|
|
// Read 6 bytes of data
|
|
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
|
|
if (Wire.available() == 6)
|
|
{
|
|
data[0] = Wire.read();
|
|
data[1] = Wire.read();
|
|
data[2] = Wire.read();
|
|
data[3] = Wire.read();
|
|
data[4] = Wire.read();
|
|
data[5] = Wire.read();
|
|
}
|
|
|
|
// Convert the data
|
|
float cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
|
|
float CalcHumidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);
|
|
int RoundHumidity = CalcHumidity;
|
|
// Output data to serial monitor
|
|
Serial.print("Relative Humidity : ");
|
|
Serial.print(CalcHumidity);
|
|
Serial.println(" %RH");
|
|
Serial.print("Temperature in Celsius : ");
|
|
Serial.print(cTemp);
|
|
Serial.println(" C");
|
|
|
|
StaticJsonDocument<100> JSONHum;
|
|
StaticJsonDocument<100> JSONTemp;
|
|
StaticJsonDocument<100> JSONComb;
|
|
|
|
JSONHum["idx"] = idxhum;
|
|
JSONHum["nvalue"] = RoundHumidity;
|
|
JSONHum["svalue"] = "0";
|
|
|
|
JSONTemp["idx"] = idxtemp;
|
|
JSONTemp["nvalue"] = 0;
|
|
JSONTemp["svalue"] = String(cTemp) ;
|
|
|
|
JSONComb["idx"] = idxcomb;
|
|
JSONComb["nvalue"] = 0;
|
|
JSONComb["svalue"] = String(cTemp) + String(";") + String(CalcHumidity) + String(";0");
|
|
|
|
char buffer1[256];
|
|
char buffer2[256];
|
|
char buffer3[256];
|
|
serializeJson(JSONTemp, buffer1);
|
|
serializeJson(JSONHum, buffer2);
|
|
serializeJson(JSONComb, buffer3);
|
|
|
|
if (client.publish(domotopic, buffer1) == true) {
|
|
Serial.println("Published MQTT messages");
|
|
} else {
|
|
Serial.println("Error sending MQTT messages");
|
|
}
|
|
client.publish(domotopic, buffer2);
|
|
client.publish(domotopic, buffer3);
|
|
client.loop();
|
|
delay(60000); //1min
|
|
} |