#Turn On Or Off This is the embeded client that will control a relay board. It will send a request to the server and get the response. If the response is "on" then the relay will turn on, if the response is "off" it will turn off. In any other case we don't change state. We should however emit the reponse to the serial port for debugging.
You should have completed the following tutorials:
- NodeMCU
- LED
- 200 -1k Ohm resistor
- Relay board ideally one capable of taking 3.3 v input.
- We will begin by making a request to the server and get the response. To do that you will need to include the WebRead library.
#include <WebRead.h>
- define the ssid and password of your network variable for your network
char ssid[] = "network id (SSID)";
char pass[] = "netowrk password";
- Also define the url of the server
char url[]="http://hocfun.azurewebsites.net/api/echo?code=<code>&name=<your name>";
- Define the pin that will be used for the LED
int ledPin = 2;
- We need to setup the serial port for communication
Serial.begin(9600);
- Add an initial print statement
Serial.println("Begin loop ... ");
- set pin mode
pinMode(pin, OUTPUT);
- Connect to the network
webread.connect(ssid, pass);
- Make a request to the server
String response = webread.get(url);
- Print the response to the serial port
Serial.println(response);
- if "on" turn the pin on, if off turn it off. Otherwise do nothing
if (response == "on") {
digitalWrite(ledPin, HIGH);
} else if (response == "off") {
digitalWrite(ledPin, LOW);
}
- wait for 2 seconds
delay(2000);
Proceed to Read the web to check setup your server and test your code.
- Validate that "on" turns the LED on and "off" turns it off
- Validate that any other response does not change the state of the LED
Using a relay board with an opto driver connect the coil to the pin used for the LED.
- sample relay board
- AEDIKO DC 5V Relay Module 1 Channel Relay Module Optocoupler Isolation High and Low Level Trigger for Arduino Raspberry Pi PIC AVR DSP ARM ARM MCU TTL Logic
- connect the coil + to 3.3v
- connect the coil - to ground
- connect signal (IN) to the pin defined in the code
- if the relay has a high/low jumper make sure it is set to High
- Validate that "on" turns the relay on and "off" turns it off.
In this step we will connect the relay control side to a light bulb. The light bulb should have an independant power supply. If using anything other than a basic low voltage light bulb be very careful. Exposure to high voltage can be fatal.
The Solution is one possible solution.