first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,269 @@
#include "Domoticz.h"
Domoticz::Domoticz(String domoticz, String port, const char* ssid, const char* pass)
{
_domoticz = domoticz;
_port = port;
_ssid = ssid;
_pass = pass;
// // Domo
// _domoc[_domoticz.length() + 1];
// _domoticz.toCharArray(_domoc, _domoticz.length() + 1);
//
// // Port
// char portc[_port.length() + 1];
// _port.toCharArray(portc, _port.length() + 1);
// _iport = atoi(portc);
}
boolean Domoticz::connect()
{
// Domo
char _domoc[_domoticz.length() + 1];
_domoticz.toCharArray(_domoc, _domoticz.length() + 1);
// Port
char portc[_port.length() + 1];
_port.toCharArray(portc, _port.length() + 1);
int _iport = atoi(portc);
boolean connected = _client.connect(_domoc, _iport);
Serial.print(_domoc);
Serial.print(" ");
Serial.print(_iport);
Serial.print(" ");
Serial.print(" connected ");
Serial.println(connected);
return connected;
}
void Domoticz::close()
{
}
void Domoticz::initWifi()
{
WiFi.mode(WIFI_AP);
WiFi.begin(_ssid, _pass); //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.print("Connexion au reseau ");
Serial.println(WiFi.localIP());
}
void Domoticz::initWifiStatic(
IPAddress ip,
IPAddress gateway,
IPAddress subnet,
IPAddress DNS)
{
WiFi.config(ip, gateway, subnet, DNS);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(_ssid, _pass);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(200);
}
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println();
Serial.println("Fail connecting");
delay(5000);
ESP.restart();
}
Serial.print(" static OK ");
Serial.print("Module IP: ");
Serial.println(WiFi.localIP());
}
String Domoticz::generateKey()
{
// WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ESP8266 Thing " + macID;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);
for (int i = 0; i < AP_NameString.length(); i++)
{
AP_NameChar[i] = AP_NameString.charAt(i);
}
// WiFi.softAP(AP_NameChar, WiFiAPPSK);
Serial.println("macID=" + macID);
return macID;
}
IPAddress Domoticz::getIP(String macId)
{
IPAddress ip; //(192, 168, 1, 222);
String fst = macId.substring(0, 2);
String sec = macId.substring(2);
char fstc[fst.length() + 1];
fst.toCharArray(fstc, fst.length() + 1);
char secc[sec.length() + 1];
sec.toCharArray(secc, fst.length() + 1);
return IPAddress(192, 168, strtol(fstc, 0, 16), strtol(secc, 0, 16));
}
void Domoticz::executeJson(String json, String svalue, String nvalue)
{
// Domo
char _domoc[_domoticz.length() + 1];
_domoticz.toCharArray(_domoc, _domoticz.length() + 1);
// Port
char portc[_port.length() + 1];
_port.toCharArray(portc, _port.length() + 1);
int _iport = atoi(portc);
_client.print("GET " + json); //"GET /json.htm?type=command&param=getuservariables");
if (svalue != "") {
_client.print("&svalue=" + svalue);
}
if (nvalue != "") {
_client.print("&nvalue=" + nvalue);
}
Serial.println(json + "&svalue=" + svalue + "&nvalue=" + nvalue);
_client.println(" HTTP/1.1");
_client.print("Host: ");
_client.print(_domoc);
_client.print(":");
_client.println(_iport);
_client.println("User-Agent: Arduino-ethernet");
_client.println("Connection: close");
_client.println();
}
void Domoticz::readResponse()
{
int x = 0; // variable for looping
char dataStr[16]; //array to store the response
char c;
while(_client.available()) { //_client.connected() || _client.available()) {
String req = readLine();
Serial.println(req);
if (req == "") {
break;
}
// c = _client.read(); //read first character
// while (c!= '<') { //while < character is not coming yet, keep reading character
// c = _client.read();
// }
// c = _client.read(); //read the '<' character, but not storing in array
// while (c != '>') { //while > character is not coming yet,
// dataStr[x] = c; //Store character in array
// x++; //incrementing index array
// }
// for (x=0; x<12; x++) {
// Serial.print(dataStr[x]);
// }
}
Serial.println("Fin réponse");
}
//--------------------------------------------------------------------------------------------------
// Read current supply voltage
//--------------------------------------------------------------------------------------------------
String Domoticz::readVcc() {
// most exact output
uint16_t v = ESP.getVcc();
float_t v_cal = ((float) v / 1024.0f);
char v_str[10];
dtostrf(v_cal, 5, 3, v_str);
sprintf(v_str, "%s", v_str);
Serial.print("Tension lue ");
Serial.println(String(v_str));
return String(v_str); //ESP.getVcc() / 1024.0f; // Vcc in millivolts
}
String Domoticz::getIndexOfString(String data, String separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator.charAt(0) || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
String Domoticz::readLine()
{
return _client.readStringUntil('\n');
}
void Domoticz::readTempDayValues(String idx)
{
String json = "/json.htm?type=graph&sensor=temp&idx=" + idx + "&range=day";
int nb = 0;
int nbVal = 0;
Serial.println("readTempDayValues " + idx);
connect();
executeJson(json, "", "");
while (1)
{
String line = readLine();
if (line == "") {
break;
}
if (line.indexOf("\"te\"") != -1) {
//Serial.print(line + " ");
nbVal++;
if (nbVal % 10) {
String val = line.substring(line.indexOf(":") + 2, line.length());
// Serial.println(val);
}
}
nb++;
}
Serial.print("Nombre de lignes ");
Serial.println(nb);
close();
}
int Domoticz::strToHex(char str[])
{
return (int) strtol(str, 0, 16);
}

View File

@@ -0,0 +1,40 @@
#ifndef Domoticz_h
#define Domoticz_h
#include <ESP8266WiFi.h>
//IPAddress ip(192, 168, 1, 222);
class Domoticz
{
public:
Domoticz(String domoticz, String port, const char* ssid, const char* pass);
boolean connect();
void close();
void initWifi();
void initWifiStatic(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress DNS);
void executeJson(String json, String svalue, String nvalue);
String generateKey();
IPAddress getIP(String macId);
String readLine();
void readResponse();
String readVcc();
void readTempDayValues(String idx);
void getIdFromDomoticz(String macID);
int strToHex(char str[]);
static String getIndexOfString(String data, String separator, int index);
private:
const char* _ssid;
const char* _pass;
public:
WiFiClient _client;
char _domoc[];
int _iport;
String _domoticz;
String _port;
};
#endif

View File

@@ -0,0 +1,120 @@
/////////////////////
// Domoticz Classe
/////////////////////
#include "Domoticz.h"
Domoticz domo("192.168.1.3", "81", "Livebox-37cc", "8A6060920A8A86896F770F2C47");
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress DNS(192, 168, 1, 1);
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 9800
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 4800
uint16_t samples[NUMSAMPLES];
void setup()
{
Serial.begin(9600);
//delay(200);
String macId = domo.generateKey();
IPAddress ip = domo.getIP(macId);
// Conversion d'objet en pointeur
domo.initWifiStatic(ip, gateway, subnet, DNS);
}
void loop()
{
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.print("Average analog reading ");
Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(steinhart * 1.25);
Serial.println(" *C");
printInfo(steinhart * 1.25);
sleep(300);
//delay(5000);
}
void sleep(int sleepTime)
{
Serial.print("Go to sleep ");
Serial.println(sleepTime);
delay(20);
ESP.deepSleep(sleepTime * 1000000L);
//sleepWifi();
delay(200);
}
void printInfo(double temperature)
{
// Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT;BAR;BAR_FOR
boolean connected = domo.connect();
if (connected) {
Serial.println("Dans set temperature");
String svalue = String(temperature) ;
Serial.println(svalue);
domo.executeJson("/json.htm?type=command&param=udevice&idx=1139", svalue, "0");
domo._client.stop();
delay(200);
}
Serial.print("Time = ");
Serial.println(millis());
delay(200);
}