Skip to content

Instantly share code, notes, and snippets.

@autoiue
Created December 21, 2016 17:35
Show Gist options
  • Save autoiue/d88a4ae3a0f86538ab20589a2d83a745 to your computer and use it in GitHub Desktop.
Save autoiue/d88a4ae3a0f86538ab20589a2d83a745 to your computer and use it in GitHub Desktop.
using ESP8266 AT commands with Arduino
// #define CADREID 2
// #define VOLTAGE 4.95f
// #define SENSOR_BOX A3
// #define SENSOR_EXT A1
// #define SENSOR_LED A2
#define CADREID 1 // Identifiant device
#define VOLTAGE 5.01f // Voltage to calibrate temp sensor
#define SENSOR_BOX A3 // Sensors wiring
#define SENSOR_EXT A2
#define SENSOR_LED A1
#define RELAY_POWER 10 // Relaies wiring
#define RELAY_FAN 11
#define LIGHT_WIFI 13 // Feedback wiring
#define LIGHT_INTT 12
#define OFFSET 0.5f // Temp sensor offset
#define SSID_NB 3L // number of known ssids
#define SEND_IP "max.multoo.eu" // ip to send reports
#define SEND_INTV 10000L // sending interval in ms
#define BAUDRATE 115200L // baudrate of the serial port to communicate with the ESP8266
String SSIDS[] = {"Apple Store", "57", "JUDOKA"}; // SSIDs
String PASSS[] = {"", "0663045757", "FURAX"}; // passwords
bool connected = false; // are connected the te wifi ?
bool internet = false; // are we connected to the internet ?
long nextSend = 0; // next epoch we have to send data
bool commandPower = false; // do we turn on the power ?
bool commandFan = true; // do we turn on the fans ?
String lastRead = ""; // string to old commands
void setup(){
Serial.begin(BAUDRATE); // begin comm with esp
pinMode(LIGHT_INTT, OUTPUT); // setting up pins
pinMode(LIGHT_WIFI, OUTPUT);
digitalWrite(LIGHT_INTT, LOW);
digitalWrite(LIGHT_WIFI, LOW);
pinMode(RELAY_FAN, OUTPUT);
pinMode(RELAY_POWER, OUTPUT);
digitalWrite(RELAY_FAN, commandFan);
digitalWrite(RELAY_POWER, commandPower);
pinMode(SENSOR_BOX, INPUT);
pinMode(SENSOR_EXT, INPUT);
pinMode(SENSOR_LED, INPUT);
waitForStartup(); // wait for the ESP to startup
if(waitFor("GOT IP", 10000)){ // wait for the ESP to automatically connect using its default settings
connected = true;
Serial.println("ATE1"); // test command
}else{
nextSend = millis() + SEND_INTV;
}
delay(300);
// connecté !
}
void loop(){
// read sensors
float box = ((float)analogRead(SENSOR_BOX) * (float)VOLTAGE / 1024.0 - (float)OFFSET) * 100.0;
float ext = ((float)analogRead(SENSOR_EXT) * (float)VOLTAGE / 1024.0 - (float)OFFSET) * 100.0;
float led = ((float)analogRead(SENSOR_LED) * (float)VOLTAGE / 1024.0 - (float)OFFSET) * 100.0;
// if we are connected and it's time to send data
if(connected && millis() > nextSend){
delay(1000);
Serial.read();
nextSend = millis() + (long)SEND_INTV;
sendTemp(box, ext, led);
}else if(millis() > nextSend){
// if it's time but we are not connected, look for a wifi
delay(1000);
Serial.read();
lookForWifi();
nextSend = millis() + ((long)SSID_NB*(long)SEND_INTV);
}
// if temp too hot, cut power and turn on fans
if((int)box >= 69){
digitalWrite(RELAY_POWER, HIGH);
digitalWrite(RELAY_FAN, HIGH);
}
// if temp near limit, blink led and turn on fans
if((int)box >= 50){
digitalWrite(LIGHT_INTT, (millis() % 300 > 150));
digitalWrite(RELAY_FAN, HIGH);
}else{
// else, take command rom the internet
digitalWrite(LIGHT_INTT, HIGH);
digitalWrite(RELAY_POWER, LOW || (internet && commandPower));
digitalWrite(RELAY_FAN, true || (internet && commandFan));
}
}
void lookForWifi(){
char index = 0;
// while we didn't looked at all ssid and we still are not connected to internet
while(index != SSID_NB && !connected){
internet = false;
digitalWrite(LIGHT_WIFI, HIGH);
delay(1000);
// send command to try and ssid with its pass
Serial.print("AT+CWJAP_CUR=\"");
Serial.print(SSIDS[index]);
Serial.print("\",\"");
Serial.print(PASSS[index]);
Serial.println("\"");
Serial.flush();
blink();
blink();
// wait for the ESP to respond with an IP or timeout (20s)
connected = waitFor("GOT IP", 20000);
delay(100);
// try next ssid
index ++;
}
blink();
}
void waitForStartup(){
// if we don't find the ready word in serial feed, reset the esp
while(!Serial.find("ready")){
Serial.println("AT+RST");
blink();
}
}
bool AT(String command, char* expect){
// send any AT command and return bool if expected response found in serial feed
Serial.println(command);
Serial.flush();
if(Serial.find(expect)){
return true;
}else{
return false;
}
}
// make a very basic HTTP GET request with URLEncoded parameters
void HTTPGET(String host, String url){
String req = "GET " + url + "\n\n";
Serial.read();
// open tcp socket on 'host' on port 1156
if(AT("AT+CIPSTART=\"TCP\",\""+ host +"\",1156", "ERROR")){
blink();
connected = false;
// if something went wrong, close socket and force reconnection
Serial.println("AT+CIPCLOSE");
return;
}else{
}
// send request trough socket
if(AT("AT+CIPSEND=" + String(req.length()), "ERROR")){
blink();
connected = false;
Serial.println("AT+CIPCLOSE");
return;
}
Serial.print(req);
// wait for http response and read commands from the internet
if(waitFor("success", 15000)){
internet = true;
commandPower = lastRead.indexOf("PWOFF") != -1;
commandFan = lastRead.indexOf("FANON") != -1;
delay(1000);
}else{
// if something went wrong, same deal as above
internet = false;
blink();
Serial.println("AT+CIPCLOSE");
blink();
Serial.read();
}
}
// blocking blink
void blink(){
delay(20);
digitalWrite(LIGHT_WIFI, HIGH);
delay(20);
digitalWrite(LIGHT_WIFI, LOW);
delay(20);
digitalWrite(LIGHT_WIFI, HIGH);
delay(20);
digitalWrite(LIGHT_WIFI, LOW);
}
// wait for and string in seria feed
bool waitFor(String trigger, long timeout){
long timeoutTime = millis() + timeout;
lastRead = "";
while(millis() < timeoutTime){
if(Serial.available() != 0){
String incoming = Serial.readString();
lastRead += incoming;
if(incoming.indexOf(trigger) != -1){
return true;
}
}
}
return false;
}
// send data with an urlencoded string
void sendTemp(float box, float ext, float led){
digitalWrite(LIGHT_WIFI, HIGH);
HTTPGET(SEND_IP, "/input/"+ String(CADREID) +"/"+ String(box,2) + "/"+ String(ext,2) + "/"+ String(led,2) + "/0.01");
digitalWrite(LIGHT_WIFI, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment