Files
Arduino/ATMEGA_WIFI_ESP2866/ATMEGA_WIFI_ESP2866.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

314 lines
8.0 KiB
C++
Executable File

#include <SoftwareSerial.h>
//#include <Narcoleptic.h>
SoftwareSerial ESP8266(10, 11);
//#include <Adafruit_BMP085.h>
//#include <Wire.h>
String NomduReseauWifi = "Livebox-37cc"; // Garder les guillements
String MotDePasse = "8A6060920A8A86896F770F2C47"; // Garder les guillements
// Time to sleep (in seconds):
const int sleepTimeS = 60;
#define SEND_MESSAGE_DELAY 30000 // Ne pas dépasser 32000 !! Delay in ms between each value's extraction
#define SEND_433_PAUSE 160 // 16 multiple
//#define DEBUG true
// 163 = Radiateur bureau
#define idRadiateurInDomoticz 290
// Radiateur Manon 217
// Radiateur Théo 219
// Radaiteur chambre 218
// Radiateur salon 207 208 209
// EthernetServer server(80);
boolean done = false;
/****************************************************************/
/* INIT */
/****************************************************************/
int baud = 9600;
void setup()
{
#ifdef DEBUG
Serial.begin(baud);
#endif
setupEsp8266();
}
void setupEsp8266() {
ESP8266.begin(115200);
ESP8266.println("AT+RST");
recoitDuESP8266(1000);
ESP8266.println("AT+CIOBAUD=" + baud);
recoitDuESP8266(1000);
ESP8266.begin(baud);
initESP8266();
}
void debugPrint(String m) {
#ifdef DEBUG
Serial.print(m);
#endif
}
void debugPrintln(String m) {
#ifdef DEBUG
Serial.println(m);
#endif
}
/****************************************************************/
/* BOUCLE INFINIE */
/****************************************************************/
void loop() {
// connection();
// envoie("/json.htm?type=devices&rid=" + String(idRadiateurInDomoticz));
//
String s = "/json.htm?type=devices&rid=";
envoie(s + String(2));
envoie(s + String(257));
envoie(s + String(258));
// envoie("/json.htm?type=command&param=udevice&idx=158&svalue=255");
// deconnexion();
debugPrintln("Sleep");
delay(1000);
// 0 minute
//Narcoleptic.delay(SEND_MESSAGE_DELAY);
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// // 1
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// // 2
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// // 3
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// // 4
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// Narcoleptic.delay(SEND_MESSAGE_DELAY);
// 5
delay(10000);
setupEsp8266();
}
void connection() {
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"192.168.0.10\",8080");
recoitDuESP8266(2000);
}
void envoie(String url) {
connection();
int maxLoops = 10;
int currentLoop = 0;
//String url = "/json.htm?type=devices&rid=" + String(idRadiateurInDomoticz);
String cmdGET = "GET " + url + " HTTP/1.1\r\n"
+ "Host: 192.168.0.10\r\nUser-Agent: ESP8266_HTTP_Client\r\nConnection: close\r\n\r\n";
//debugPrintln("***GET url = " + url);
// demande d'envoi
ESP8266.print("AT+CIPSEND=4,");
ESP8266.println(cmdGET.length());
delay(500);
done = ESP8266.find(">");
currentLoop = 0;
while(!done){
delay(500);
done = ESP8266.find(">");
if(currentLoop >= maxLoops){
//debugPrintln(" Attente dépassée");
break;
}
currentLoop++;
//debugPrint(".");
}
// requete
ESP8266.println(cmdGET+"\r\n\r\n");
String ret = recoitDuESP8266WithJson(10000, "status");
debugPrintln("** ret=" + ret);
if (ret.equalsIgnoreCase("On")) {
digitalWrite(3, HIGH);
digitalWrite(13, HIGH);
} else if (ret.equalsIgnoreCase("Off")) {
digitalWrite(3, LOW);
digitalWrite(13, LOW);
}
deconnexion();
}
void deconnexion() {
////////////////////////////////
// sendToDomoticz("288", "15", "225");
///////////////////////////////
ESP8266.println("AT+CIPSTATUS");
recoitDuESP8266(2000);
// Close all connections
ESP8266.println("AT+CIPCLOSE=5");
recoitDuESP8266(2000);
// restart from zero
ESP8266.println("AT");
recoitDuESP8266(2000);
// 4 secondes déjà passées
delay(200);
}
//void sendToDomoticz(String id, String nvalue, String svalue) {
//
// debugPrintln("**Send To Domoticz " + id + " " + nvalue + " " + svalue);
//
// int maxLoops = 5;
// int currentLoop = 0;
// String url = "/json.htm?type=command&param=udevice&idx=" + id + "&nvalue=" + nvalue + "&svalue=" + svalue; // + String(val);
// //String url = "/json.htm?type=command&param=udevice&idx=288&nvalue=10&svalue=220.5"; // + String(val);
// //String url = "/json.htm?type=command&param=getSunRiseSet";
// String cmdGET = "GET " + url + " HTTP/1.1\r\n"+
// "Host: 192.168.0.10\r\nUser-Agent: ESP8266_HTTP_Client\r\nConnection: close\r\n\r\n";
//
//
// ESP8266.print("AT+CIPSEND=4,");
// ESP8266.println(cmdGET.length());
//
// done = ESP8266.find(">");
// currentLoop = 0;
// while(!done){
// delay(500);
// done = ESP8266.find(">");
// if(currentLoop >= maxLoops){
// break;
// }
// currentLoop++;
// }
// ESP8266.println(cmdGET+"\r\n\r\n");
// recoitDuESP8266(5000);
//
//}
/****************************************************************/
/* Fonction qui initialise l'ESP8266 */
/****************************************************************/
void initESP8266()
{
pinMode(13, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(13, LOW);
//debugPrintln("**************** DEBUT DE L'INITIALISATION ***************");
ESP8266.println("AT");
recoitDuESP8266(1000);
ESP8266.println("AT+CWMODE=3");
recoitDuESP8266(1000);
ESP8266.println("AT+CWJAP=\""+ NomduReseauWifi + "\",\"" + MotDePasse +"\"");
recoitDuESP8266(10000);
ESP8266.println("AT+CIFSR");
recoitDuESP8266(1000);
ESP8266.println("AT+CIPMUX=1");
recoitDuESP8266(1000);
ESP8266.println("AT+CIPSERVER=1,80");
recoitDuESP8266(1000);
//debugPrintln("***************** INITIALISATION TERMINEE ****************");
}
/****************************************************************/
/*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/
/****************************************************************/
String recoitDuESP8266(const int timeout)
{
String reponse = "";
long int time = millis();
while( (time+timeout) > millis())
{
while(ESP8266.available())
{
//reponse = "";
char c = ESP8266.read();
reponse+=c;
if (c == 13 || reponse.length() > 100) {
//debugPrint(reponse);
reponse = "";
}
}
}
//debugPrint(reponse);
return reponse;
}
/****************************************************************/
/*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/
/****************************************************************/
String recoitDuESP8266WithJson(const int timeout, String jsonId)
{
String ret = "";
String reponse = "";
boolean found = false;
long int time = millis();
while( (time+timeout) > millis())
{
while(ESP8266.available())
{
//reponse = "";
char c = ESP8266.read();
reponse+=c;
//reponse.trim();
// if (reponse.equalsIgnoreCase(jsonId)) {
// debugPrintln("Trouve" + reponse + " " + jsonId);
// }
if (c == 10) {
reponse.trim();
// debugPrint(reponse);
int p = reponse.indexOf(':');
String id = reponse.substring(0,p);
String value = reponse.substring(p + 1, reponse.length() -1);
id.trim();
value.trim();
id.replace("\"", "");
value.replace("\"", "");
if (id.equalsIgnoreCase(jsonId) && !found) {
//debugPrintln("========> Trouve " + jsonId + " == " + value);
ret = value;
found = true;
} else {
//debugPrintln(id + " " + value);
}
reponse = "";
}
}
}
//debugPrintln(reponse);
return ret;
}