The use of the ESP8266 in the world of IoT

User avatar
By dinbooo
#61488 Hi,

I have a raspberry pi that is running a php page from where i can control the lights on my house from my smartphone.
Now i want to have a backup solution or even to change the classic switches with a smart solution, ESP8266.
I have 6 light bulbs in my home controlled by rpi via a 8 relay board(i use only 6 relays). When i want a bulb running a string is sent to RPI:
http://ip_address/switch.php?flipsw8=on&sw8=1%0D%0A
In this way switch number 8 is changed from off to on.
Now i want some help from you guys to create a firmware for ESP8266.
As a starting point, i think we can adapt the door status project to accomplish this, but since i am not a programmer and my arduino experience is near to 0 could someone help me? :P

/*
Created by Rui Santos

All the resources for this project:
http://randomnerdtutorials.com/

Based on some ESP8266 code examples
*/

#include <ESP8266WiFi.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* host = "maker.ifttt.com";
const char* apiKey = "YOUR_IFTTT_API_KEY";

int pin = 2;
volatile int state = false;
volatile int flag = false;
const char* door_state = "closed";

unsigned long previousMillis = 0;
const long interval = 3000;

void changeDoorStatus() {

unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

state = !state;
if(state) {
door_state = "opened";
}
else{
door_state = "closed";
}
flag = true;
Serial.println(state);
Serial.println(door_state);
}

}


void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Preparing the Door Status Monitor project...");

pinMode(pin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pin), changeDoorStatus, CHANGE);

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(flag){
Serial.print("connecting to ");
Serial.println(host);

WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

String url = "/trigger/door_status/with/key/";
url += apiKey;

Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state + "\r\n");
flag = false;
}
delay(10);
}

I want to define six inputs, or i think 8 is best, to have some space for further development. When a push button is pressed ESP will try to send this string:
http://ip_address/switch.php?flipsw8=on&sw8=1%0D%0A - light is on
When the same button is pressed a second string will be send:
http://ip_address/switch.php?flipsw8=on&sw8=0%0D%0A - light is off

Now i am thinking about something that should be implemented in my project on RPI, to modify the status of relays to off when outside is light(day) using a gpio pin on RPI and a light sensor. But this is subject of another research.
If someone need details about my setup in order to replicate i will provide everything i have.

Br,