Control relay from website - Arduino + Ethernet shield or NodeMCU

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
}

hi,
excuse me. i have some questions?
did you test this project yourself?
is it working well?
shall we have problems with it?
Serial.begin(9600); Arduino Uno don't use 115200? is this one right?
because of lots of sketch's in internet that don't work, i am asking these questions.
thanks a lot.

Hello. You can use speed which you want.. maximum is 230400 i think.
It is working well with each speed. I think in my opinion, 9600 is standard. 115200 is standard at ESP8266 for instance.
I have tested it, it is working great. I haven't problem with speed or something like that.

I am planning a similar application. My sensor nodes in the field will send data like temp, humidity to a server and on the server it will analyze if the values are low or high and accordingly send a notification to user on android app. Then user will press a button to maybe turn the pump on. This signal should go to server and from there to a arduino relay connected to internet via GPRS module to turn pump on.

Could you explain how the server API can be written for this purpose

atulkatti:
I am planning a similar application. My sensor nodes in the field will send data like temp, humidity to a server and on the server it will analyze if the values are low or high and accordingly send a notification to user on android app. Then user will press a button to maybe turn the pump on. This signal should go to server and from there to a arduino relay connected to internet via GPRS module to turn pump on.

Could you explain how the server API can be written for this purpose

from 1. web page to server php (writing api) > 2 .writes to mysql db > 3. arduino code triggers the php (reading api ) to read what's written into db in stage number 2 4. the read data is sent to arduino as 'echo'

Kasim explained it great :slight_smile: