first commit
This commit is contained in:
293
ESP8266_DOMOTICZ_DALLAS/Domoticz.cpp
Executable file
293
ESP8266_DOMOTICZ_DALLAS/Domoticz.cpp
Executable file
@@ -0,0 +1,293 @@
|
||||
#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);
|
||||
//
|
||||
// // Port1024
|
||||
// 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;
|
||||
}
|
||||
|
||||
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¶m=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();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
// 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();
|
||||
}
|
||||
|
||||
void Domoticz::getIdFromDomoticz(String macID, Params * params)
|
||||
{
|
||||
String separator = ",";
|
||||
int nb = 0;
|
||||
|
||||
Serial.println("Dans getIdFromDomoticz " + macID);
|
||||
|
||||
// -------------------
|
||||
// LECTURE PARAM
|
||||
// -------------------
|
||||
if (params->esp8266_id == "") {
|
||||
boolean connected = connect();
|
||||
|
||||
if (connected) {
|
||||
Serial.println("Connected to domoticz");
|
||||
executeJson("/json.htm?type=command¶m=getuservariables", "", "");
|
||||
// Read the first line of the request
|
||||
while (1) {
|
||||
String req = readLine();
|
||||
if (req.indexOf(macID) != -1) {
|
||||
Serial.println(req);
|
||||
req = readLine();
|
||||
req = readLine();
|
||||
String val = req.substring(req.indexOf(":") + 2, req.length() - 1);
|
||||
Serial.println(val);
|
||||
val.replace("\"", "");
|
||||
params->esp8266_id = getIndexOfString(val, separator, 0);
|
||||
String tmp = getIndexOfString(val, separator, 1);
|
||||
|
||||
char tmpc[tmp.length() + 1];
|
||||
tmp.toCharArray(tmpc, tmp.length() + 1);
|
||||
params->sleepTime = atoi(tmpc);
|
||||
if (params->sleepTime <= 0) {
|
||||
params->sleepTime = 60;
|
||||
}
|
||||
params->esp8266_id_Vcc = getIndexOfString(val, separator, 2);
|
||||
|
||||
params->esp8266_id_lum = getIndexOfString(val, separator, 3);
|
||||
Serial.println("val1=" + params->esp8266_id);
|
||||
Serial.print("val2=");
|
||||
Serial.println(params->sleepTime);
|
||||
Serial.println("val3=" + params->esp8266_id_Vcc);
|
||||
Serial.println("val4=" + params->esp8266_id_lum);
|
||||
|
||||
break;
|
||||
}
|
||||
if (req == "" || nb > 500) {
|
||||
break;
|
||||
}
|
||||
nb++;
|
||||
}
|
||||
_client.stop();
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
38
ESP8266_DOMOTICZ_DALLAS/Domoticz.h
Executable file
38
ESP8266_DOMOTICZ_DALLAS/Domoticz.h
Executable file
@@ -0,0 +1,38 @@
|
||||
#ifndef Domoticz_h
|
||||
#define Domoticz_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "Params.h"
|
||||
|
||||
|
||||
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 getIdFromDomoticz();
|
||||
String generateKey();
|
||||
IPAddress getIP(String macId);
|
||||
String readLine();
|
||||
String readVcc();
|
||||
void readTempDayValues(String idx);
|
||||
void getIdFromDomoticz(String macID, Params * params);
|
||||
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
|
||||
104
ESP8266_DOMOTICZ_DALLAS/ESP8266_DOMOTICZ_DALLAS.ino
Executable file
104
ESP8266_DOMOTICZ_DALLAS/ESP8266_DOMOTICZ_DALLAS.ino
Executable file
@@ -0,0 +1,104 @@
|
||||
//extern "C" {
|
||||
//#include "user_interface.h"
|
||||
//}
|
||||
|
||||
// Need for getVcc
|
||||
ADC_MODE(ADC_VCC);
|
||||
|
||||
/////////////////////
|
||||
// Domoticz Classe
|
||||
/////////////////////
|
||||
#include "Domoticz.h"
|
||||
Domoticz domo("192.168.1.3", "81", "Livebox-37cc", "8A6060920A8A86896F770F2C47");
|
||||
#include "Modules.h"
|
||||
Modules modules;
|
||||
Params * params;
|
||||
|
||||
IPAddress gateway(192, 168, 1, 1);
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
IPAddress DNS(192, 168, 1, 1);
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(PIN_ALIM_DS, OUTPUT);
|
||||
digitalWrite(PIN_ALIM_DS, HIGH);
|
||||
|
||||
Serial.begin(9600);
|
||||
// Conversion d'objet en pointeur
|
||||
String macId = domo.generateKey();
|
||||
IPAddress ip = domo.getIP(macId);
|
||||
|
||||
Params p;
|
||||
params = &p;
|
||||
domo.initWifiStatic(ip, gateway, subnet, DNS);
|
||||
|
||||
// domo.initWifi();
|
||||
domo.getIdFromDomoticz(domo.generateKey(), params);
|
||||
|
||||
modules.readTemperature();
|
||||
|
||||
digitalWrite(PIN_ALIM_DS,LOW);
|
||||
// modules.readLuminosite();
|
||||
printInfo();
|
||||
|
||||
modules.sleep(params->sleepTime);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void printInfo()
|
||||
{
|
||||
// Domoticz format /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
|
||||
|
||||
Serial.println("Dans printInfo " + params->esp8266_id);
|
||||
|
||||
boolean connected = domo.connect();
|
||||
|
||||
if (connected && modules.temp > 0) {
|
||||
Serial.println("Dans set temperature");
|
||||
String svalue = String(modules.temp) + ";" + String(modules.humidity) + ";0;0;0";
|
||||
Serial.println(svalue);
|
||||
domo.executeJson("/json.htm?type=command¶m=udevice&idx=" + params->esp8266_id, svalue, "0");
|
||||
|
||||
domo._client.stop();
|
||||
|
||||
delay(200);
|
||||
|
||||
}
|
||||
|
||||
connected = domo.connect();
|
||||
if (connected && params->esp8266_id_Vcc != "") {
|
||||
Serial.print(" Envoi tension ");
|
||||
Serial.print(params->esp8266_id_Vcc);
|
||||
Serial.print(" ");
|
||||
String vcc = domo.readVcc();
|
||||
Serial.println(vcc);
|
||||
|
||||
// domo._client.print("GET /json.htm?type=command¶m=udevice&idx=");
|
||||
domo.executeJson("/json.htm?type=command¶m=udevice&idx=" + params->esp8266_id_Vcc, vcc, "0");
|
||||
domo._client.stop();
|
||||
}
|
||||
|
||||
// Serial.print("Luminosite ");
|
||||
// Serial.println(modules.lum);
|
||||
//
|
||||
// connected = domo.connect();
|
||||
// if (connected && params->esp8266_id_lum != "") {
|
||||
// Serial.print(" Envoi luminosite ");
|
||||
// Serial.print(params->esp8266_id_lum);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(modules.lum);
|
||||
//
|
||||
// // domo._client.print("GET /json.htm?type=command¶m=udevice&idx=");
|
||||
// domo.executeJson("/json.htm?type=command¶m=udevice&idx=" + params->esp8266_id_lum, String(modules.lum), "0");
|
||||
// domo._client.stop();
|
||||
// }
|
||||
|
||||
Serial.print("Time = ");
|
||||
Serial.println(millis());
|
||||
|
||||
delay(200);
|
||||
}
|
||||
74
ESP8266_DOMOTICZ_DALLAS/Modules.cpp
Executable file
74
ESP8266_DOMOTICZ_DALLAS/Modules.cpp
Executable file
@@ -0,0 +1,74 @@
|
||||
#include "Modules.h"
|
||||
|
||||
// int status = WL_IDLE_STATUS; // the Wifi radio's status
|
||||
//WiFiClient client;
|
||||
// Dallas
|
||||
|
||||
OneWire oneWire(ONE_WIRE_BUS);
|
||||
DallasTemperature DS18B20(&oneWire);
|
||||
|
||||
Modules::Modules()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Modules::readTemperature()
|
||||
{
|
||||
int bcl = 0;
|
||||
temp = -127;
|
||||
DS18B20.requestTemperatures();
|
||||
while (bcl < 10 && (temp < 0 || temp > 50)) {
|
||||
temp = DS18B20.getTempCByIndex(0);
|
||||
Serial.print("Temperature: ");
|
||||
Serial.println(temp);
|
||||
bcl++;
|
||||
if (temp < 0 || temp > 50) {
|
||||
Serial.print("Temp non lue");
|
||||
Serial.println(temp);
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//void Modules::barometre() {
|
||||
// /* See Example: TypeA_WithDIPSwitches */
|
||||
//
|
||||
// delay(500);
|
||||
// Serial.print("Barometre: ");
|
||||
// //Serial.println(bmp.begin());
|
||||
// // BMP
|
||||
//
|
||||
// if (bmp.begin()) {
|
||||
// delay(1000);
|
||||
// temp = bmp.readTemperature();
|
||||
// pressure = bmp.readPressure() / 100.0;
|
||||
// pression = pressure / 101.325;
|
||||
// pression = pression * 0.760 * 100;
|
||||
// // http://en.wikipedia.org/wiki/Atmospheric_pressure#Mean_sea_level_pressure
|
||||
// // Serial.print("Presiure la nivelul marii (calculata) = ");
|
||||
// presiune = bmp.readSealevelPressure(ALTITUDE) / 101.325;
|
||||
// presiune = presiune * 0.760;
|
||||
// Serial.print("Temperature="); Serial.println(temp);
|
||||
// Serial.print("pressure="); Serial.println(pressure);
|
||||
// Serial.print("pression="); Serial.println(pression);
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
||||
void Modules::sleep(int sleepTime)
|
||||
{
|
||||
Serial.print("Go to sleep ");
|
||||
Serial.println(sleepTime);
|
||||
delay(20);
|
||||
|
||||
ESP.deepSleep(sleepTime * 1000000L); // Infini
|
||||
|
||||
//sleepWifi();
|
||||
delay(200);
|
||||
}
|
||||
|
||||
void Modules::readLuminosite()
|
||||
{
|
||||
lum = analogRead(PIN_LUM);
|
||||
}
|
||||
34
ESP8266_DOMOTICZ_DALLAS/Modules.h
Executable file
34
ESP8266_DOMOTICZ_DALLAS/Modules.h
Executable file
@@ -0,0 +1,34 @@
|
||||
#ifndef Modules_h
|
||||
#define Modules_h
|
||||
|
||||
#include <Arduino.h>
|
||||
//#include <Adafruit_BMP085.h>
|
||||
#include <DallasTemperature.h>
|
||||
#include "Pins.h"
|
||||
|
||||
// ################# Barometre ####
|
||||
#define ALTITUDE 19.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters
|
||||
|
||||
//SFE_BMP180 pressure;
|
||||
// #####################
|
||||
|
||||
class Modules
|
||||
{
|
||||
public:
|
||||
Modules();
|
||||
void barometre();
|
||||
void readTemperature();
|
||||
void readLuminosite();
|
||||
void sleep(int sleepTime);
|
||||
private:
|
||||
public:
|
||||
// Adafruit_BMP085 bmp;
|
||||
double humidity = 0;
|
||||
double temp = -127;
|
||||
double pression = 0;
|
||||
double pressure = 0;
|
||||
double presiune = 0;
|
||||
double lum = 0;
|
||||
|
||||
};
|
||||
#endif
|
||||
7
ESP8266_DOMOTICZ_DALLAS/Params.cpp
Executable file
7
ESP8266_DOMOTICZ_DALLAS/Params.cpp
Executable file
@@ -0,0 +1,7 @@
|
||||
#include "Params.h"
|
||||
|
||||
Params::Params()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
16
ESP8266_DOMOTICZ_DALLAS/Params.h
Executable file
16
ESP8266_DOMOTICZ_DALLAS/Params.h
Executable file
@@ -0,0 +1,16 @@
|
||||
#ifndef Params_h
|
||||
#define Params_h
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
class Params {
|
||||
public :
|
||||
Params();
|
||||
public :
|
||||
String esp8266_id;
|
||||
String esp8266_id_Vcc;
|
||||
String esp8266_id_lum;
|
||||
int sleepTime;
|
||||
};
|
||||
|
||||
#endif
|
||||
14
ESP8266_DOMOTICZ_DALLAS/Pins.h
Executable file
14
ESP8266_DOMOTICZ_DALLAS/Pins.h
Executable file
@@ -0,0 +1,14 @@
|
||||
#ifndef Pins_h
|
||||
#define Pins_h
|
||||
#include <Arduino.h>
|
||||
|
||||
//#define SDA D1
|
||||
//#define SCL D2
|
||||
|
||||
#define ONE_WIRE_BUS 4 // 2 for esp01 4 for esp12 // DS18B20 pin
|
||||
#define PIN_LUM A0
|
||||
#define PIN_ALIM_DS 5 // 1 for esp01 5 for esp12
|
||||
//#define PIN_FADER 1 // analog
|
||||
//#define PIN_TMP 0 // analog
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user