Objectives
- Control the NodeMCU on board LED using Adafruit IO
- Visualize analog sensor data on Adafruit IO
Things
- NodeMCU (quantity: 1 no.)
- Breadboard (quantity: 1 no.)
- Micro USB cable (quantity: 1 no.)
- IR sensor (quantity: 1 no.)
- Adafruit account
- Arduino IDE on your computer
Let’s begin!
Adafruit.io is a online service which can communicate with the NodeMCU using MQTT. We will use this service to control the on board LED of the NodeMCU. We will also connect an analog IR sensor and visualize the values on Adafruit IO.
Interfacing IR sensor with NodeMCU
Setting up Adafruit IO dashboard
Step 1: Open a browser and go to io.adafruit.com. If you’ve signed in for the first time, this page should show up. If not, navigate to Dashboards
Step 2: Navigate to Actions and select Create a New Dashboard
Step 3: Name the dashboard and click Create
Step 4: Open the newly created dashboard
Step 5: You will get a blank dashboard. Click on + to create new blocks on the dashboard
Step 6: Select a Toggle block
Step 7: Select Enter new feed name. Type in BUILTIN and click Create
Step 8: Select the newly created field. Click on Next step
Step 9: Give an appropriate Block Title. Set the ON and OFF texts as 0 and 1 respectively. Click on Create Block
Step 10: Click on + to create a new blocks
Step 11: Select a Guage block
Step 12: Select Enter new feed name. Type in Analog and click Create
Step 13: Select the newly created field. Click on Next step.
Step 14: Give an appropriate Block Title. Set the Guage Max Value as 1024. Click on Create Block
Step 15: Click on the Settings symbo
Step 16: Rearrange the blocks and click Save
Step 17: Success! Now you are ready to continue
Final Code
You will need to install Adafruit MQTT library. Go to Sketch -> Include Library -> Manage libraries. Search for Adafruit MQTT and click Install.
Change the following lines in the code:
Add your WiFi credentials
#define WLAN_SSID "Your_SSID" #define WLAN_PASS "Your_Password"
Add your Adafruit username
#define AIO_USERNAME "xxxxxxxxxx"
Add your AIO key
#define AIO_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
To copy the code, right click on view raw at the bottom of the code, click on open link in new tab and then copy the code.
Uploading the code
Step 1: Enter the final code in the Arduino IDE window.
- When you click on New to open a new blank code in the Arduino IDE, you will get a new window with two functions: void setup() and void loop()
- You can either type commands as shown in the final code under void setup() and void loop() functions or
- delete the contents of this window. Then copy and paste the final code in the Arduino IDE window.
Step 2: Compile/ verify the code by clicking on the verify button.
- Make sure the board is connected to your computer
- Check if the correct board and port is selected.
Step 3: Upload the code by clicking on the upload button, which is right next to the verify button.
- The Arduino IDE provides a progress bar when it’s uploading.
- After it is done, you will see “Done uploading” on the bottom left of your IDE.
- If you encounter any errors, you need to go back to the code and fix the errors.