Control relay over the internet from the comfort of work or home. Suitable for outdoor lighting, remote administration, intelligent house etc... Relays can switch 230V 10A max.
I have two variants available. Ethernet for Arduino to HTTP website and NodeMCU with wifi connectivity to HTTPS page. The control is adjusted to 8 outputs. To the code for Arduino / NodeMCU I will give the code for the part of the web, the scheme for the relay connection and the relays themselves. It is best to buy an 8-channel relay for this project, it is tested under both boards and works great.
Codes for Arduino are compatible with any Arduino, where W5100 Ethernet shiled can be connected. The code for NodeMCU is compatible with v2 Amica and v3 Lolin.
Demo for testing: https://arduino.php5.sk/rele/ (Slovak language sorry, i will translate your sketch/php files to your language)
NodeMCU sketch for test:
#include <ESP8266WiFi.h> //kniznica importovana v Arduine core, testovana verzia 2.3.0
#include <WiFiClientSecure.h> //kniznica importovana v Arduine core, testovana verzia 2.3.0
const int led = 16; //GPIO 16 = D0 on NodeMCU board
const char * ssid = "menowifi";
const char * password = "heslowifi";
const char * host = "arduino.php5.sk"; //bez https a www
const int httpsPort = 443; //https port
const char * fingerprint = "13 9f 87 1d b1 85 be e6 bd 73 c1 8d 04 63 58 99 f0 32 43 92"; // odtlacok certifikatu SHA1
void setup() {
Serial.begin(9600);
Serial.println();
pinMode(led, OUTPUT);
Serial.print("pripajam na wifi siet: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi uspesne pripojene");
Serial.println("IP adresa: ");
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClientSecure client; //funkcia pre HTTPS spojenia
Serial.print("pripajam sa na server ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("pripojenie neuspesne");
return;
}
if (client.verify(fingerprint, host)) {
Serial.println("certifikat zhodny");
} else {
Serial.println("certifikat nezhodny");
}
String url = "/rele/rele1.txt";
Serial.print("poziadavka na adresu: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: NodeMCU\r\n" +
"Connection: close\r\n\r\n");
Serial.println("poziadavka vykonana");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("Response prijata");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("Vratena premenna: ");
Serial.println(line);
if (line == "ZAP") { //zapnem vystup (rele alebo diodu)
digitalWrite(led, HIGH);
} else if (line == "VYP") { //vypnem vystup (rele alebo diodu)
digitalWrite(led, LOW);
} else {
Serial.println("Nepodporovana instrukcia, opakujte prepnutie rele z internetu");
}
}
The resulting code for controlling the 8x relay is in a nice graphic web template.
Arduino Uno + Ethernet shield sketch for test:
#include <SPI.h>
#include <Ethernet.h>
int led = 13;
byte mac[] = { 0xAA, 0xBB, 0xCC, 0x81, 0x7B, 0x4A }; //fyzicka adresa MAC
char serverName[] = "www.arduino.php5.sk"; // webserver
IPAddress ip(192, 168, 1, 254);
EthernetClient client;
String readString;
int x=0; //pocitadlo riadkov
char lf=10; //line feed character
void setup(){
pinMode(led, OUTPUT);
if (Ethernet.begin(mac) == 0) {
Serial.println("DHCP nepridelilo adresu, skusam so statickou...");
Ethernet.begin(mac, ip);
}
Serial.begin(9600);
}
void loop(){
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /rele/rele1.txt HTTP/1.1"); //download text
client.println("Host: www.arduino.php5.sk");
client.println("Connection: close"); // ukonc HTTP/1.1 spojenie
client.println(); //koneic requestu
}
else {
Serial.println("Pripojenie neuspesne"); //chyba ak nie som pripojeny
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //cakaj na data
while (client.connected() || client.available()) { //pramenna
char c = client.read(); //dostan bity z buffera
Serial.print(c); //cela http hlavicka
if (c==lf) x=(x+1); //pocitaj
else if (x==12) readString += c; //nasa premenna
}
if(readString=="ZAP"){
digitalWrite(led, HIGH);
}else if(readString=="VYP"){
digitalWrite(led, LOW);
}
else{
Serial.println("Nepodporovana premenna.");
}
readString = ("");
x=0;
client.stop(); //ukonc spojenie
delay(5000); //pockaj 5s a vykonaj novu slucku loop
}