NodeMCU Wi-Fi switch programmed using Zerynth Studio
|Time required||30 minutes|
Since the boom of IoT, ESP8266 is one of the most famous Wi-Fi-based embedded systems used for various applications. It became one of the successors to Arduino as it can almost do the same functions. NodeMCU is an IoT platform that uses ESP8266 as its embedded system. Boards such as NodeMCU V2 and V3 were developed for easier prototyping instead of using the ESP8266 chips alone. In this tutorial, we want to introduce a very quick application of the NodeMCU programmed using Zerynth Studio IDE. We particularly use Zerynth Studio in this tutorial to show its advantages over other IDEs as it can be used as a common platform IDE for different IoT boards. Here are the materials and components as follows:
Materials and components
- NodeMCU V2 or V3 (V3 is used for this tutorial)
- USB cable
- Female-female pin connectors
- 1-port 5V relay
STEP 1: NodeMCU drivers installation
If you haven’t installed any drivers for your NodeMCU board, we suggest you follow this step. Proceed to STEP 2 if you have already installed the drivers before.
1.1 Download CH340 drivers and install
Once you open the installer, it will show you something like this:
Click install and wait for the installation to finish. Once done, navigate to your PC’s Device Manager. The Device Manager can be brought up by pressing WIN + PAUSE BREAK or simply go to Control Panel > Hardware and Sound > Device Manager. Now, connect your NodeMCU board to your PC via micro-USB cable as shown in the next images and check your Device Manager window. You should be able to see a device such as USB-SERIAL CH340 (COMx).
STEP 2: Set-up your Zerynth studio environment and your NodeMCU board
2.1. If you haven’t done so, download and install Zerynth Studio:
After installation, open the Zerynth Studio IDE and you should be able to see the following:
As requested by the IDE, you need to log-in using an account to avail of their IoT services and such. Thus, sign up for an account to log-in.
2.2 Once you can access the IDE, the first step is to set-up your board for the Zerynth Studio environment. In the center (or upper left) or corner of your IDE, find your device, NodeMCU V3 from the Device drop-down menu.
2.3 Next, select your device from the Device info, then click Create.
In the Create window, it will ask you for which VM you are going to use. Assuming that you have a Free or Starter account, just simply click Create.
After creating your device, click Virtualize.
Next, this window will show up, and just click Virtualize again.
By this time, your NodeMCU board should be blinking as the device is being virtualized. Wait until it is done showing the following console output:
STEP 3: Wiring
Connect your components based on the following image:
To briefly explain about the pins used, we used D5 as the GPIO pin since not all digital pins of the NodeMCU are useable for general purposes. Depending on the board manufacturer, the relay board can also be powered by 3V. Otherwise, use an external power supply for the relay.
One of the handy functions of the IDE is that we can see the pin-outs of the board we are using by clicking the Device Pinmap button
You can always use this as your reference.
STEP 4: Programming
4.1 Finally, after several IDE preparations, let’s start working on the codes of our small IoT project. Go to Project > New and type in a name for your project. To make it easier, clone our GitHub repository here:
After doing so, open the main.py file to edit some variables in Lines 26-30. Particularly, edit the SSID and PASSWORD according to your Wi-Fi router’s settings.
As you might’ve noticed, there is the UID and TOKEN entries. You can register for a UID and TOKEN by going to the Connected Devices of your toolbar.
This will show your connected device now. Afterward, click the + Template button.
In the next window, you can create your template and name it. After creating one, this should show up.
From the previous window, click update. It should show a folder selection window where you have to select your cloned repository’s template folder.
This is an important step because this uploads your webpage to the cloud server of Zerynth for use in the APP later on.
4.2 Now we start uploading the code by clicking the uplink button on the upper left corner of your IDE.
Once done, you should be able to see Uplink done. If not, go back to STEP 2. The most common error in setting up Zerynth Studio is the virtualization part. Make sure everything is done smoothly to avoid errors when uploading.
STEP 5: Testing with the APP
Now, finally, we can test our program with the Zerynth APP. The APP is available for both Android and iOS users. Make sure that the login credentials you use for your APP are same as the one you used for the Zerynth Studio IDE, otherwise this won’t work. The download link is here:
Once logged in, you should be able to see your device in the dashboard like this:
Click your device and it should show the interface we prepared for this example:
That’s it! If you click the switch your relay should make a ticking sound that means it switches on and off. With this, you can make your own AC power switch to turn off your room lights or fan.
This shows the advantage of using Zerynth in developing applications for IoT. It reduces the burden in writing APPs that can be very time-consuming and might require a good background in mobile app development. Moreover, all the IoT devices are also connected to the Zerynth cloud making it very convenient.