adamsstephen
Published © GPL3+

ESP01 Webserver to Control Your Arduino UNO, Mega or Nano

Using WiFi to control your Arduino UNO, Nano or Mega as a basis. Includes feedback and simple hardware.

BeginnerFull instructions provided18,698
ESP01 Webserver to Control Your Arduino UNO, Mega or Nano

Things used in this project

Story

Read more

Schematics

1K resistor complusory

Use instead of 220 ohms colours shown in Fritzog.

Code

ESP-01 and UNO Webserver Remote Control

Arduino
Control your Arduino board via WiFi and get a feedback
[code]
// You may use on board LED connected to pin 13
// Elminated delays and serial monitor commands, reduced response time to 5 seconds.
#include <SoftwareSerial.h>

int serialRx = 2;   // software serial  RX  TX 
int serialTx = 3;   // ESP-01 RX goes to this port, TX goes to Port 2
int d = 0;
// ESP-01 CH_PD pin 7 Must go to 3.3v, not +5v to enable operation as UART (AP)
// UNO R3 has enough power to make it work.
// No other connections required
String inMsg ;     // variable to collect strings
int sensorPin = 8;    // set maximum number of pins for the Analouge inputs
int LEDPin = 13 ;  // variable to assign pin to LED.
int responseTime = 10; //communication timeout
SoftwareSerial portOne(serialRx, serialTx); // communications port to ESP-01
 String Msg = ""; // to collect Analouge input data 
void setup() {

// Serial.begin(19200); // for debugging only

String  msg =""  ; 
  portOne.begin(115200);                // Start software serial port
  delay(200);

  sendToWifi("ATE0",100);                           // set Echo Off. Otherwise echo was ON.
  sendToWifi("AT+CWMODE=2",responseTime); // configure as access point on Chinese version
  // sendToWifi("AT+CWMODE_CUR = 2",responseTime); // configure as access point (AP)
  sendToWifi("AT+CIPMUX=1",responseTime); // configure for multiple connections
  // AT+CIPAP=<ip>[,<gateway>,<netmask>]
  sendToWifi("AT+CIPAP_CUR=\"192.168.5.1\"",responseTime); // Set IP address for AP
  sendToWifi("AT+CIPSERVER=1,80",responseTime); // Create and turn on server on port 80
  sendToWifi("AT+CWDHCP_CUR=0,1",responseTime); // Set AP with DHCP ON (required)
  //AT+CWSAP=<ssid>,<pwd>,<chl>,<ecn>[,<max conn>][,<ssid hidden>]
  // Open AP Port
  sendToWifi("AT+CWSAP=\"ESP8266\",\"1234567890\",5,3,1,0",responseTime); // Set Soft AP parameters problems sending " characters
  sendToWifi("AT+CIPAP_CUR?",responseTime);
  sendToWifi("AT+CIPSTO=600",responseTime); // Set timeout to 10 minutes
  pinMode(LEDPin,OUTPUT); // Open Port to operate LED on board
//   //serial.println("Setup is done!");
  // delay(2000);
}

void loop() {
   //Serial.println("Waiting....");
  if(portOne.available()>0){
    inMsg = readFromWifi();
    //serial.print("Received msg inside loop = ");
    inMsg.toUpperCase();
   // Serial.println(inMsg);
    // Messages in CAPITALS
    if (inMsg.endsWith("HELLO") ) {
    // Serial.println ("Wifi says : Hello");
        sendData("Wifi says : Hello\r");    // Send confirmation of Command + CR
        Send_inputs();
  }
    else if (inMsg.endsWith("LEDON") ){
          digitalWrite(LEDPin,HIGH);       // If just LEDON turn on Pin 13
          sendData("Wifi says : LEDON\r");        // Send confirmation of Command + CR
          Send_inputs(); // Send Analouge inputs
    }
   else if (inMsg.endsWith("LEDOFF") ){  
       String     msg = SETLEDS(0) ; // set all LEDs off.
        sendData("Wifi says : LEDOFF\r");   // Send confirmation of Command + CR  
        Send_inputs(); // Send Analouge inputs     
    }   
    else if (inMsg.indexOf("LEDONN",0)>6 ){  // if Command is to set a Number
         d = inMsg.indexOf("LEDONN",0)+6; // Set d to next letter after command // string starts with zero
     int a = 0; // zero collected number
     int dt = inMsg.length(); // find end of string (CR)
    if ( d+3 < dt){              // if there is a 4 digit number after command
            a = inMsg.substring(d, d+4).toInt(); // store 4 digits in number a
      String    msg ="ERROR";
            if ((a > 0) & (a < 1024) ) {  // Command not used for zero (use LEDOFF), avoids no number available
             msg = SETLEDS(a) ; // set all LEDs on or off, returns Binary string as confirmation
             }
             msg = "Wifi says : LEDONN="+msg+"\r";
            sendData(msg ); // Send confirmation of Command + CR
             Send_inputs();  // Send Analouge inputs
             }
   }
   // delay(9000);                              // deliberately large delay to help collect dump of serial port.
}
     inMsg="";  // Clear buffer
    delay(800); // delay to stop duplicate messages
}
void sendToWifi(String command, const int timeout){
// only send command, not intereted in response
  String response = "";
  // Serial.print("Sent command to ESP8266-E12: ");
   // Serial.print(command);
  delay(50);
  portOne.println(command); 
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(portOne.available())
    { 
      char c = portOne.read(); 
      response+=c;
    }  
  }

   // Serial.print("Response from ESP8266-E12: ");
   // Serial.print(response);
}

String  readFromWifi(){
  char arrayInMsg[100];
  String tempStr; 
  int count =0;
  while( portOne.available() > 0 ){
  
    arrayInMsg[count]= portOne.read();
    delay(80);
    // get out if you see CR or LF
    if ((arrayInMsg[count]=='\n') or (arrayInMsg[count]=='\r')) break; 
    // changed character array comparison or it causes errors
    count++;       
  }
  arrayInMsg[count] = '\0';                     // Null terminate finally
  tempStr = String (arrayInMsg);                // cast it to string
  tempStr.trim();                               // precaution
  // Serial.print(tempStr);
  return tempStr;
}
/*
* Name: sendData
* Description: Function used to send string to tcp client using cipsend
* Params: 
* Returns: void
*/
 void sendData(String str){
  String len="";
  len+=str.length();
  sendToWifi("AT+CIPSEND=0,"+len,responseTime); // Setup command to send str data length to channel 0
  delay(100);
  sendToWifi(str,responseTime);//Send String str with 0 closing the transmission and return to AT mode
   delay(100);
  do {
  delay(100);
  } while (portOne.available()==0); // wait untill response data "sent" or "failed"
}

String  SETLEDS(int long pulse) {
  pulse= pulse << 4; // move numbers up to pin 4
 String response2 = "" ;
    for ( int x = 13; x >3 ; x--)  { // Pins 13 down to 4
    if (pulse & (1 << x)) { // compare pulse with bit number of Digital output port
     digitalWrite(x,HIGH);
     response2 = response2 + "1";
     // Serial.print("1"); // just for testing
    }
    else {
     digitalWrite(x,LOW);
     response2 = response2 + "0";
    // Serial.print("0"); // just for testing
     }
     }
    // Serial.println(""); // just for testing
     return response2; // Return Binary string
  }
// Send Analouge data
void Send_inputs(){
  delay (1000);
  Msg = Sensor_Read(); // replace Msg with Analouge input string
        Msg = "Analoue Reads 0-7:"+ Msg + "\r";
        sendData(Msg);    // Send Analouge inputs + CR
}
  // Read all analouge inputs routine
String Sensor_Read(){
  String response3 = "" ;
  for (int y = 0; y < sensorPin; y++) { // read ALL analouge inputs
  String an = "A" + String(y,DEC);      // format for Pin numbers
 int f = an.toInt(); // turn string into a Analouge Input pin number
 response3 = response3 + String(analogRead(f),DEC); // add value to string
 if (y < sensorPin-1){
 response3 = response3 +",";    // add a comma if not last number
 }
 }
  return response3;            // return string of numbers to loop
}
[/code]

Credits

adamsstephen

adamsstephen

0 projects • 7 followers

Comments