first commit
This commit is contained in:
400
ESP8266_DOMOTICZ_ECRAN_HALL/ESP8266_DOMOTICZ_ECRAN_HALL.ino
Normal file
400
ESP8266_DOMOTICZ_ECRAN_HALL/ESP8266_DOMOTICZ_ECRAN_HALL.ino
Normal file
@@ -0,0 +1,400 @@
|
||||
/////////////////////
|
||||
// Domoticz Classe
|
||||
/////////////////////
|
||||
const uint16_t port = 81;
|
||||
const char * host = "192.168.1.3"; // ip or dns
|
||||
#include "Modules.h"
|
||||
Modules modules;
|
||||
#include "Ecran.h"
|
||||
Ecran * ecran;
|
||||
#include "Graph.h"
|
||||
Graph * graph;
|
||||
#include "Mesure.h"
|
||||
|
||||
|
||||
Ecran e;
|
||||
|
||||
#define NB_MESURES 15
|
||||
#define SLEEP_DELAY 2000 //interval envoi des données
|
||||
|
||||
Mesure mesures[NB_MESURES];
|
||||
|
||||
// domoticz
|
||||
String macID = "";
|
||||
unsigned long milli;
|
||||
|
||||
|
||||
// SOLAIRE
|
||||
//#include <SoftwareSerial.h>
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
ESP8266WiFiMulti WiFiMulti;
|
||||
|
||||
#include "math.h"
|
||||
const int analogIn = A0;
|
||||
int mVperAmp = 185; // 185 pour 5A, use 100 for 20A Module and 66 for 30A Module
|
||||
int RawValue= 0;
|
||||
int ACSoffset = 2500;
|
||||
double mp_offset = 0.040;
|
||||
double Voltage = 0;
|
||||
double Amps = 0;
|
||||
double max_amps = 0;
|
||||
double max_watt = 0;
|
||||
double sum_watt = 0;
|
||||
String start_time = "";
|
||||
long time_from_start = 0;
|
||||
int lectures = 0;
|
||||
double sum_amps = 0;
|
||||
double sum_watts = 0;
|
||||
String IDX_U = "1085"; //idx du capteur virtuels tension
|
||||
String IDX_I = "1086"; //idx du capteur virtuels intensite
|
||||
String IDX_W = "1087"; //idx du cap
|
||||
|
||||
///////////////////////////// TIME
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
WiFiUDP ntpUDP;
|
||||
|
||||
NTPClient timeClient(ntpUDP);
|
||||
|
||||
// Variables to save date and time
|
||||
String formattedDate;
|
||||
String dayStamp;
|
||||
String timeStamp;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
WiFi.setSleepMode(WIFI_NONE_SLEEP);
|
||||
|
||||
pinMode(D0, WAKEUP_PULLUP);
|
||||
/* sda scl */
|
||||
Wire.pins(SDA, SCL);
|
||||
Wire.begin(SDA, SCL);
|
||||
|
||||
// Conversion d'objet en pointeur
|
||||
ecran = &e;
|
||||
|
||||
delay(500);
|
||||
|
||||
// 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());
|
||||
|
||||
delay(500);
|
||||
///////////////////////////
|
||||
|
||||
// macID = domo.generateKey();
|
||||
// ecran->LcdString(0, 0, domo._domoticz);
|
||||
// ecran->LcdChar(0, 10, "ID : ");
|
||||
// ecran->LcdString(40, 10, macID);
|
||||
// ecran->u8g2->sendBuffer();
|
||||
|
||||
pinMode(RELAY_PIN, OUTPUT);
|
||||
digitalWrite(RELAY_PIN, HIGH);
|
||||
|
||||
milli = millis();
|
||||
|
||||
for (int i =0; i < NB_MESURES; i++){
|
||||
mesures[i] = Mesure();
|
||||
}
|
||||
time_from_start = millis();
|
||||
|
||||
// Initialize a NTPClient to get time
|
||||
timeClient.begin();
|
||||
// Set offset time in seconds to adjust for your timezone, for example:
|
||||
// GMT +1 = 3600
|
||||
// GMT +8 = 28800
|
||||
// GMT -1 = -3600
|
||||
// GMT 0 = 0
|
||||
timeClient.setTimeOffset(3600);
|
||||
|
||||
getTime();
|
||||
// start_time = getTime();
|
||||
// if (start_time.length() >=5) {
|
||||
// String h = start_time.substring(0,2);
|
||||
// String m = start_time.substring(2,2);
|
||||
// Serial.println("Time=" + start_time+ " " + h + " " + m);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// SOLAIRE
|
||||
ecran->LcdClear();
|
||||
int i = 0;
|
||||
RawValue = 0;
|
||||
// Somme du courant alternatif pendant 20 ms ==> 50hz
|
||||
// Détermination du max et max pour hauteur de crete
|
||||
int vmin = 1024;
|
||||
int vmax = 0;
|
||||
int max_bcl = 200;
|
||||
for (i = 0; i < max_bcl; i++) {
|
||||
int value = analogRead(analogIn);
|
||||
if (value >= 0) {
|
||||
RawValue += value;
|
||||
vmax = max(value,vmax);
|
||||
vmin = min(value,vmin);
|
||||
} else {
|
||||
i--;
|
||||
}
|
||||
delay(1);
|
||||
|
||||
}
|
||||
RawValue = RawValue / max_bcl;
|
||||
lectures ++;
|
||||
|
||||
Serial.print("rawValue = " );
|
||||
Serial.print(RawValue);
|
||||
Serial.print(" min = " );
|
||||
Serial.print(vmin);
|
||||
Serial.print(" max = " );
|
||||
Serial.print(vmax);
|
||||
|
||||
// Tension continue
|
||||
// why 40 ?
|
||||
// Voltage = (RawValue / 1023.0) * 5000; // Gets you mV
|
||||
// Amps = ((Voltage - ACSoffset) / mVperAmp);
|
||||
|
||||
|
||||
// La valeur maxi * racine carrée de 2 pour obtenir la tension "réelle"
|
||||
// La tension efficace pour l'effet Hall étant réduite d'un facteur 0,707
|
||||
// Voltage = ((vmax - vmin) / 430.0) * 5000;
|
||||
// branchement 5V 0.341 sinon 0.171 et pas de multilplication voltage * 2
|
||||
|
||||
Voltage = (vmax - vmin -28); //1.4142;
|
||||
Amps = max(1.05 * (Voltage / mVperAmp) /*- 0.341*/, 0.0);
|
||||
sum_amps += Amps;
|
||||
|
||||
|
||||
//Amps = max(5.5 * (vmax - vmin) / 473.0 -0.0580, 0.0); // <= pour le bruit
|
||||
max_amps = max(max_amps, Amps);
|
||||
max_watt = max(max_watt, Amps * 220);
|
||||
double watt = Amps * 220;
|
||||
|
||||
int watth = (int) (Amps * 220 * 3600 / (millis() - milli) );
|
||||
// Serial.print(" Raw Value = " ); // shows pre-scaled value
|
||||
// Serial.print(RawValue);
|
||||
Serial.print("\t mV = "); // shows the voltage measured
|
||||
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
|
||||
Serial.print("\t Amps = "); // shows the voltage measured
|
||||
Serial.print(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
|
||||
Serial.print("\t Watt = "); // shows the voltage measured
|
||||
Serial.print(Amps * 220,3);
|
||||
Serial.print("\t WattH = "); // shows the voltage measured
|
||||
Serial.print(watth);
|
||||
Serial.println(millis() - milli);
|
||||
|
||||
ecran->drawRect(1, 1, LCD_X -1, LCD_Y -1);
|
||||
// ecran->drawJauge(10, 24, LCD_X -10, 34, i, "Jauge");
|
||||
ecran->LcdString(0, 5, "T: " + String(Voltage));
|
||||
ecran->LcdString(0,12, "A: " + String(Amps) + " " + max_amps);
|
||||
ecran->LcdString(0,19, "W: " + String(Amps * 220) + " " + max_watt);
|
||||
|
||||
ecran->drawJauge(10, 30, LCD_X -10, 40, 100 * (watt / 600), "Watt");
|
||||
|
||||
ecran->u8g2->sendBuffer();
|
||||
|
||||
//domo.envoieDomoticz(IDX_U,String((vmax - vmin))); // tension
|
||||
if (lectures == NB_MESURES) {
|
||||
Serial.println("Envoi domoticz..." + String(sum_amps / lectures) + " " + String(sum_amps * 220 / lectures) );
|
||||
ecran->LcdString(2,35, "Envoi Domo");
|
||||
envoieDomoticz(IDX_I,String(sum_amps / lectures)); // intensite
|
||||
envoieDomoticz(IDX_W,String(sum_amps * 220 / lectures)+ ";" +String(watth)); // puissance
|
||||
Serial.println("Envoi domoticz effectué.");
|
||||
|
||||
delay(100);
|
||||
milli = millis();
|
||||
long seconds = (milli - time_from_start) / 1000;
|
||||
int hours = seconds / 3600;
|
||||
int minutes = (seconds - hours * 3600) / 60;
|
||||
Serial.println("Envoi domoticz... " + String(hours) + ":" + String(minutes) + " " + seconds);
|
||||
lectures = 0;
|
||||
sum_amps = 0;
|
||||
sum_watts = 0;
|
||||
}
|
||||
|
||||
//Serial.println("Pause de "+String(SLEEP_DELAY_IN_SECONDS)+" secondes...");
|
||||
|
||||
|
||||
|
||||
// ecran->LcdClear();
|
||||
// Graph g (ecran, 0, 0, 84, 48) ;
|
||||
// g.title = "Production";
|
||||
// g.axe_x = "t";
|
||||
// g.axe_y = "W";
|
||||
// g.drawGraph();
|
||||
// ecran->drawRect(1, 1, LCD_X -1, LCD_Y -1);
|
||||
//ecran->u8g2->drawLine(5, y1 - 1, 5, y1 -1);
|
||||
|
||||
delay(SLEEP_DELAY);
|
||||
|
||||
|
||||
// Dark
|
||||
// while (isDark()) {
|
||||
// sleep(30);
|
||||
// }
|
||||
}
|
||||
|
||||
String getTime()
|
||||
{
|
||||
while(!timeClient.update()) {
|
||||
timeClient.forceUpdate();
|
||||
}
|
||||
// The formattedDate comes with the following format:
|
||||
// 2018-05-28T16:00:13Z
|
||||
// We need to extract date and time
|
||||
formattedDate = timeClient.getFormattedTime();
|
||||
Serial.println(formattedDate);
|
||||
|
||||
// Extract date
|
||||
int splitT = formattedDate.indexOf("T");
|
||||
dayStamp = formattedDate.substring(0, splitT);
|
||||
Serial.print("DATE: ");
|
||||
Serial.println(dayStamp);
|
||||
// Extract time
|
||||
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
|
||||
Serial.print("HOUR: ");
|
||||
Serial.println(timeStamp);
|
||||
}
|
||||
|
||||
void envoieDomoticz(String IDX, String Svalue) {
|
||||
|
||||
|
||||
Serial.print("connecting to ");
|
||||
Serial.println(host);
|
||||
|
||||
// Use WiFiClient class to create TCP connections
|
||||
WiFiClient client;
|
||||
|
||||
if (!client.connect(host, port)) {
|
||||
Serial.println("connection failed");
|
||||
Serial.println("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("----------------------------------------------");
|
||||
Serial.println(line);
|
||||
Serial.println("----------------------------------------------");
|
||||
|
||||
//Serial.println("closing connection");
|
||||
client.stop();
|
||||
|
||||
delay(500);
|
||||
}
|
||||
|
||||
void sleep(int sleepTime)
|
||||
{
|
||||
Serial.print("Go to sleep ");
|
||||
Serial.println(sleepTime);
|
||||
delay(20);
|
||||
|
||||
ESP.deepSleep(sleepTime * 1000000L); // Infini
|
||||
|
||||
//sleepWifi();
|
||||
delay(200);
|
||||
}
|
||||
|
||||
boolean isDark()
|
||||
{
|
||||
|
||||
|
||||
String separator = ",";
|
||||
int nb = 0;
|
||||
|
||||
WiFiClient client;
|
||||
|
||||
if (client.connect(host, port)) {
|
||||
Serial.println("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;
|
||||
}
|
||||
}
|
||||
|
||||
String getVariableValue(String idx)
|
||||
{
|
||||
|
||||
|
||||
String separator = ",";
|
||||
int nb = 0;
|
||||
|
||||
WiFiClient client;
|
||||
|
||||
if (client.connect(host, port)) {
|
||||
Serial.println("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("Idx=" + idx + " Value " + ret);
|
||||
|
||||
break;
|
||||
}
|
||||
if (req == "" || nb > 500) {
|
||||
break;
|
||||
}
|
||||
nb++;
|
||||
}
|
||||
client.stop();
|
||||
delay(100);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user