Go Down

Topic: Control Relay with Arduino and ESP8266 (Read 1 time) previous topic - next topic

Rahmatrf

hello sir ..
i want to create "control relay using arduino Uno adn ESP8266. I copy code from some blog, and i try it...
but i find some error. please help me...
If you have succes...
my email : rahmatfauren28@gmail.com

i found error in serial monitor:

AT+RST
Module have no response
Begin error
Init error



#define SSID       "Andromax-M3Z-65FD"
#define PASSWORD   "36434633"

#include "uartWIFIUNO.h"

#include <SoftwareSerial.h>
WIFIUNO wifi;

extern int chlID;

// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

const int relay1 =  2;
const int relay2 =  3;
const int relay3 =  4;
const int relay4 =  5;   

boolean device1 = false;
boolean device2 = false;
boolean device3 = false;
boolean device4 = false;


void setup(){
 
  pinMode(ledPin, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
 
  //pinMode(ESP8266_CHPD, OUTPUT);
  //digitalWrite(ESP8266_CHPD,LOW);
 
  digitalWrite(relay1,LOW);
  digitalWrite(relay2,LOW);
  digitalWrite(relay3,LOW);
  digitalWrite(relay4,LOW);
 
   DebugSerial.println("ESP8266 Server v0.21");
 
 
  if(!wifi.begin())
  {
     DebugSerial.println("Begin error");
  }
  bool b = wifi.Initialize(STA, SSID, PASSWORD);
  if(!b)
  {
    DebugSerial.println("Init error");
  }
  delay(8000);  //make sure the module can have enough time to get an IP address
  String ipstring  = wifi.showIP();
  DebugSerial.println(ipstring);      //show the ip address of module
 
  delay(1000);
  wifi.confMux(1);
  delay(100);
  if(wifi.confServer(1,80))
   DebugSerial.println("Server is set up");
   
}
void loop()
{
 
  char buf[500];
  int iLen = wifi.ReceiveMessage(buf);
 
  if(iLen > 0)
  {
   
      DebugSerial.print(buf);
      //delay(300);
         
       if (strncmp(buf, "GET /?status1=1", 15) == 0) {               
           //DebugSerial.print("RELAY 1 ON");
           digitalWrite(relay1, HIGH);
           device1 = true;           
       }   
       else if (strncmp(buf, "GET /?status1=0", 15) == 0) {               
           //DebugSerial.print("RELAY 1 OFF");
           digitalWrite(relay1, LOW);
           device1 = false;           
       }
       
         
   
    if (strncmp(buf, "GET /?status2=1", 15) == 0) {               
           //DebugSerial.print("RELAY 2 ON");
           digitalWrite(relay2, HIGH);
           device2 = true;           
       }   
    else if (strncmp(buf, "GET /?status2=0", 15) == 0) {               
           //DebugSerial.print("RELAY 2 OFF");
           digitalWrite(relay2, LOW);
           device2 = false;           
       }
       
 
     
      String cmd;
      cmd = "HTTP/1.1 200 OK\r\n";
      cmd += "Content-Type: text/html\r\n";
      cmd += "Connection: close\r\n";
      //cmd += "Refresh: 15\r\n";
      cmd += "\r\n";
      //cmd += "<!DOCTYPE HTML>\r\n";
      cmd += "<html>\r\n";
      //cmd += "<header><title>ESP8266 Webserver</title><h1>\"ESP8266 Web Server Control\"</h1></header>";
     
     
      if(device1){
        cmd +=("<br/>Device1  : ON");
      }
      else{
        cmd +=("<br/>Device1  : OFF");
      }
     
      if(device2){
        cmd +=("<br/>Device2  : ON");
      }
      else{
        cmd +=("<br/>Device2  : OFF");
      }
     
     
       if(device3){
        cmd +=("<br/>Device3  : ON");
      }
      else{
        cmd +=("<br/>Device3  : OFF");
      }
     
      if(device4){
        cmd +=("<br/>Device4  : ON");
      }
      else{
        cmd +=("<br/>Device4  : OFF");
      }
     
      cmd += "<html>\r\n";

        wifi.Send(chlID,cmd);
        //delay(200);
        wifi.closeMux(chlID);
   //delay(1000);
 

  }
}

vnn3

Should use a code block for easy to read.

You are using the uartWIFIUNO library: https://github.com/DonEduardo/SprinklerControl/blob/master/ESP8266UNO/examples/chatServer/chatServer.ino

The easest way is trying to test with example after that change to what you want. However, there are more popular way which uses Arduino core for ESP8266 WiFi chip having much examples and tutorials as well.

Go Up