first commit
This commit is contained in:
156
ESP8266_SCT013/ESP8266_SCT013.ino
Normal file
156
ESP8266_SCT013/ESP8266_SCT013.ino
Normal file
@@ -0,0 +1,156 @@
|
||||
// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
|
||||
|
||||
#include "EmonLib.h" // Include Emon Library
|
||||
EnergyMonitor emon_conso; // Create an instance
|
||||
EnergyMonitor emon_solar;
|
||||
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
ESP8266WiFiMulti WiFiMulti;
|
||||
|
||||
#define NB_MESURES 50
|
||||
|
||||
const uint16_t port = 81;
|
||||
const char * host = "192.168.1.3"; // ip or dns
|
||||
|
||||
int lectures = -10;
|
||||
double sum_conso = 0;
|
||||
double sum_solar = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
emon_conso.current(0, 60); // Current: input pin, calibration.
|
||||
emon_solar.current(1, 60);
|
||||
|
||||
// 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);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
double Irms_conso = emon_conso.calcIrms(1480); // Calculate Irms_conso only
|
||||
double Irms_solar = emon_solar.calcIrms(1480); // Calculate Irms_conso only
|
||||
|
||||
lectures ++;
|
||||
|
||||
Serial.print(Irms_conso*230.0); // Apparent power
|
||||
Serial.print(" ");
|
||||
Serial.println(Irms_conso); // Irms_conso
|
||||
|
||||
Serial.print(Irms_solar*230.0); // Apparent power
|
||||
Serial.print(" ");
|
||||
Serial.println(Irms_solar);
|
||||
|
||||
delay(10);
|
||||
if (lectures > 0) {
|
||||
sum_conso += Irms_conso;
|
||||
sum_solar += Irms_solar;
|
||||
|
||||
if (lectures == NB_MESURES) {
|
||||
envoieDomoticz("1053",String(sum_conso / lectures)); // intensite
|
||||
delay(500);
|
||||
envoieDomoticz("1086",String(sum_solar / lectures)); // intensite
|
||||
|
||||
lectures = -10;
|
||||
sum_conso = 0;
|
||||
delay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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