Smart buttons are one of the current trends in IoT-enabled wearables. You can use them to trigger a diverse set of actions effortlessly. This tutorial walks you through the basic setup for creating your own DIY smart wifi button and linking it with IBM Cloud.
Initially, the ESP8266 device is in deep sleep, which helps to reduce the overall power consumption and to save the low capability batteries from running out quickly. When the user hits the reset button, it triggers a boot cycle. Once the device finishes booting up, it connects to a hardcoded wifi hotspot, connects to the public MQTT server, publishes a message with the device ID in it, and then goes to sleep. The message triggers an action on the cloud. Figure 1 shows this cycle.
Figure 1. Boot cycle
This tutorial will take approximately 60 minutes to complete.
This tutorial assumes that you have basic knowledge of electronics (and optionally some soldering skills). Before beginning this tutorial, you need the following:
- IBM Cloud account
- The following hardware
- Project files
You can build your smart button in these 4 steps:
Set up connections
We are going to use Arduino IDE to program our smart button. To set the ESP8266 device in bootloader mode, first connect it, as shown in Figure 2.
Figure 2. Set ESP8266 in bootloader mode
One best practice to properly boot (especially in bootloader mode) is to first reset with GPIO0 connected to GND and then leave GPIO0 as floating.
Now before connecting the ESP8266 device to your PC, you need to install a couple of chipset drivers:
Open the Arduino IDE, and install the ESP8266 microcontroller board by adding the following url in Arduino->Preferences->Settings->Additional Boards, as shown in Figure 3.
Figure 3. Add additional board
Install the ESP8266 board from library manager at Sketch->Include Library->Manage Libraries, search for ESP8266, and install the built-in board, as shown in Figure 4.
Figure 4. Install built-in board
Install PubSubClient, as shown in Figure 5.
Figure 5. Install PubSubClient
Now you can quickly use the following code in Arduino IDE, replace the wifi credentials, and burn it onto the ESP8266 board. Make sure that you’ve selected the right board preferences before you upload, especially the board type and port number.
Board: "NodeMCU 1.0(ESP-12E Module)" Flash Size: "4M(3M SPIFFS)" CPU Frequency: "80 MHz" Upload Speed: "115200" Port Get Board Info
Once you are done uploading, you can quickly verify it by opening your serial port, and you should see the text “Published click trigger” appear every time you reset.
Test your device
For this project, we are using IBM Cloud and Node-RED to test out our device quickly.
- Sign in to your IBM Cloud account.
- Click Catalog.
- Search for Internet of Things Platform.
Click Create to create the service.
Figure 6. Create IoT service
Open the app URL, and you should be redirected to a Node-RED dashboard. Create the flow shown in Figure 7. Copy the flow from the JSON file in my repo, and click Import from the Node-RED main menu.
Figure 7. Flow in Node-RED dashboard
Try resetting your ESP8266 device and you should see a message on the debug pane in Node-RED, shown in Figure 8. This indicates that our smart button is working as it is supposed to.
Figure 8. Message in debug pane
Finalize the build
Once you have verified that your device is working as it is supposed to, then you can remove the pins from it and quickly solder it down according to Figure 9.
Figure 9. Circuit diagram
If the trigger is working as expected, you can use it to trigger multiple actions, like sending an SMS, controlling an actual electronic appliance, or trigger a serverless function that starts a container.
Common errors and debugging
warning: espcomm_sync failederror: espcomm_upload_mem failed
step1: connect GPIO0 ->GND step2: Reset step3: disconnect GPIO0 from GND and leave it floating (Not connected to anything)
Sometimes the current supplied by a standard USB is not enough to run the chip. Try to connect an external power supply or use a different port while programming.
This tutorial showed you how to set up an ESP8266 microcontroller and connect a smart button to it, and then program the smart button to trigger various actions. What will you program your smart button to do?