Overkapping/Overkapping.ino
Frank 8b92484ce6 Update Overkapping.ino
Temp + Humidity + Baro implemented
2022-08-19 16:50:04 +02:00

324 lines
6.3 KiB
C++

#define DEBUG 1
#include <ESP8266WiFi.h>
//#include <ESP8266HTTPClient.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <RCSwitch.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"
#define idx_temp 2269
#define BUZZER_PIN D8
const char* ssid = "FrankHilde";
const char* password = "HildeFrank";
const char* mqtt_server = "192.168.2.153";
const char* mqtt_user = "domoticz";
const char* mqtt_pwd = "MQTT&Domoticz";
WiFiClient espClient;
PubSubClient client(espClient);
RCSwitch mySwitch = RCSwitch();
Adafruit_BMP280 bme; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();
long lastMsg = 0;
char msg[100];
int value = 0;
#define idx_deurbel 906
#define idx_deurbelstil 917
#define idx_overkapping 2268
char buffer[100] = {0};
//global veriables
//Page main
const char* datumtijd;
const char* weer_text;
int main_btn_staandelamp = 0;
int main_btn_tuinverlichting = 0;
int main_btn_hanglamp = 0;
int main_btn_haardverlichting = 0;
int main_btn_rooster = 1;
int main_btn_deurbelstil = 0;
int alarm_sound = 0;
int main_pic_weer;
float main_temp_in = 0;
float main_hum_in = 0;
float main_baro = 0;
float main_temp_out = 0;
float main_hum_out = 0;
float main_temp_min = 0;
float main_temp_max = 0;
float main_rain = 0;
float main_rain_hour = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
#ifdef DEBUG
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
#endif
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}
randomSeed(micros());
#ifdef DEBUG
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#endif
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
#ifdef DEBUG
Serial.print("Attempting MQTT connection...");
#endif
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
// if (client.connect(clientId.c_str())) {
if (client.connect(clientId.c_str(),mqtt_user,mqtt_pwd)) {
#ifdef DEBUG
Serial.println("connected");
#endif
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
// client.subscribe("domoticz/out/example/display");
client.subscribe("domoticz/out/display/Overkapping");
} else {
#ifdef DEBUG
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
#endif
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, 0);
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
#ifdef DEBUG
Serial.begin(115200);
#endif
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(mqtt_callback);
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(D7);
// Optional set protocol (default is 1, will work for most outlets)
mySwitch.setProtocol(1);
// Optional set pulse length.
mySwitch.setPulseLength(390); //Orgineel op 390 !!
// Optional set number of transmission repetitions.
mySwitch.setRepeatTransmit(5);
Serial.println(F("RF Init done..."));
if (!bme.begin())
{
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
if (!sensor.begin())
{
Serial.println("Did not find Si7021 sensor!");
while (true);
}
}
void loop() {
if (!client.connected())
{
reconnect();
}
client.loop();
if(alarm_sound == 1)
{
tone(BUZZER_PIN, 1000, 50);
delay(1000);
}
long now = millis();
if (now - lastMsg > 60000)
{
update_temp();
lastMsg = now;
++value;
}
}
//MQTT receive
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
int svalue1, loc, StepperTarget;
String sub, devicename;
StaticJsonDocument<512> doc;
#ifdef DEBUG
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
#endif
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
#ifdef DEBUG
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
#endif
return;
}
// Get the root object in the document
JsonObject root = doc.as<JsonObject>();
int idx = root["idx"];
const char* idx_name = root["name"];
const char* idx_svalue1 = root["svalue1"];
const char* idx_svalue2 = root["svalue2"];
const char* idx_svalue4 = root["svalue4"];
int idx_nvalue = root["nvalue"];
#ifdef DEBUG
Serial.println(idx);
Serial.println(idx_name);
Serial.println(idx_svalue1);
Serial.println(idx_nvalue);
#endif
if (idx == idx_deurbel)
{
if (idx_nvalue == 1)
{
#ifdef DEBUG
Serial.println("Show doorbell");
#endif
//delay(200);
for (int i = 0; i < 10; i++)
{
tone(BUZZER_PIN, 1000, 200);
delay(500);
}
}
}
if (idx == idx_overkapping)
{
if (idx_nvalue == 1)
{
#ifdef DEBUG
Serial.println("Overkapping LED --> On !!");
#endif
mySwitch.send(14944537, 24); //ON
}
else
{
#ifdef DEBUG
Serial.println("Overkapping LED --> Off !!");
#endif
mySwitch.send(14944539, 24); //OFF
}
}
}
void update_temp()
{
float val_temp;
float val_hum;
float val_baro;
StaticJsonDocument<200> doc;
// Make our document be an object
JsonObject root = doc.to<JsonObject>();
root["command"] = "udevice";
root["idx"] = 2269;
root["nvalue"] = 0 ;
//svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
val_temp = sensor.readTemperature();
val_hum = sensor.readHumidity();
val_baro = bme.readPressure() / 100;
root["svalue"] = String(val_temp) + String(";") + String(val_hum) + String(";0;") + String(val_baro) + String(";0");
// root["svalue"] = "20;60;0;1024;0";
serializeJson(root, msg);
#ifdef DEBUG
Serial.println(msg);
#endif
client.publish("domoticz/in", msg);
}