I swear my wiring isn’t that neat. Photo by Tim Käbel on Unsplash

NodeMCU is a development kit based on ESP8266, integrating GPIO, PWM, IIC, 1-Wire, and ADC all in one board. Yeah, I also don’t know what any of those means.

So umm, let’s create a few bulbs (real bulbs, bulbs running on AC, not those stupid LED bulbs which kids use) which can be controlled by HTTP requests.

Things needed:

NodeMCU (obviously) Relay ( 😎) You can get one on Amazon. Bulbs or LEDs, if that’s all you want.

How to get the NodeMCU ready if you’ve never done this:

If you’ve set up the Arduino IDE and have done something with the NodeMCU before, go to the next heading.

To program the NodeMCU we can use the Arduino IDE which you can download from here. NodeMCU is not supported by default, so go to File -> Preferences in Additional Boards Manager URLs,and add http://arduino.esp8266.com/stable/package_esp8266com_index.json.

Then go to Tools -> Board:xxxx -> Boards Manager and search for ESP and install ESP8266 by ESP8266 Community.

After installation, go to Tools again and select the board you have.

Or basically go here, which is what I copy-pasted, or stole from, after all, Good artists copy; great artists steal. Try the blink program, which is :

void setup() {

// initialize inbuilt LED pin as an output.

pinMode(13, OUTPUT);

}



// loop function runs over and over again forever

void loop() {

digitalWrite(13, HIGH); // turn the LED on by making the pin 13 HIGH

delay(500); // wait for a 0.5 second

digitalWrite(13, LOW); // turn the LED off by making the pin 13 LOW

delay(500); // wait for a 0.5 second

}

If the light blinks, you did an awesome job. Wow…

But this is not what you came here for. So here is the real stuff.

Real Stuff:

On your NodeMCU, there should be pins marked D1 to D8. These are configurable pins. I will walk you through the code. If you don’t want to know what the code does and just want to see some things work, change "ROUTERNAME","PASSWORD" to your router SSID and password, copy this code to your Arduino IDE, upload this and go to the next step.

We assign the pins to variables R0 to R8. R0 is an LED which I am using just to see if things are working.

The pins can either be output or input, but we want them all to be output. The setup function does two things. One, makes all the switches to be output switches. Two, it connects to the wifi and once connected makes it listen to HTTP requests.

server.on("switchOn", handleSwitchOn); listens to switchOn, and calls the function handleSwitchOn and so on.

The functions handleSwitchOn , handleSwitchOff and handleSwitchStatus handle the HTTP requests. The requests should be something like http://10.0.09/switchOn?switchId=1 for switching on the second pin in the array bool State defined above(the first index of an array is zero). String t = server.arg("switchId") will get the value of switchId. The next line converts it to integer and then changes the state of the pin to high and then sends a response of 1 to the browser.

I guess the remaining functions are self-explanatory. The handleSwitchStatus is there so we can get the status of the switch.

The reason why the response is 0 and 1 and there is a function to handle the switch status is for another project which I am hoping to write an article on pretty soon.

Switching on LED bulbs:

A blue LED on your NodeMCU should already be on. That is the LED on D0. So to switch it off, go to the first URL, and to switch it on again, the second URL.

http://<your_nodemcu_ip_address>/switchOff?switchId=0

http://<your_nodemcu_ip_address>/switchOn?switchId=0

If you’re using LEDs, connect the positive leg of the LED to one of the output pins D2 to D8 and the other leg to ground (marked GND on the NodeMCU). To get it working, go to the URL as above with the switchId the same as of the pin (D2 should be switchId=2).

Switching on real bulbs:

Connect the relay to the board. The connections are as follows:

What is marked as Vcc on the relay should go to Vin on the NodeMCU, the ground should go to ground and the others should go to each of the D1 to D8 on NodeMCU. I think that part is pretty straight forward. And on the other side is where you give the AC current. There are three sockets or places where you can connect the wire. You can look at the diagram above them, and you’ll understand that one is closed by default and the other is open by default. Wire them as you need. Connect one to the center and the other to the socket which is open by default.

Aaaaaaanddddd voila, we have switches that can be controlled by HTTP requests.