Go Down

Topic: Control relay from website - Arduino + Ethernet shield or NodeMCU (Read 1 time) previous topic - next topic

martinius96

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:
Code: [Select]
#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:
Code: [Select]
#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
}

Arduino and website programmer

coretech1988

 The technicians usually have years of experience and based on that they can solve your queries in very less time period. The


best thing about AOL customer support number is that you can get support 24/7. Yes, you can call to the experienced team  at any point of time no matter whether it is a day or night. There are a number of team of experts who are trained to fix any of issue that you may experience with your email account. Their services are quite affordable and sometimes negligible and does not burn a hole in your pocket. Whether you are unable to sign into your account, or maybe issue is sending or Aol Support number-1-844-205-0712 team that will help you out in the shortest time period and at your doorstep only.

Engafshar

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.

martinius96

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.
Arduino and website programmer

Go Up