307 lines
7.5 KiB
C++
307 lines
7.5 KiB
C++
#include <Wire.h>
|
|
#include <stdlib.h> // for itoa() call
|
|
#include <stdio.h> // for printf() call
|
|
#include <ESP8266WiFiMulti.h>
|
|
ESP8266WiFiMulti WiFiMulti;
|
|
const uint16_t port = 81;
|
|
const char * host = "192.168.1.3"; // ip or dns
|
|
|
|
#define I2C_SLAVE_ADDRESS 0x0B // 12 pour l'esclave 2 et ainsi de suite
|
|
#define PAYLOAD_SIZE 2
|
|
int n = 0;
|
|
int buttonState = 0; // variable for reading the pushbutton status
|
|
|
|
#define SLAVE_ADDRESS 0x0B
|
|
|
|
int aiValue = 0; // input value
|
|
const int buttonPin = 14; // the number of the pushbutton pin
|
|
|
|
#include <DallasTemperature.h>
|
|
#define ONE_WIRE_BUS D4 // 2 for esp01 4 for esp12 // DS18B20 pin
|
|
#define PIN_ALIM_DS D3 // 1 for esp01 5 for esp12
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
DallasTemperature DS18B20(&oneWire);
|
|
|
|
|
|
// DEMUX
|
|
#define MUX_A D5
|
|
#define MUX_B D6
|
|
#define MUX_C D7
|
|
#define ANALOG_INPUT A0
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
while (!Serial)
|
|
{
|
|
delay(10);
|
|
}
|
|
pinMode(buttonPin, INPUT);
|
|
//pinMode(6, OUTPUT);
|
|
//Deifne output pins for Mux
|
|
pinMode(MUX_A, OUTPUT);
|
|
pinMode(MUX_B, OUTPUT);
|
|
pinMode(MUX_C, OUTPUT);
|
|
|
|
pinMode(PIN_ALIM_DS, OUTPUT);
|
|
digitalWrite(PIN_ALIM_DS, HIGH);
|
|
|
|
Wire.begin();
|
|
delay(200);
|
|
Serial.print("Demarrage");
|
|
// We start by connecting to a WiFi network
|
|
WiFi.mode(WIFI_STA);
|
|
WiFiMulti.addAP("Livebox-37cc", "8A6060920A8A86896F770F2C47");
|
|
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Wait for WiFi... ");
|
|
|
|
while(WiFiMulti.run() != WL_CONNECTED) {
|
|
Serial.print(".");
|
|
delay(500);
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
//digitalWrite(6, HIGH);
|
|
}
|
|
|
|
void loop() {
|
|
String result = "";
|
|
|
|
|
|
double temp = readTemperature();
|
|
|
|
double a0 = readAnalogic(0);
|
|
double a1 = readAnalogic(1);
|
|
|
|
// while (result == "") {
|
|
// delay(200);
|
|
// Serial.print(".");
|
|
Wire.requestFrom(SLAVE_ADDRESS,20);
|
|
//Serial.println("Master Demande envoyée");
|
|
delay(1);
|
|
while(Wire.available()) {
|
|
byte dataReceived = Wire.read();
|
|
result = result + String((char)dataReceived);
|
|
}
|
|
// if (result == "") {
|
|
// Wire.begin();
|
|
// }
|
|
// }
|
|
int splitT = result.indexOf("\n");
|
|
result = result.substring(0, splitT);
|
|
|
|
Serial.println(result);
|
|
splitT = result.indexOf(";");
|
|
String last_conso_value = result.substring(0, splitT);
|
|
String last_solar_value = result.substring(splitT + 1);
|
|
Serial.println("Master " + last_conso_value);
|
|
Serial.println("Master " + last_solar_value);
|
|
|
|
if (isDark()) {
|
|
Serial.println("Master Mode nuit");
|
|
if (temp > -10) {
|
|
envoieDomoticz("723", String(temp));
|
|
}
|
|
Wire.beginTransmission(SLAVE_ADDRESS); // transmit to device
|
|
Wire.write("nuit");
|
|
Wire.endTransmission();
|
|
delay(100);
|
|
Serial.print("Go to sleep nuit");
|
|
delay(20);
|
|
ESP.deepSleep(300 * 1000000L);
|
|
}
|
|
else {
|
|
if (temp > -10) {
|
|
envoieDomoticz("723", String(temp));
|
|
}
|
|
//envoieDomoticz("1053", last_conso_value); // intensite
|
|
delay(200);
|
|
//envoieDomoticz("1086", last_solar_value); // intensite
|
|
delay(200);
|
|
|
|
Serial.println("Master Mode jour");
|
|
Wire.beginTransmission(SLAVE_ADDRESS); // transmit to device
|
|
Wire.write("jour");
|
|
Wire.endTransmission();
|
|
delay(100);
|
|
Serial.print("Waiting ");
|
|
|
|
ESP.deepSleep(15 * 1000000L);
|
|
// WiFi.forceSleepBegin();
|
|
// delay(15000);
|
|
// WiFi.forceSleepWake();
|
|
}
|
|
//sleepWifi();
|
|
delay(200);
|
|
|
|
}
|
|
|
|
void envoieDomoticz(String IDX, String Svalue) {
|
|
|
|
|
|
Serial.print("Master connecting to ");
|
|
Serial.println(host);
|
|
|
|
// Use WiFiClient class to create TCP connections
|
|
WiFiClient client;
|
|
|
|
if (!client.connect(host, port)) {
|
|
Serial.println("Master connection failed");
|
|
Serial.println("Master wait 5 sec...");
|
|
delay(5000);
|
|
return;
|
|
}
|
|
|
|
// This will send the request to the server
|
|
client.print("GET /json.htm?type=command¶m=udevice&idx="+IDX+"&nvalue=0&svalue=" + Svalue);
|
|
client.println(" HTTP/1.1");
|
|
client.println("Host: 192.168.1.3:81");
|
|
client.println("User-Agent: Arduino-ethernet");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
//read back one line from server
|
|
String line = client.readStringUntil('\r');
|
|
Serial.println("Master ----------------------------------------------");
|
|
Serial.println(line);
|
|
Serial.println("Master ----------------------------------------------");
|
|
|
|
//Serial.println("Master closing connection");
|
|
client.stop();
|
|
|
|
delay(500);
|
|
}
|
|
boolean isDark()
|
|
{
|
|
String separator = ",";
|
|
int nb = 0;
|
|
|
|
WiFiClient client;
|
|
|
|
if (client.connect(host, port)) {
|
|
Serial.println("Master Connected to domoticz");
|
|
|
|
client.print("GET /json.htm?type=command¶m=getuservariable&idx=6");
|
|
client.println(" HTTP/1.1");
|
|
client.println("Host: 192.168.1.3:81");
|
|
client.println("User-Agent: Arduino-ethernet");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
// Read the first line of the request
|
|
boolean ret = false;
|
|
while (1) {
|
|
String req = client.readStringUntil('\n');
|
|
if (req.indexOf("Value") != -1) {
|
|
if (req.indexOf("True") != -1) {
|
|
ret = true;
|
|
}
|
|
break;
|
|
}
|
|
if (req == "" || nb > 500) {
|
|
break;
|
|
}
|
|
nb++;
|
|
}
|
|
client.stop();
|
|
delay(100);
|
|
return ret;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
String getVariableValue(String idx)
|
|
{
|
|
|
|
|
|
String separator = ",";
|
|
int nb = 0;
|
|
|
|
WiFiClient client;
|
|
|
|
if (client.connect(host, port)) {
|
|
Serial.println("Master Connected to domoticz");
|
|
|
|
client.print("GET /json.htm?type=command¶m=getuservariable&idx=" + idx);
|
|
client.println(" HTTP/1.1");
|
|
client.println("Host: 192.168.1.3:81");
|
|
client.println("User-Agent: Arduino-ethernet");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
// Read the first line of the request
|
|
String ret = "";
|
|
while (1) {
|
|
String req = client.readStringUntil('\n');
|
|
if (req.indexOf("Value") != -1) {
|
|
ret = req.substring(req.indexOf("Value"));
|
|
Serial.println("Master Idx=" + idx + " Value " + ret);
|
|
|
|
break;
|
|
}
|
|
if (req == "" || nb > 500) {
|
|
break;
|
|
}
|
|
nb++;
|
|
}
|
|
client.stop();
|
|
delay(100);
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
double readTemperature()
|
|
{
|
|
int bcl = 0;
|
|
double 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);
|
|
}
|
|
}
|
|
return temp;
|
|
}
|
|
|
|
double readAnalogic(int y)
|
|
{
|
|
float value;
|
|
|
|
if (y == 0) {
|
|
changeMux(LOW, LOW, LOW);
|
|
value = analogRead(ANALOG_INPUT); //Value of the sensor connected Option 0 pin of Mux
|
|
delay(200); Serial.println("value 1=" + String(value));
|
|
}
|
|
if (y == 1) {
|
|
changeMux(LOW, LOW, HIGH);
|
|
value = analogRead(ANALOG_INPUT); //Value of the sensor connected Option 1 pin of Mux
|
|
delay(200); Serial.println("value 2=" + String(value));
|
|
}
|
|
// S2 = 0, S1 = 1, S0 = 0, - Z = Y2
|
|
// S2 = 0, S1 = 1, S0 = 1, - Z = Y3
|
|
// S2 = 1, S1 = 0, S0 = 0, - Z = Y4
|
|
// S2 = 1, S1 = 0, S0 = 1, - Z = Y5
|
|
// S2 = 1, S1 = 1, S0 = 0, - Z = Y6
|
|
// S2 = 1, S1 = 1, S0 = 1, - Z = Y7
|
|
|
|
return value;
|
|
}
|
|
|
|
void changeMux(int c, int b, int a) {
|
|
digitalWrite(MUX_A, a);
|
|
digitalWrite(MUX_B, b);
|
|
digitalWrite(MUX_C, c);
|
|
}
|