Skip to main content

NodeMCU Wi-Fi switch programmed using Zerynth Studio

Author Dan 
Difficulty Easy
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

  1. NodeMCU V2 or V3 (V3 is used for this tutorial)
  2. USB cable
  3. Female-female pin connectors
  4. 1-port 5V relay

0119_b080ab75d34a2d64d0e935cf96cafe69867a99d8.png

Instructions:

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

https://sparks.gogo.co.nz/ch340.html

Once you open the installer, it will show you something like this:

0215_dda03f6a532584a1f9369ace92ac07fc4b19459c.png

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).

0317_63cb13de616abc97ab59c59f25b1c9568f3ba187.png

0416_c0b250b2c0653b235c2f2e883e38895826174a18.png

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:

https://www.rs-online.com/designspark/zerynth 

After installation, open the Zerynth Studio IDE and you should be able to see the following:

0512_2884c075571399da4937d4deb9f902c7b89c1407.png

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.

0610_18eba07b10296b1af8fe740d3c19068577119b1b.png

2.3 Next, select your device from the Device info, then click Create.

0713_a136f90a07097a73ac30dbb27c2b041e3325494e.png

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.

0811_e83b87e0d5e010de80eff668e56dc7fee8b32a62.png

After creating your device, click Virtualize.

099_b166bc4a742afeb2e2106008e7f17e5cfff02323.png

Next, this window will show up, and just click Virtualize again.

1025_9ba555af3b55463795125d22841c88541214f61a.png

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:

1142_17334c71de9bc8ad71f7bf60db99bf2808aa9abc.png

STEP 3: Wiring

Connect your components based on the following image:

1221_2b916a4a840ce09758536d3886d2b97251722995.png

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

1417_f9ad971a932b2e0257eb88779206de21b80aadd7.png

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:

https://github.com/danrustia11/NodeMCU_SmartSwitch_Zerynth.git

1516_2a76a4575b38061c6e2806c55daea5bf989e3900.png

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.

1617_a44e6165d992bf66a71d298d162cf3d5643b6df1.png

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.

1715_85aee4f7751f887f71aa0ba063d0ea56d0e272e7.png

This will show your connected device now. Afterward, click the + Template button.

1815_6d2690b5ba0214de287370704df8618422250234.png

In the next window, you can create your template and name it. After creating one, this should show up.

1915_052a653c1bb56767ac038d2f19fde275b2bc6a61.png

From the previous window, click update. It should show a folder selection window where you have to select your cloned repository’s template folder.

2014_05a057d859b34683d0e7cf7405a03bdc5f35fad2.png

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.

2116_4655c3e2229d2c155199f02296d9261f1b8eb7e2.png

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.

2214_cf0b1870a9aa4a257a9f9d3d3f1d64541c958d4c.png

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:

https://play.google.com/store/apps/details?id=thingsoninternet.biz.viperapp&hl=en

Once logged in, you should be able to see your device in the dashboard like this:

2317_27b4454e90b65163e53e65516e21c3c03fd751d5.png

Click your device and it should show the interface we prepared for this example:

2415_c9751421a1ffd90f34a2cdaf2da4e344d2961085.png 2515_1ae385e2574b3172a5637922f676ce76fa4efd20.png

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.

Source code:

https://github.com/danrustia11/CaveduRS/tree/master/NodeMCU/SmartSwitch

CAVEDU Education is devoted into robotics education and maker movement since 2008, and is intensively active in teaching fundamental knowledge and skills. We had published many books for readers in all ages, topics including Deep Learning, edge computing, App Inventor, IoT and robotics. Please check CAVEDU's website for more information: http://www.cavedu.com, http://www.appinventor.tw
DesignSpark Electrical Logolinkedin