loading

A major component of or rather the very concept, Internet of Things, is about how to connect various devices to the network so that they can both send data and receive commands. Various technologies to address the last mile connectivity, such as bluetooth, wifi, NFC, etc... already exist, but most of these are complicated to deploy and often need additional hardware such as a local control server or appliance.

In this instructable, I show you how to build and configure a simple standalone board that with the help of a wifi network can send environmental data to the internet and receive control commands to turn on/off a switch. This project, which can be built for less than 10 or 15 US$ does the following:

- Send temperature/humidity readings from a DHT11 sensor to a MQTT broker and a Thingspeak channel.

- Listen for MQTT messages and turn on or off a relay connected to ESP8266.

- Check a Thingspeak channel periodically and turn on/off a relay if the field has been updated.

In simple terms, this is a internet enabled temperature/humidity sensor and relay. The relay acts a two-way switch and can be used to turn on/off any AC appliance connected to it. The parts needed cost only less than 15 US$ and absolutely no other hardware is needed. The software used is all open source and only free online services have been used to connect the board to the internet. It is assumed there is a working wifi network available for the board to connect to.

Although a few instructables that show you how to configure a ESP8266 module to push data to the internet already exist, I have not seen a solution which also lets you control a device attached to the ESP8266 module by receiving commands from the internet. This instructable shows ways to do that easily.

Step 1: Basics

What is ESP8266?

ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.

https://espressif.com/en/products/esp8266/.

http://mcuoneclipse.com/2014/10/15/cheap-and-simpl...

https://scargill.wordpress.com/?s=esp8266

What is Thingspeak?

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios.

http://mqtt.org/

http://en.wikipedia.org/wiki/MQTT

What is Thingspeak?

ThingSpeak is an open source “Internet of Things” application and API to store and retrieve data from things using HTTP over the Internet or via a Local Area Network. With ThingSpeak, you can create sensor logging applications, location tracking applications, and a social network of things with status updates.

https://thingspeak.com/

Step 2: Parts Needed

1. ESP8266 ESP-01 module

2. DHT11 or DHT22 sensor

3. 5v relay

4. LM1117 3.3v LDO voltage regulator

5. PN2222A transistor

6. 1 x 470 uF capacitor

7. 2 x 10 uF Tantalum capacitor

8. 2 x 1K resistor

9. 1 LED

10. male breakaway headers

11. PCB Board

12. FTDI USB to TTL adapter cable

The ESP8266 module, the DHT11/22 sensor and the USB to TTL cable can be ordered from ebay.com and all three of them together should not cost more than $9.00. And the rest of the components can be bought from any electronic shop for a couple of dollars.

A working wifi access point and its login details should be available for the ESP8266 module to connect to.

Step 3: Circuit

The circuit is pretty simple.

The board can be powered either by just the USB cable or through a 5v DC supply connected to the power socket at the top. A LM1117-3.3 LDO is used to get 3.3v needed for the ESP8266 module. A DHT11 sensor is connected to GPIO2 pin of ESP8266.

The GPIO0 pin can be connected to the ground or relay based on the jumper position.

Step 4: Accounts on Cloudmqtt.com and Thingspeak.com

The board pushes temperature and humidity readings as well as the relay state to:

  • MQTT broker: Any MQTT (http://mqtt.org/) broker can be used. In my case, I have used a free online broker at www.cloudmqtt.com. You would need to create an account as well as an instance. And enter the instance details in user_config.h. An advantage of using an online broker is that it can be accessed from any network. If a local broker is used (for example, running on a local Raspberry Pi box), then it may be difficult to access it from outside the home network since incoming ports are usually blocked by ISPs.
  • Thingspeak.com: An account needs to be created on Thingspeak.com. Additionally, a channel with these three channels also needs to be created and its details entered in user_main.c:
    • field1 - relay state
    • field2 - temperature
    • field3 - humidity

Although in its present form, the board needs both cloudmqtt and Thingspeak connectivity for all functions to work properly, the code can be easily tweaked to use only one of these options.

Step 5: Code

The code is available at:

https://github.com/tangophi/esp_mqtt

Before compiling:

<p>	#define MQTT_HOST	"your cloudmqtt instance name" <br>	#define MQTT_PORT	your cloudmqtt instance port</p>
	#define MQTT_USER	"your cloudmqtt instance username"
	#define MQTT_PASS	"your cloudmqtt instance password"
	#define STA_SSID          "your WiFi SSID"
	#define STA_PASS         "your WiFi password"
  • Update following two global variables in user_main.c to your values:
	char YOUR_THINGSPEAK_API_KEY[]= "xxxxxxxxxxxxxxxxx";

	char YOUR_THINGSPEAK_CHANNEL[]= "xxxxxxxxx";
  • Build target 'all'.

Step 6: Flashing the Firmware to the Module

* Connect GPIO0 to ground by putting the jumper in the proper position as shown in the figure.

* Disconnect and reconnect the USB cable to reset the ESP8266 module.

* Flash the firmware.

Step 7: Running the Board

  • Remove the jumper.
  • Power the board using either the USB cable or a 5v DC power supply. Make sure the power to the ESP8266 module is reset after the jumper is removed.
  • After a couple of seconds, put the jumper in a position to connect GPIO0 to the relay.
  • Open CoolTerm or RealTerm window and connect to the correct COM port. An output like the following indicates everything is running fine.

WIFI_INIT

MQTT_InitConnection

MQTT_InitClient

System started ...

mode : sta(18:fe:34:9d:f5:36)

add if0

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

scandone

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

STATION_IDLE

add 0

aid 4

pm open phy_2,type:2 0 0

cnt

connected with dlink, channel 2

dhcp client start...

STATION_IDLE

STATION_IDLE

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

STATION_IDLE

STATION_IDLE

STATION_IDLE

STATION_IDLE

ip:192.168.0.104,mask:255.255.255.0,gw:192.168.0.1

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

TCP: Connect to domain m11.cloudmqtt.com:19449

DNS: found ip 107.22.157.224

TCP: connecting...

MQTT: Connected to broker m11.cloudmqtt.com:19449

MQTT: Sending, type: 1, id: 0000

TCP: Sent

TCP: data received 4 bytes

MQTT: Connected to m11.cloudmqtt.com:19449

MQTT: Connected

MQTT: queue subscribe, topic"/esp8266/temperature", id: 1

MQTT: queue subscribe, topic"/esp8266/humidity", id: 2

MQTT: queue subscribe, topic"/esp8266/relay", id: 3

MQTT: Sending, type: 8, id: 0001

TCP: Sent

TCP: data received 5 bytes

MQTT: Subscribe successful

MQTT: Sending, type: 8, id: 0002

TCP: Sent

TCP: data received 5 bytes

MQTT: Subscribe successful

MQTT: Sending, type: 8, id: 0003

TCP: Sent

TCP: data received 5 bytes

MQTT: Subscribe successful

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

Temperature: 27.0 *C, Humidity: 35.0 %

MQTT: queuing publish, length: 28...

MQTT: queuing publish, length: 25...

MQTT: Sending, type: 3, id: 0000

TCP: Sent

MQTT: Published

TCP: data received 28 bytes

MQTT topic: /esp8266/temperature, data: 27.0

MQTT: Sending, type: 3, id: 0000

TCP: Sent

MQTT: Published

TCP: data received 25 bytes

MQTT topic: /esp8266/humidity, data: 35.0

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

In http_post_callback... http_status=200

response===82===

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

In http_get_relay_state_callback... http_status=200

response===3

1.0

0

===

Sending on to /esp8266/relay...

MQTT: queuing publish, length: 20...

MQTT: Sending, type: 3, id: 0000

TCP: Sent

MQTT: Published

TCP: data received 20 bytes

MQTT topic: /esp8266/relay, data: on

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

In http_post_callback... http_status=200

response===0===

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

Step 8: View and Control Through Thingspeak.com

View

The temperature/humidity readings as well as the relay state can be seen at

https://thingspeak.com/channels/21370

Control

For switching on the relay, the field1 of the Thingspeak channel should be updated to 1.0 using the following URL:

https://api.thingspeak.com/update?key=YF2DC4HXFSUQ...

Simply copy/paste the above URL in a browser window. If the response returned is a non-zero integer, it means that the field has been updated correctly. If a zero is returned, then try copy/paste the URL again till you get a non zero integer.

For switching off the relay, the field1 of the Thingspeak channel should be updated to -1.0 using the following URL:

https://api.thingspeak.com/update?key=YF2DC4HXFSUQ...

Try it out yourselves !!!!

The board is online and you are welcome to turn on/off the switch by sending commands using the above URLs. I have connected a low power AC bulb to the relay and it will turn on or off :)

Step 9: View and Control Using MQTT

View

Any MQTT client can be used to connect to the cloudmqtt broker to display the messages published by the board. I have used a Chrome App called MQTTlens.

Control

A message can be 'published' to the /esp8266/relay topic using a MQTT client to turn on/off the relay.

Step 10: Issues

There is a small issue in the circuit. If the board is powered on with the jumper connecting GPIO0 to the relay, then the relay switches on immediately, but the board does not boot up properly. I suspect that the relay may be momentarily drawing too much current.

Workaround

A workaround for this issue is to power on the board with the jumper removed. After a couple of seconds, the jumper can be inserted to connect GPIO0 to the relay and everything works fine after that.

Proper solution

1. Instructable member JadinA2A suggested a 10K pull up resistor between GPIO0 and Vcc can be used to fix this issue.

2. Another suggestion was to use a zener diode beween GPIO0 and Vcc to fix this issue.

3. I guess a inverter can be also be used between GPIO0 and the relay. So initially GPIO0 is HIGH and hence the relay should not turn on automatically on power up and the ESP8266 module should boot up fine. Later on to turn on the relay, GPIO0 can be made LOW and to turn off the relay it can be made HIGH.

Step 11: Next Steps and Conclusion

In this project, we have seen how a standalone ESP8266 module can be used to talk to the Internet to publish data as well as to receive commands. A ESP-01 module was used which has two GPIO ports only.

Instead of a ESP-01 module, if a ESP-03 module is used, then more sensors can be attached. However, there are still only 6 GPIO ports on ESP-03 modules. ESP-12 has 8 GPIO ports.

Anyway, a ESP-01 module can be used in conjunction with an Arduino board to connect more sensors and even a display. The ESP-01 module should still have the standalone firmware such as the one used in this project (and not the AT firmware) and can talk to the Arduino through the UART port.

Plans for next project include having a ESP8266 module connected to an Arduino with LCD display, motion detector, more relays and other sensors. Also develop an Android app to control the board through thingspeak or mqtt.

All suggestions/comments are welcome. Please feel to use the code/design in this instructable as you may please. If you make something interesting out of this instructable, a message on what it is would be nice :)

<p>Following all of the steps in this Instructable (especially those in: </p><ul><br><li>Configure ESP8266 build environment using steps in<a href="http://signusx.com/esp8266-windows-compilation-tutorial/" rel="nofollow">http://signusx.com/esp8266-windows-compilation-tut...</a> <br><br>) are beyond what you can reasonably expect. To post something like this, I wish you would include steps (rather than providing a link to a post with dozens of steps) so people know what they are getting into when they start the project.<br><br>Your steps made this look like a really easy project, but when you get into all of the steps in the links, it is not easy at all.<br></ul>
<p>I would disagree. My intention with this instructable was not to teach all the basics. Rather i wanted to show how to construct an inexpensive to board to control a light and to post temperature data to the web. I really don't see a point to replicate information that is already widely available. Also, those steps could change and it would be tedious to keep my instructable up to date with those changes.</p><p>I agree the steps may not be easy, especially if you want do how to through the SDK method. I suggest you look at nodeMCU firmware and LUA scripts if you want to try this. Again the steps are easily available if you google for it.</p>
<p>I fully agree, and though it might be a matter of taste, I would even steer away from LUA, but just go with Arduino IDE<br><br>Using MQTT-spy may come in handy to iron out any mqtt problems</p>
<p>One other comment. My current strategy is one computer per application and then use MQTT to connect everything together. That means I will probably not need more pins so I probably won't add an Arduino to the mix. Smaller is better. If I connect two devices I will use one ESP-&lt;??&gt; board for each sensor and/or switch unless they obviously work together closely. Once MQTT is up and reliable that changes the way I think about most of my home projects. MQTT can service completely independent threads in different topics. It is more like LEGO with mix and match parts. I'm running mosquito on my Mac but that will probably move to a Raspberry PI eventually. My next Mac will be a laptop so it won't be available 24/7 like my current Mac.</p>
<p>Nice project. I have been doing something similar which is of course why i like it. Great minds think alike. ;-) I prefer MicroPython on the esp8266 but that is a matter of style. Rather that use an external USB charger I added a 5v power supply which is basically the guts of a USB charger without case or connectors. You can get them for a couple of bucks each on ebay, BangGood and the like. That will be my new standard for powering my projects. I'm tired of a gazillion wall worts all over the house and want everything in one box, especially the wifi switch because 110 was already going to the relay anyway. I didn't want two power cables for a single wifi switch.</p><p>I have my first model working but my strategy has changed. I just discovered the Itead Sonoff product line. They have several versions. I'm using the &quot;relay switch only&quot; version which I got for $5 each. At that price it was 1/3 the cost of all the individual parts I put together and it was already assembled in its own custom box so I didn't have to worry about the risk of dealing with 110V in design or build. If you are impatient you can buy one of these from Amazon for $12 with two day delivery.</p><p>The Sonoff is almost exactly the same as my design: esp8266, relay to control 110V and with the digital power powered internally from the 110v; except it is all on a single small PC board with a nice compact and strong custom case. Of course they have their own software which connects to their own cloud service. I didn't even try it. I immediately downloaded microPython. The PC board has connection points for the ESP8266 serial which gives you complete control. I discovered that there are a bunch of open source projects that support MQTT on the Sonoff. Now that I know how to search for it I notice a bunch of Instructables doing various things with the Sonoff, Of course there is one from Itead which might be biased but there are others.</p><p> The original version I bought didn't have a temperature sensor but the two most recent TH versions do. They'll do humidity as well so the humidifier is another device that will get MQTT control in my house. I haven gotten the new versions yet but I will. At the low cost for the Sonoff, the ability to use open source software and the ability to leave 110V issues in someone else's hands I will no longer be building these myself. I have plenty of other projects to do. It wasn't a waste doing it the hard way first though. I learned a lot about the ESP8266 and MQTT etc. so now I know how to use the Sonoff effectively. I run the same software on both. The only change is the relay is on a different pin.</p>
<p>hello, i m able to follow the steps till 6 successfully but as per the output concern i am getting like:(image- sample.jpg)</p><p>&gt;my esp8266 is not able to connect to WIFI network since error called &quot;fatal exception(29)&quot; is coming eventhough i put the SSID &amp; password correctly.(no problem with the board, works fine for both STATION &amp; ACCESS Point interface)</p><p>&gt;#define MQTT_HOST &quot;m12.cloudmqtt.com&quot; ?? is it correct (see image sample1.jpg)</p><p>&gt;#define MQTT_CLIENT_ID &quot;DVES_%08X&quot; (default) is it correct ??</p><p>&gt;#define MQTT_PORT 10981 ?? (Port??, SSL Port??,Websockets Port (TLS only)?? ) which one ??</p><p>&gt;LOCAL char YOUR_THINGSPEAK_CHANNEL[]= &quot;163381&quot;;(see image- sample2.jpg) is it correct ??</p><p>looking forward for ur support , thanx in advance ;-)</p>
I am not really sure what the error is.<br><br>Btw, I have stopped writing firmware code by using the SDK directly. . Instead i use Arduino IDE with ESP8266 support to code for ESP8266 now. Have you tried that approach? Its a lot simpler than using the SDK directly.
I tried on Arduino IDE and you are Right, its quite simpler and easy to implement.. :-)<br><br>
<p>Hi Marappan. I would like to include a circuit breaker so if a certain voltage is exceeded, it breaks. Any ideas?</p>
<p>Hi,</p><p>Great post.<br>This is a request for guidance.<br>My ESP8266 module connects to &ldquo;test.mosquitto.org&rdquo; and subscribes to a topic say &ldquo;b1&rdquo;. What ever is published to &ldquo;b1&rdquo;, it can read it immediately.<br>Problem happens when my second ESP8266 module subscribes to &ldquo;test.mosquitto.org&rdquo; to &ldquo;b1&rdquo;. It continuously returns :</p><p>WiFi connected<br>IP address:<br>192.168.0.9<br>Attempting MQTT connection&hellip;connected<br>Message arrived [b1] 0<br>Attempting MQTT connection&hellip;connected<br>Message arrived [b1] 0<br>Attempting MQTT connection&hellip;connected<br>Message arrived [b1] 0<br>Attempting MQTT connection&hellip;connected</p><p>Could you kindly suggest what might be going wrong. I look forward to your advice.</p>
<p>Are you using different client IDs from the two modules or the same ID? Using the same IDs may cause such an issue, i think. If thats the case, try using different client IDs for the two modules.</p>
<p>You should have used Talkback api instead of channel fields to send commands to relay .</p>
<p>Anxious to give this a try but am having a hard time following the circuit schematic. Not sure when lines cross on your diagram whether there is a junction or not and it seems some some but not all suggestions in the comments were added to the diagram. Power for DHT11 comes from the relay coil? Is there a better schematic available?</p>
<p>Nice project... One suggestion is to use DIoTY.co as your cloud MQTT server... Added value is that DIoTY comes with a mobile app (android and iOS) where you can see your latest publications and have switches to control your relay...</p><p>@BoX2: DIoTY only supports QoS = 0 and QoS = 1. In my opinion for a project like this, QoS = 0 is sufficient. If you publish sufficiently frequent, missing one reading is not going to make any difference. When controlling a relay, you as an end user will see on your mobile app that the switch went back on/off immediately, and you will do your action again (you will probably even think it was you who didn't touch the screen correctly). QoS = 2, you only need when it makes a real difference when you miss or double send a single message... which is hardly ever the case unless maybe you're making a mobile payment app!</p>
<p>Thanks for getting back! Yes I agree with you as far as this project is concerned. However for my own project, I will need to collect time-series sensor data such as power demand in KW on a 3-second interval. Any missing observation will cause significant issues with modeling and diminished data quality. Further more I will need to sync up multiple sensors in time (e.g. 18:09:02 on one sensor vs. 18:09:04 on the other would be considered out of sync). So at least QoS 1 will be required.</p>
<p>I am wondering has anyone tested QoS = 1 or Qos = 2 on MQTT with this particular implementation. In theory, data integrity should not be affected by power/network outage since both modes rely on local buffers. Also since MQTT is asynchronous, the sensor should still be able to call the publish method and return even under network congestion or outage. Any input would be appreciated!</p>
<p>You can use a capacitor between the GPIO and 3.3v to delay, I did and it works.</p>
<p>I checked out your Google+ page and noticed you have created a Ambilight clone. But you have said that you used raspberrypi + hyperion + ws2812b + Arduino. </p><p>Why do you need the Arduino here? You can connect the WS281b strip directly to the raspberry pi. I have done it that way and it works.</p>
I read only now your reply about Ambilight.. I used an arduino on that moment because the sync between Raspberry GPIO and Led strip was not fast enought. I used serial to arduino with the data then the arduino can update the strip properly.<br>Maybe now it works like you did if I update my project. Thanks
<p>Oh cool. You can also use a switching diode, according to KevinC10.</p><p>Btw, can you please tell us about what project you are working on and also about the interface(s) that you are using to talk to the ESP8266 module. Thanks.</p>
<p>Hi, there is a transistor in the part image and the circuit ... but you do not show the transistor code in the part list... </p>
<p>Added the transistor to the list. Thanks!</p>
<p>Before I start building, there is a question: do I really need a MQTT server, or can I without too many problems tweak the code so I just communicate straight away with my own website?</p><p>Paai</p>
yes. you can tweak the code to use with a website. It all depends on what you want to do. Good luck.
<p>Hi </p><p>Excellent project, i am trying to build this but only have a 32 bit machines, following the tutorial it states that the library will only work on 64 bit machines, does a work around exist for 32 bit machines?</p><p>I have been using Arduino IDE for working on the esp8266 which is very simple to use, is there are reason why you are not using this and building your toolchains yourself?</p><p>Steve</p>
Thanks!<br><br>You should be able to use 32 bit machines as well. Not sure where I have mentioned about it working on 64 bit machines as well.<br><br>When I made this project, Arduino IDE support for programming ESP8266 was not yet available. Hence, I had a choice of using either nodeMCU or building a firmware using the SDK. I chose the latter, because I was having too many issues with nodeMCU.<br><br>Now I think nodeMCU has become stable. You can either try that or use the Arduino IDE. The code is not complicated and I think both nodeMCU and Arduino IDE should work.
<p>Its going well but i think i need a little help. I have copied the user_init part of your code inside the setup() for Arduino, i don't think i need a main loop as the user_init creates a timer loop.</p><p>I am receiving an error &quot;INFO&quot; was not declared in this scope. I can't find where the call for INFO comes from, could i replace this with &quot;Serial.print&quot;?</p>
<p>Thank you for your speedy response. Your right, you didn't mention 64 bit machines in your instructable, i was following the link in step 5 for building the ESP8266 build environment, withing this link for building the toolchain your are required to download &quot;xtensa-lx106-elf-141114.7z&quot; which is only for 64 bit processors.</p><p>I will however try to get the code working within Arduino IDE.</p><p>Much appreciation for your support.</p><p>Steve</p>
<p>Hi, i want to replicate this project.</p><p>I am completely new to home automation just wanted to ask something. Is it ok to use normal capacitors or do we have to use Tantalum capacitors? Tantalum capacitor cost is quite high actually, so any suggestion or workaround will be helpful.</p>
<p>The capacitors used here are just for decoupling. I think it should be okay to use normal capacitors too. Btw, I dont think tantalum capacitors are that costly compared to normal capacitors. Do you know how much more tantalum capacitors cost compared to normal ones?</p>
<p>I may be wrong, but i think the issue is that the relay pulls GPIO0 to low on boot before GPIO0 gets assigned as an output. A Solution may be to use a relay that triggers on low, and pull GPIO0 to high with a small resistor?</p><p>Very nice instructable by the way!</p>
Thanks!<br><br>Regarding the GPIO0 issue, another user has also suggested a zener diode between it and the relay to fix this issue. Have not tried it yet though, but looks promising. So does your suggestion.
<p>I just tested adding a 10k resistor between VCC and GPIO0, it works perfectly. I was getting a random &quot;ON/OFF&quot; every time the esp8266 restarted, now it's disappeared and works like a charm. The only thing you need to do is immediately after you assign GPIO0 as an output, you set it HIGH with gpio.write(0, gpio.HIGH)). My relays are configured to trigger on LOW, which is why this works. I don't have any zener diodes lying around to test that route.</p>
<p>Its good to hear that the pull up workaround fixes the issues.</p><p>Btw, what type of project are you making? I would be interested to know the details as well as a photo :)</p>
<p>I'm busy making a WiFi switch for my hot water geyser at home, although, I have plans to control many circuits with it. I have a simple web server running with a mobile friendly ui thanks to some css styling. I want the geyser to only turn on for 45 minutes and then turn off. This is more than enough time to sufficiently heat the water for a shower. Currently everything is still on the breadboard so it's a bit of a mess. I'll post a photo when I have something a bit more mature for sure! I know (and have been told) that I could simply use a geyser timer to achieve the same result, but, this is more of a learning project than something to solve a real world problem. </p>
<p>Hi, nice idea :) It looks good. I am just wondering if you have a version where you can update data instead of directly in the code, in the wepage of ESP8266 ?</p><p>Many thanks :)</p>
<p>Thanks for your kind words. Regarding &quot;webpage of ESP8266&quot;, I am not sure i follow. Are you talking about a webserver running on ESP8266 which will show the data?</p><p>If thats the case, it can be done. But i have not tried it though. You can check out nodeMCU firmware. I think you can just flash the nodeMCU firmware and write a small LUA script to start a webserver on a ESP8266 modules which will show the latest data. You dont have to write the whole firmware and build it like I have shown in the tutorial.</p><p>Also, note that a lot of things have changed/improved since I wrote this instructable four months back. For instance, you can now install Arduino bootloader on the ESP8266 module and program it similar to how you do with an actual Arduino board. But, i have not tried this method mainly because i am already used to writing my own firmware and hence have not had the inclination/need to try the Arduino method. However, if you are only recently starting out with ESP8266 module, then I recommend checking that option out. From what i have read and understood about the various techniques to program the ESP module, i think Arduino bootloader method may be the quickest/easiest way, assuming of course, that you are already familiar with Arduino programming.</p><p>Hope this helps. </p>
<p>Hi,</p><p>I am trying to follow the project's steps, however when I got to step 5 &quot;the code&quot;</p><p>I completely lost you. I can't really understand what's the app/software I should run this code with. I downloaded everything, but I still don't know how to use it.</p><p>is there a more detailed tutorial or instructions for rookie like me ? </p><p>Thank you &amp; well done for sharing this project. </p><p>Tal</p>
<p>Hi tshalev,</p><p>I am not sure how familiar you are working with microcontrollers (such as Arduino) and stuff. Well you see, at a high level, the ESP8266 module is a simple microcontroller with builtin wifi capabilities.</p><p>In order to make the module what you want, it needs to be programmed. There are two mains ways on how this could be done. </p><p>1. Write your own code. Compile it with the help of the SDK. Build the firmware. Flash the firmware. I agree this method is quite complex for new comers. You need to be familiar with C programming, setting up a build environment, makefiles, etc...</p><p>2. Or you can download a firmware from the net. There are many firmwares available such as LUA, nodeMCU, etc.. You dont need to worry about the SDK if you go this route. After you flash the firmware, you can write a small script ( similar to shell scripts) to do the tasks you need. This is a better option for new comers. </p><p>For this project, you can go either way. Method 1 is complex as I have mentioned, but if you go this route, you can use my code directly. </p><p>Method 2 is not so complex to get started with. But you will have to write some new scripts to get the temperature/humidity data and send it to thingspeak. Also, to get value from thingspeak and switch on/off relay.</p><p>I would recommend you to try method 2. For this, check out nodeMCU firmware that has MQTT support. Personally I have not used this firmware myself, but from what I have read about it, I heard it is quite easy to use and there are lots of info available on the web. You can start from here:</p><p><a href="https://github.com/nodemcu/nodemcu-firmware" rel="nofollow">https://github.com/nodemcu/nodemcu-firmware</a></p><p>Hope this helps. Let me know if you need more info about anything.</p>
Thank you very much for the prompt &amp; detailed reply. I would dig it further more using these links &amp; hopefully make progress.<br><br>Tal
<p>Sure thing Tal. The reason that I have used method 1 in this instructable was because at the time I wrote it, MQTT support was available only in libraries... but since then, nodeMCU has added MQTT support.. so that's probably a better alternative now... however, i am too lazy to update the project with info on how to work with nodeMCU... also, i have moved on to other projects... but since it gives me the flexibility i need, I will most probably continue to use method 1. </p><p>However, do let me know if you have any specific queries and I will try to help you as much as I can. </p><p>Cheers!</p><p>Gopi</p>
<p>Hi,</p><p>It is really a very nice instructable.</p><p>I will try to fetch your code and compile it.</p><p>This is exactely what I was looking for to automate my heating system.</p><p>I enjoy the MQTT protocol more than thingspeak as it allows to send commands to your device. (although it's possible to push a tweet with thingspeak, but not the same thing...)</p><p>I have to see how difficult it would be to design an android App to connect to MQTT broker... Probably not too complicated.</p><p>One question did you implement the deepSleep function of ESP8166 ? It would be nice to drain a little less if only interested to push sensor data to MQTT from time to time.</p><p>I imagine a system with 2 ESP. One to measure temp in my living room and send it to MQTT broker. The second to react and switch On/Off the relay.</p><p>JP</p>
<p>Hi freedom2000,</p><p>you might wanna have a look at DIoTY.co (www.dioty.co). It has a MQTT cloud broker and a mobile app. For now, it does not give you graphs as no history is stored, but the mobile app allows you to see the latest temp published, create your (on-off) switches or even a slider to adjust your thermostat's requested temperature...</p>
<p>Thanks!</p><p>I have not really used any power saving techniques in my code since its always gong to be connected to the mains supply through an adapter. From what little I understand about this stuff, I think with ESP8266, the majority of the power is used only when the modules is tranmitting or receiving data through wifi. And if you are using only MQTT, that means only on publish and receiving MQTT messages as well as the periodic keep alive messages. So if you control how frequently such messages are used, I guess you can also control how much power is used.</p><p>Btw, are you planning to do your project to run from batteries only?</p><p>Regarding the userinterfaces, i guess you can write your own. Or you can try out something like openhab. I have just started trying this out. You need to install the openhab software on a local computer such as a Raspberry PI or even a windows box. This supports MQTT and has a nice webUI as well as an Android app. </p><p>Right now, I am building another project, in which I plan to use multiple boards, each with any of the following:</p><p>1. ESP8266 module (ESP12 that has an ADC port) connected to a motion sensor and an air quality/smoke alarm sensor that will be installed in the ceiling.</p><p>2. ESP8266 module + ATmega1284 + TFT LCD touch display + relays + AC Fan speed control = one in living room and one in bedroom.</p><p>3. ESP8266 modules + ATmega328p + current sensor for measuring power usage ( mains power as well as UPS power)</p><p>4. ESP8266 modules + ATmega328p + LPG gas sensor + smoke alarm in kitchen.</p><p>I also plan to attach WS2812b led strips to some of the above to simulate mood lighting. </p><p>All of these individual nodes can talk among themselves as well as to openhab. And through openhab, i should be able to monitor/control from anywhere using the webUI or Android App.</p><p>I have already build a prototype of 2 above. But I am continuously refining and redesigning each and every component to make sure that I will be using them effectively for a long time and it does not end up as nothing more than a hobby project. So its going to be a while before I am done with it.</p>
<p>Hi again,</p><p>I have really progressed today :</p><p>- MQTT on ESP8266 with NodeMcu lua firmware --&gt; works easily</p><p>- MQTT on android with B4A library (http://www.b4x.com/android/forum/threads/mqtt-mq-t...</p><p>works also very well, can publish/subscribre and send messages</p><p>- regarding the WS2812b led strip, you'd better jump to the new AP102 leds. They are driven with SPI bus at very fast speed (impressive result) : see my Android App with them : <iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/zr7ZrnF-bfM" width="500"></iframe></p><p>I know that ESP8266 has SPI bus embeded, but I must admit that I am stuck with Espressif API... That's a pitty otherwise would be nice to have a fully standalone driver... Wifi enabled on one side and outputing SPI messages on the other side.</p><p>BTW if you need help for the APA102 protocol don't heasitate</p><p>JP</p>
<p>Thats great news JP. And thanks for sharing the details of your project. Lots of new things for me to absorb.</p><p>I had meant to tell you about the nodemcu firmware, but somehow forgot to mention it in my reply. I didnt realize till a couple of days back that nodemcu supports MQTT. I still need to find out if it does everything that I need (looks like it does) and if so, I will probably switch over to nodemcu instead of building custom firmware myself.</p><p>Regarding the LEDs, i had been using WS2812b for some other projects and like it because it needs just one GPIO pin. And regarding speed, I thought WS2812b itself was pretty good. I have done a DIY Ambilight project for my 32 inch TV and the responses are pretty good. Anyway, I did a search for AP102 on ebay (my primary source for components - free shipping from China to India :) ), but couldnt find any listings, probably AP102 is pretty new and not widely available yet.</p><p>About your troubles with the Espressif SDK, where exactly are you stuck? I had also faced a lot of issues setting it up correctly and if you can tell me the details of your issue, maybe its something i have seen and can help.</p><p> I had tried dabbling in Android development a while back, but didnt really go through with it since at that time, I really had no concrete projects. Now that I do, i again want to get back into it. But confused by so many options available for beginners. Tried MIT App Inventor, but its too simplistic for me. I believe you have some good background in Android App dev, the LED App in the video clip looks pretty slick. In your opinion, is B4A a good option for newbies - btw, i have good background in software, so coding is not a problem.</p><p>Also, are you planning to do redo LED project with ESP8266 modules. Do you believe it can be done with just nodemcu or atleast a custom firmware. I have seen one guy using SPI to talk to a port expander (MCP23217), but i cant seem to find how exactly he has done that. I have tried connecting a ESP-01 to a i2c OLED display and it works good. But I was thinking if I get a ESP-12 module, would it be possible to connect a SPI TFT LCD display to it. </p><p>Thanks</p><p>Gopi</p>
<p>Hi,</p><p>Please look for APA102 either on ebay or even cheaper on Aliexpress</p><p>You will find a lot of offers.</p><p>I really think that APA102 are better than others leds. SPI bus is much more responsive and stable compared to PWM on one wire...</p><p>Yes I do believe that B4A is really enough to start coding for Android. You can do pretty much what you want. And if not you can even encapsulate calls to java API. If you need a discount for buying the compilo I have one.</p><p>Regarding the APA102, I wanted to setup a ESP8266 driver connected one one side with TCP socket and on the other with SPI bus.</p><p>But as I said, the Esprissif SDK is not that easy !</p><p>Today I have finished a simple lua project with :</p><p>- auto setup of wifi connection</p><p>- if none, switch to access point</p><p>- web server to enter SSID password and MQTT parameters</p><p>- auto reboot to sation mode</p><p>subscribe to MQTT and listen to events</p><p>Lua is really simple. But iti is interpreted and eats a lot of memory...</p><p>I can share with you my code and adroid apk send me a mil at</p><p>freedom2000 at free.fr (replace at by @)</p><p>JP</p>
<p>So you are using thingspeak for the graphs, and the mqtt for the commands?</p>
Yes. Actually, i can even get commands from thingspeak - not a very elegant way, but it works. At the time of making this project, i already knew that MQTT was the way to go, but since I also wanted to show the temperature and humidity data in graph from, i used thingspeak for the graphs and cloudmqtt for the commands.<br><br>But later I found out that there is another site called devicehub.net that supports MQTT as well as showing your data in graph form. Plus that site has buttons to turn on/off things.<br><br>Anyways, now i am already into node-red and NetIO and its looking pretty cool. Check it out if you haven't already. I will still be using thingspeak and devicehub, but they are just for show.
<p>Very interesting. With regard to programming the ESP8266 that is also possible with the Arduino IDE: </p><p>http://makezine.com/2015/04/01/installing-building-arduino-sketch-5-microcontroller/</p>

About This Instructable

268,148views

388favorites

More by gopinath.marappan:IoT Power Strip IoT Mood Lighting IoT Wall Outlet with Arduino and ESP8266 
Add instructable to: