161 lines
3.4 KiB
C++
161 lines
3.4 KiB
C++
/*
|
|
* Send TeleInfo via HTTP GET request to $host service.
|
|
* Read ERDF TeleInfo when received, then send it to the server.
|
|
* Version 1.0
|
|
*/
|
|
|
|
|
|
/*
|
|
* TODO : Gestion du temps pour n'envoyer que toute les minutes (ou un truc du genre)
|
|
*/
|
|
#include <ESP8266WiFi.h>
|
|
|
|
//----------------------------------------------------
|
|
|
|
const char* ssid = "TNCAP3F2E03";
|
|
const char* password = "73ABCCAA87";
|
|
|
|
//const char* host = "datahouse.kingpenguin.tk";
|
|
const char* host = "192.168.1.143";
|
|
const int httpPort = 8080;
|
|
const int DEFAULT_INTERVAL = 60000; // 60 sec
|
|
|
|
//----------------------------------------------------
|
|
String ADCO = "";
|
|
String OPTARIF = "";
|
|
String BASE = "";
|
|
String HCHP = "";
|
|
String HCHC = "";
|
|
//----------------------------------------------------
|
|
|
|
// Wait for x second.
|
|
void sleepSec(int sec) {
|
|
for (int i=0; i<1000; i++) {
|
|
delay(sec);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
long nextInterval;
|
|
|
|
void sendDataToServer() {
|
|
// Use WiFiClient class to create TCP connections
|
|
WiFiClient client;
|
|
if (!client.connect(host, httpPort)) {
|
|
Serial.print("connection failed on host ");
|
|
Serial.print(host);
|
|
Serial.print(":");
|
|
Serial.println(host);
|
|
return;
|
|
}
|
|
|
|
// We now create a URI for the request
|
|
// TODO a finir
|
|
String url = "/teleinfo/add?ADCO="+ADCO+"&OPTARIF="+OPTARIF+"&BASE="+BASE+"&HCHC="+HCHC+"&HCHP="+HCHP;
|
|
ADCO = "";
|
|
OPTARIF = "";
|
|
BASE = "";
|
|
HCHC = "";
|
|
HCHP = "";
|
|
|
|
Serial.print("Requesting URL: ");
|
|
Serial.println(url);
|
|
|
|
// This will send the request to the server
|
|
client.println("GET "+url+" HTTP/1.1");
|
|
client.println("Host: "+String(host));
|
|
client.println("Connection: keep-alive");
|
|
// client.println("Connection: close");
|
|
client.println();
|
|
delay(10);
|
|
|
|
Serial.println();
|
|
String line = "";
|
|
while (client.available()) {
|
|
char c = client.read();
|
|
line += c;
|
|
}
|
|
Serial.println(line);
|
|
|
|
nextInterval = line.toInt();
|
|
if (nextInterval == 0) {
|
|
nextInterval = DEFAULT_INTERVAL;
|
|
}
|
|
nextInterval = millis() + nextInterval;
|
|
}
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(1200);
|
|
delay(10);
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
|
|
void loop() {
|
|
if (Serial.available() > 0) {
|
|
String str = Serial.readStringUntil('\n');
|
|
if (millis() >= nextInterval) {
|
|
bool sendable = false;
|
|
|
|
int i = str.indexOf("ADCO");
|
|
if (i != -1) {
|
|
i = i + 4 +1;
|
|
ADCO = str.substring(i, i+12);
|
|
}
|
|
|
|
i = str.indexOf("OPTARIF");
|
|
if (i != -1) {
|
|
i = i + 7 +1;
|
|
OPTARIF = str.substring(i, i+4);
|
|
}
|
|
|
|
i = str.indexOf("BASE");
|
|
if (i != -1) {
|
|
i = i + 4 +1;
|
|
BASE = str.substring(i, i+9);
|
|
}
|
|
|
|
i = str.indexOf("HCHC");
|
|
if (i != -1) {
|
|
i = i + 4 +1;
|
|
HCHC = str.substring(i, i+9);
|
|
}
|
|
|
|
i = str.indexOf("HCHP");
|
|
if (i != -1) {
|
|
i = i + 4 +1;
|
|
HCHP = str.substring(i, i+9);
|
|
sendable = true;
|
|
}
|
|
|
|
|
|
if (sendable && ADCO != "") {
|
|
sendDataToServer();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|