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.
ESP-12E WiFi module is developed by Ai-thinker Team. Core processor ESP8266 in smaller sizes of the module encapsulates Tensilica L106 integrates industry-leading ultra low power 32-bit MCU micro, with the 16-bit short mode, Clock speed support 80 MHz, 160 MHz, supports the RTOS, integrated Wi-Fi MAC/BB/RF/PA/LNA, on-board antenna. Nov 15, 2015 - A development kit for NodeMCU firmware. Contribute to nodemcu/nodemcu-devkit development by creating an account on GitHub.
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. 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.
![]() ![]()
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. 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. ESP8266 ESP-01 module 2. DHT11 or DHT22 sensor 3. 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. 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. The board pushes temperature and humidity readings as well as the relay state to:. MQTT broker: Any MQTT (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 userconfig.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 usermain.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. The code is available at: Before compiling:.
Configure ESP8266 build environment using steps in. Update userconfig.h and change MQTT host and login details as well as WiFi login details. #define MQTTHOST 'your cloudmqtt instance name' #define MQTTPORT your cloudmqtt instance port #define MQTTUSER 'your cloudmqtt instance username' #define MQTTPASS 'your cloudmqtt instance password' #define STASSID 'your WiFi SSID' #define STAPASS 'your WiFi password'. Update following two global variables in usermain.c to your values: char YOURTHINGSPEAKAPIKEY= 'xxxxxxxxxxxxxxxxx'; char YOURTHINGSPEAKCHANNEL= 'xxxxxxxxx';. Build target 'all'. 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.
WIFIINIT MQTTInitConnection MQTTInitClient 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% STATIONIDLE add 0 aid 4 pm open phy2,type:2 0 0 cnt connected with dlink, channel 2 dhcp client start. STATIONIDLE STATIONIDLE DHT: 24 00 1b 00 3f CS: 3fTemperature = 2700.C, Humidity = 3600% STATIONIDLE STATIONIDLE STATIONIDLE STATIONIDLE 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. View The temperature/humidity readings as well as the relay state can be seen at Control For switching on the relay, the field1 of the Thingspeak channel should be updated to 1.0 using the following URL: 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: 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:).
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. Another suggestion was to use a zener diode beween GPIO0 and Vcc to fix this issue. 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. 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:). 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.
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. 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- 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. 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. 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 'relay switch only' 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. 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. 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. Hello, i m able to follow the steps till 6 successfully but as per the output concern i am getting like:(image- sample.jpg) my esp8266 is not able to connect to WIFI network since error called 'fatal exception(29)' is coming eventhough i put the SSID & password correctly.(no problem with the board, works fine for both STATION & ACCESS Point interface) #define MQTTHOST 'm12.cloudmqtt.com'?? Is it correct (see image sample1.jpg) #define MQTTCLIENTID 'DVES%08X' (default) is it correct?? #define MQTTPORT 10981?? (Port??, SSL Port??,Websockets Port (TLS only)??
) which one?? LOCAL char YOURTHINGSPEAKCHANNEL= '163381';(see image- sample2.jpg) is it correct?? Looking forward for ur support, thanx in advance;-). Hi, Great post. This is a request for guidance. My ESP8266 module connects to “test.mosquitto.org” and subscribes to a topic say “b1”.
What ever is published to “b1”, it can read it immediately. Problem happens when my second ESP8266 module subscribes to “test.mosquitto.org” to “b1”.
It continuously returns: WiFi connected IP address: 192.168.0.9 Attempting MQTT connectionconnected Message arrived b1 0 Attempting MQTT connectionconnected Message arrived b1 0 Attempting MQTT connectionconnected Message arrived b1 0 Attempting MQTT connectionconnected Could you kindly suggest what might be going wrong. I look forward to your advice.
Comments are closed.
|
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |