IoT Project - Controlling an Electric door and a Water heater from anywhere in the world using ESP8266 and Arduino

Hello folks!
I’ve used the esp-01 and Arduino Nano to control an Electric door and a Water heater from internet, using the Thinger platform. The Android application is used to turn ON/OFF the Water heater and to hold open the Electric door for 2 seconds.

This can be done with esp-01 itself and 2 transistors, without Arduino, but I’m planing to upgrade the project and add 2 more devices on it using RF 433mhz modules to communicate to the other room.

https://www.youtube.com/watch?v=B7YCGIZ6iuY

Components used

  • Arduino Nano
  • esp8266 wifi module
  • 2 channel relay
  • 1 channel relay
  • 400 point breadboard
  • breadboard power supply

esp8266 code

First I’ve created a device called “wifi” on Thinger and then uploaded the code to the esp-01:

bojler = water heater
vrata = electric door

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#define USERNAME "username"
#define DEVICE_ID "deviceid"
#define DEVICE_CREDENTIAL "devicecredentials"
#define SSID "networkName"
#define SSID_PASSWORD "networkPassword"
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  thing.add_wifi(SSID, SSID_PASSWORD);
  // resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["bojler"] << [](pson& in){ digitalWrite(0, in ? HIGH : LOW); };
  // resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["vrata"] << [](pson& in){ digitalWrite(2, in ? HIGH : LOW); };
}
void loop() {
  thing.handle();
}

Arduino code

then uploaded the code to the Arduino:

> int bojler = 2;
> int vrata = 3;
> int sostojba = 0;

> void setup() {
>   // initialize serial communication at 9600 bits per second:
>   Serial.begin(9600);
>   pinMode(bojler, OUTPUT);
>   pinMode(vrata, OUTPUT);
> }

> // the loop routine runs over and over again forever:
> void loop() {
>     digitalWrite(vrata, LOW);
>     int pom1 = analogRead(A0);
>     int pom2 = analogRead(A1);
>     float bojlerV = pom1 * (5.0 / 1023.0);
>     float vrataV = pom2 * (5.0 / 1023.0);

>     
>     if (bojlerV<2)
>     {
>       digitalWrite(bojler, HIGH);
>     }
>     else if (bojlerV>3)
>     {
>       digitalWrite(bojler, LOW);
>     }
>     
>     if (vrataV<2)
>     {
>       sostojba = 0;
>     }
>     else if (vrataV>3)
>     {
>       sostojba++;
>     }      
>     if (sostojba==10)
>     {
>       sostojba = 2;
>     }
>     if (sostojba==1)
>     {
>       digitalWrite(vrata, HIGH);
>       delay(2000);
>       digitalWrite(vrata, LOW);
>     }
>     
>     delay(250);
>     Serial.print("Bojler napon: ");
>     Serial.println(bojlerV);
>     Serial.print("Vrata napon: ");
>     Serial.println(vrataV);
>     Serial.print("Sostojba: ");
>     Serial.println(sostojba);
> }

And the wiring looks like this:

Problem

Everything works fine, but there is a little problem.
When I open the Android app the values of the two GPIO reset to LOW, if my heater is turned ON and someone opens the Android app, the heater will turn OFF. I think it is because this part of the code:

  // resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["bojler"] << [](pson& in){ digitalWrite(0, in ? HIGH : LOW); };
 
  // resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["vrata"] << [](pson& in){ digitalWrite(2, in ? HIGH : LOW); };

Now I need some suggestions, how to make to keep the old value i.e. when I open the app the current state to be shown, not to reset to LOW.

1 Like

Thanks for the sharing @markoski!! :grinning:

The project looks so cool! It was so nice to see how do you open the electric door with your mobile phone. Looking forward to your project update using RF devices to control other devices. This is quite interesting.

Regarding to the problem related with the GPIO reset when you interact with the API interface…, you can place the following code to avoid the reset:

thing["bojler"] << [](pson& in){ 
  if(in.is_empty()) in = (bool) digitalRead(0);
  else digitalWrite(0, in ? HIGH : LOW); 
};

thing["vrata"] << [](pson& in){ 
  if(in.is_empty()) in = (bool) digitalRead(2);
  else digitalWrite(2, in ? HIGH : LOW); 
};

This way, when there is no input data (it happens when we fetch the device api), we can return the current value making a digitalRead over the pin we are controlling, otherwise we make a digitalWrite with the supplied input. Maybe it has sense for the bojler but not for the vrata (this is your choice :smile:). So with this code you should see how the GPIO state does not change when you update the device API or use another device.

Let me know if it works!

1 Like

Thank you about the nice words, about the project! I’ve applied the changes in the code and now it works perfect.

Now I have other question. How to modify this code

thing["bojler"] << [](pson& in){ digitalWrite(0, in ? HIGH : LOW); };

so to read the pin as input, to show LOW or HIGH, or maybe 0-255 like PWM? I’ve read somewhere that esp-01 have i PWN pin.

I am not sure If I understand correctly what you want to do. Maybe you want to only read the state without changing the value? It should be something like the following code. Notice the operator >> instead of <<.

thing["bojler"] >> [](pson& out){ out = (bool) digitalRead(0);};

Yes, to read a value. For example, to use 1 pin for turning something ON/OFF and the other pin to read something LOW/HIGH. I’ll try this and inform you.

Ok! then this will work fine!

You can read any value… ON/OFF, integers, floats like temperatures, etc. Take a look to this project example where it is read the DHT11 sensor: Arduino Ethernet + DHT11

1 Like

Hello, thanks for posting the project, it’s the best example of the Internet I’ve found.

After a lot of searching I’m looking for some help specifically with the arduino nano and the esp01

My question are:
Can the esp01 be used to control all the outputs on the nano or is it limited to just two outputs?

Does the esp01 need to be coded separately to the nano or can the nano take control and just use the esp01

I was thinking that if the esp01 could output using pwm the nano could read the variable and use this to control the other nano pins?

Thanks for reading. I’ve been having real trouble with my research.

Thanks and Happy Christmas. Liam