An Inexpensive IoT Enabler Using ESP8266
An Inexpensive IoT Enabler Using ESP8266
Living
Outside
Play
Technology
Workshop
Table of Contents
An inexpensive IoT enabler using ESP8266 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 1: Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What is ESP8266? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What is Thingspeak? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 3: Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step 5: Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Before compiling: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
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/
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
Image Notes
1. ESP8266 ESP-01 module
2. 5v relay
3. DHT11 temperature/humidity sensor
Image Notes
1. FTDI USB to TTL adapter cable - only needed for flasing the firmware
2. 5v DC 1A adapter
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.
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
Step 5: Code
The code is available at:
https://github.com/tangophi/esp_mqtt
Before compiling:
Configure ESP8266 build environment using steps in http://signusx.com/esp8266-windows-compilation-tut...
Update user_config.h and change MQTT host and login details as well as WiFi login details.
#define MQTT_HOST "your cloudmqtt instance name"
#define MQTT_PORT your cloudmqtt instance port
#define MQTT_USER "your cloudmqtt instance username"
#define MQTT_PASS "your cloudmqtt instance password"
#define STA_SSID
#define STA_PASS
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
Image Notes
1. jumper connects GPIO0 to ground. For flashing the firmware.
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
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 %
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
Image Notes
1. 1st step - make sure jumper is removed and board is not powered up.
Image Notes
1. 2nd step - Connect power supply to the board with the jumper still removed.
Image Notes
1. 3rd step - Put back the jumper so it connects GPIO0 to relay. Board should run
normally now.
Image Notes
1. LED is lit - indicates the relay is active (turned on)
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...
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
Control
A message can be 'published' to the /esp8266/relay topic using a MQTT client to turn on/off the relay.
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
I guess a inverter can be 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.
Related Instructables
ESP8266 WiFi
ESP8266 WiFi
temperature and relay switch by
humidity sensor EasyIoT
by EasyIoT
Use ESP8266 to
Internet enabled
AC Appliances
by shinteo
Easy ESP8266
WiFi Debugging
with Python by
jimk3038
A newbie's
guide to setup
ESP8266 with
Arduino Mega
2560 or Uno by
shinteo
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
ESP8266 Wifi
Temperature
Logger by
noelportugal
Advertisements
Comments
20 comments Add Comment
gast says:
Nice project. How did you get thingspeak to send something back to the esp8266?
Thanks
gopinath.marappan says:
Thanks!
Well, thingspeak doesn't send any data on its own to the module. Rather, the modules gets the last value of the relay field from thingspeak once every
minute and if there is a change in the value, then it will activate/deactivate the relay.
I agree this is not an ideal way to do it, but for this instructable, I wanted to find free and easy to configure services to demonstrate what can be done with
ESP8266.
If you are planning to make something like this and want a way to control the module from the internet, check out devicehub.net. Came across this yesterday
only. I believe with devicehub, you can send data to the cloud through MQTT and have it displayed graphically. Also, since it uses MQTT, two way
communication is also possible. You can think of devicehub as a combination of cloudmqtt and thingspeak in terms of functionality.
Apart from this, there are several other ways to manage/monitor/control an ESP8266 module from the internet. But I feel that sending and receiving data
from several ESP8266 modules directly to and from the cloud is a bit of a stretch. Think about the bandwidth being used. Instead a local server (raspberry pi
or something like that) should talk to the ESP8266 modules and then have another interface setup so that the interface can be accessed from anywhere.
I am thinking about a project that will have a PIR motion sensor attached to ESP8266 module and will send a SMS alert to my phone if there is movement
detected when it is "armed" like a burglar/security alarm.
gast says:
madaerodog says:
gopinath.marappan says:
Hi Constantin,
Yes indeed. As far as I can see, this is the cheapest standalone (apart from a wifi connection) board to connect to the internet. Hope you have
been reading about the ESP8266 modules - there are several variants - mainly depending on how may GPIO ports are available.
I have started working on a new project and i plan to use devicehub.net in it. I have already seen the board successfully sending data to
devicehub.net through MQTT. And I believe receiving commands from devicehub.net should be okay too.
Btw, do you know how to secure the MQTT connection from a user computer (or ESP8266 module) to devicehub.net. Is this something that you
planning to add in the future?
Thanks,
Gopi
icotoi1 says:
gopinath.marappan says:
You are very welcome. Please do let me know when your project is complete.
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
icotoi1 says:
gopinath.marappan says:
gopinath.marappan says:
paulle says:
I m new to this SDK and found your project interesting. Couple questions
What does the user_init function do? And user_main.c file?
Is your code an added on to the firmware or does it overwrite the firmware?
gopinath.marappan says:
vsomu says:
I made it based on your inputs. I am able to view data on thingspeak.com. but could not receive data from cloudmtt.com
need to look into this problem.
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
gopinath.marappan says:
Nice. Good to know that my instructable has been helpful and also that your board works.
About mqtt, make sure the cloudmqtt account details are entered properly. You should also see if using any mqtt client on your computer (MQTTlens
Chrome App) and/or a phone (MyMQTT Android), if messages are getting sent properly from one to another though cloudmqtt. If that works, then it should
also work from esp8266. I did not have any issues with mqtt. It just worked :)
vsomu says:
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
MQTT_InitClient
System started ...
mode : sta(18:fe:34:9e:65:15)
add if0
DHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
DHT: 25 00 1a 00 [3f] CS: 3fTemperature = 2600 *C, Humidity = 3700 %
STATION_IDLE
STATION_IDLE
STATION_IDLE
gopinath.marappan says:
chrisrust says:
gopinath.marappan says:
chrisrust says:
vsomu says:
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/
http://www.instructables.com/id/An-inexpensive-IoT-enabler-using-ESP8266/