208 lines
4.6 KiB
C++
Executable File
208 lines
4.6 KiB
C++
Executable File
// esp8266_test.ino
|
|
//
|
|
// Plot LM35 data on thingspeak.com using an Arduino and an ESP8266 WiFi
|
|
// module.
|
|
#include <SoftwareSerial.h>
|
|
#include <stdlib.h>
|
|
|
|
#define DEBUG true
|
|
String NomduReseauWifi = "Livebox-37cc"; // Garder les guillements
|
|
|
|
String MotDePasse = "8A6060920A8A86896F770F2C47"; // Garder les guillements
|
|
|
|
// LED
|
|
int ledPin = 13;
|
|
// LM35 analog input
|
|
int lm35Pin = A0;
|
|
|
|
// replace with your channel's thingspeak API key
|
|
String apiKey = "ZP1PZO62773HXWVU";
|
|
|
|
// connect 10 to TX of Serial USB
|
|
// connect 11 to RX of serial USB
|
|
SoftwareSerial ESP8266(10, 11); // RX, TX
|
|
|
|
// this runs once
|
|
void setup() {
|
|
|
|
#ifdef DEBUG
|
|
Serial.begin(9600);
|
|
#endif
|
|
// initialize the digital pin as an output.
|
|
pinMode(ledPin, OUTPUT);
|
|
|
|
ESP8266.begin(115200);
|
|
|
|
envoieAuESP8266("AT+RST");
|
|
recoitDuESP8266(2000);
|
|
|
|
envoieAuESP8266("AT+CIOBAUD=9600");
|
|
recoitDuESP8266(2000);
|
|
|
|
|
|
ESP8266.begin(9600);
|
|
|
|
|
|
}
|
|
// the loop
|
|
void loop() {
|
|
initESP8266();
|
|
|
|
// blink LED on board
|
|
digitalWrite(ledPin, HIGH);
|
|
delay(200);
|
|
digitalWrite(ledPin, LOW);
|
|
|
|
// read the value from LM35.
|
|
// read 10 values for averaging.
|
|
int val = 0;
|
|
// for(int i = 0; i < 10; i++) {
|
|
// val += analogRead(lm35Pin);
|
|
// delay(500);
|
|
// }
|
|
|
|
// convert to temp:
|
|
// temp value is in 0-1023 range
|
|
// LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
|
|
// So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
|
|
float temp = val*50.0f/1023.0f;
|
|
|
|
// convert to string
|
|
char buf[16];
|
|
String strTemp = dtostrf(temp, 4, 1, buf);
|
|
|
|
Serial.println("Temp " + strTemp);
|
|
|
|
// TCP connection
|
|
debugPrintln("***************** GET ******************************");
|
|
String cmd = "AT+CIPSTART=4,\"TCP\",\"192.168.0.10\",8080";
|
|
envoieAuESP8266(cmd);
|
|
recoitDuESP8266(2000);
|
|
|
|
// if(ESP8266.find("Error")){
|
|
// Serial.println("AT+CIPSTART error");
|
|
// return;
|
|
// }
|
|
|
|
// prepare GET string
|
|
// String cmdGet = "GET /update?api_key=";
|
|
// cmdGet += apiKey;
|
|
// cmdGet +="&field1=";
|
|
// cmdGet += String(strTemp);
|
|
// cmdGet += "\r\n\r\n";
|
|
|
|
String url = "/json.htm?type=command¶m=udevice&idx=158&svalue=255";
|
|
|
|
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("***************Commande GET **********************");
|
|
|
|
// demande d'envoi
|
|
int maxLoops = 10;
|
|
int currentLoop = 0;
|
|
boolean done = false;
|
|
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(".");
|
|
}
|
|
envoieAuESP8266(cmdGet + "\r\n\r\n");
|
|
recoitDuESP8266(8000);
|
|
|
|
// // send data length
|
|
// cmd = "AT+CIPSEND=";
|
|
// cmd += String(cmdGet.length());
|
|
// ESP8266.println(cmd);
|
|
//
|
|
// if(ESP8266.find(">")){
|
|
// ESP8266.print(cmdGet);
|
|
// }
|
|
// else{
|
|
// ESP8266.println("AT+CIPCLOSE");
|
|
// // alert user
|
|
// Serial.println("AT+CIPCLOSE");
|
|
// }
|
|
|
|
// thingspeak needs 15 sec delay between updates
|
|
delay(16000);
|
|
}
|
|
|
|
|
|
void initESP8266()
|
|
{
|
|
|
|
debugPrintln("**************** DEBUT DE L'INITIALISATION ***************");
|
|
|
|
envoieAuESP8266("AT");
|
|
recoitDuESP8266(1000);
|
|
|
|
envoieAuESP8266("AT+CWMODE=3");
|
|
recoitDuESP8266(1000);
|
|
|
|
envoieAuESP8266("AT+CWJAP=\"" + NomduReseauWifi + "\",\"" + MotDePasse + "\"");
|
|
recoitDuESP8266(10000);
|
|
|
|
envoieAuESP8266("AT+CIFSR");
|
|
recoitDuESP8266(1000);
|
|
|
|
envoieAuESP8266("AT+CIPMUX=1");
|
|
recoitDuESP8266(1000);
|
|
|
|
envoieAuESP8266("AT+CIPSERVER=1,80");
|
|
recoitDuESP8266(1000);
|
|
|
|
debugPrintln("***************** INITIALISATION TERMINEE ****************");
|
|
|
|
debugPrintln("");
|
|
}
|
|
/****************************************************************/
|
|
/* Fonction qui envoie une commande à l'ESP8266 */
|
|
/****************************************************************/
|
|
void envoieAuESP8266(String commande)
|
|
{
|
|
// debugPrintln(commande);
|
|
ESP8266.println(commande);
|
|
}
|
|
/****************************************************************/
|
|
/*Fonction qui lit et affiche les messages envoyés par l'ESP8266*/
|
|
/****************************************************************/
|
|
void recoitDuESP8266(const int timeout)
|
|
{
|
|
String reponse = "";
|
|
long int time = millis();
|
|
while ( (time + timeout) > millis())
|
|
{
|
|
while (ESP8266.available())
|
|
{
|
|
char c = ESP8266.read();
|
|
reponse += c;
|
|
}
|
|
}
|
|
debugPrintln(reponse);
|
|
}
|
|
|
|
void debugPrint(String m) {
|
|
#ifdef DEBUG
|
|
Serial.print(m);
|
|
#endif
|
|
|
|
}
|
|
void debugPrintln(String m) {
|
|
#ifdef DEBUG
|
|
Serial.println(m);
|
|
#endif
|
|
|
|
}
|