Files
Arduino/ESP8266_DOMOTICZ_SERIAL_SLAVE/ESP8266_DOMOTICZ_SERIAL_SLAVE.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

113 lines
2.6 KiB
C++

extern "C" {
#include "user_interface.h"
}
#include <Wire.h>
#include <ESP8266WiFi.h>
const char* ssid = "Livebox-37cc";
const char* password = "8A6060920A8A86896F770F2C47";
//const char* ssid = "SFR_E4C8"; // Put your SSID here
//const char* password = "Sept/6-Six/4"; // Put your PASSWORD here
const int sleepTime = 120; //Power down WiFi for n seconds
// int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiClient client;
// domoticz
const char * domoticz_server = "192.168.1.3"; //Domoticz port
int port = 81; //Domoticz port
int idx = 158; //IDX for this virtual sensor, found in Setup -> Devices
float lum = 0;
int inByte;
String sChaine = "";
void setup()
{
Serial.begin(9600);
initWifi();
}
void loop() {
if (Serial.available()) {
delay(100);
while (Serial.available()) {
inByte = Serial.read();
if ( inByte > 32 && inByte < 123)
{
sChaine += char(inByte);
}
}
sChaine.trim();
Serial.println(sChaine);
if (sChaine != "") {
printInfo();
}
sChaine = "";
delay(100);
}
}
//
//void readLum()
//{
// lum = analogRead(2);
// Serial.println(lum);
//}
//void sleepWifi()
//{
// //Time to let the WiFi go to sleep!
// Serial.println("Sleeping...");
// WiFi.disconnect();
// WiFi.mode(WIFI_OFF);
// WiFi.forceSleepBegin(sleepTime * 1000000L); //In uS. Must be same length as your delay
// delay(sleepTime * 1000); //Hang out at 15mA for 6 seconds
// WiFi.mode(WIFI_STA);
// Serial.println("Awake!");
//}
void initWifi()
{
WiFi.mode(WIFI_AP);
WiFi.begin(ssid, password); //Connect to local Wifi
Serial.println();
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("WiFi Connected");
Serial.println("");
Serial.println(WiFi.localIP());
}
void printInfo()
{
// Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
if (client.connect(domoticz_server, port)) {
client.print("GET " + sChaine); ///json.htm?type=command&param=udevice&idx=");
// client.print(idx);
// client.print("&nvalue=0&svalue=");
// client.print((1000.0 * lum / 1024.0));
// client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
client.println(" HTTP/1.1");
client.print("Host: ");
client.print(domoticz_server);
client.print(":");
client.println(port);
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
client.stop();
}
}