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.
Components and supplies
1
ESP8266 ESP-01
1
Stripboard
1
Arduino Mega 2560
1
Arduino Leonardo
1
Arduino Due
1
Resistor 1k ohm
1
Arduino Nano R3
1
Ardunio Header strips
1
Arduino UNO
1
LED (generic)
1
Wire Cable - By the Foot
Tools and machines
1
Soldering iron (generic)
1
Pliers
1
Reel of Solder
Apps and platforms
1
Arduino IDE
1
TCPIP Client
1
Realterm for PC
1
TCP Console for Apple Ipad and Iphone
Project description
Code
ESP-01 and UNO Webserver Remote Control
arduino
Control your Arduino board via WiFi and get a feedback
1[code] 2// You may use on board LED connected to pin 13 3// Elminated delays and serial monitor commands, reduced response time to 5 seconds. 4#include <SoftwareSerial.h> 5 6int serialRx = 2; // software serial RX TX 7int serialTx = 3; // ESP-01 RX goes to this port, TX goes to Port 2 8int d = 0; 9// ESP-01 CH_PD pin 7 Must go to 3.3v, not +5v to enable operation as UART (AP) 10// UNO R3 has enough power to make it work. 11// No other connections required 12String inMsg ; // variable to collect strings 13int sensorPin = 8; // set maximum number of pins for the Analouge inputs 14int LEDPin = 13 ; // variable to assign pin to LED. 15int responseTime = 10; //communication timeout 16SoftwareSerial portOne(serialRx, serialTx); // communications port to ESP-01 17 String Msg = ""; // to collect Analouge input data 18void setup() { 19 20// Serial.begin(19200); // for debugging only 21 22String msg ="" ; 23 portOne.begin(115200); // Start software serial port 24 delay(200); 25 26 sendToWifi("ATE0",100); // set Echo Off. Otherwise echo was ON. 27 sendToWifi("AT+CWMODE=2",responseTime); // configure as access point on Chinese version 28 // sendToWifi("AT+CWMODE_CUR = 2",responseTime); // configure as access point (AP) 29 sendToWifi("AT+CIPMUX=1",responseTime); // configure for multiple connections 30 // AT+CIPAP=<ip>[,<gateway>,<netmask>] 31 sendToWifi("AT+CIPAP_CUR=\\"192.168.5.1\\"",responseTime); // Set IP address for AP 32 sendToWifi("AT+CIPSERVER=1,80",responseTime); // Create and turn on server on port 80 33 sendToWifi("AT+CWDHCP_CUR=0,1",responseTime); // Set AP with DHCP ON (required) 34 //AT+CWSAP=<ssid>,<pwd>,<chl>,<ecn>[,<max conn>][,<ssid hidden>] 35 // Open AP Port 36 sendToWifi("AT+CWSAP=\\"ESP8266\\",\\"1234567890\\",5,3,1,0",responseTime); // Set Soft AP parameters problems sending " characters 37 sendToWifi("AT+CIPAP_CUR?",responseTime); 38 sendToWifi("AT+CIPSTO=600",responseTime); // Set timeout to 10 minutes 39 pinMode(LEDPin,OUTPUT); // Open Port to operate LED on board 40// //serial.println("Setup is done!"); 41 // delay(2000); 42} 43 44void loop() { 45 //Serial.println("Waiting...."); 46 if(portOne.available()>0){ 47 inMsg = readFromWifi(); 48 //serial.print("Received msg inside loop = "); 49 inMsg.toUpperCase(); 50 // Serial.println(inMsg); 51 // Messages in CAPITALS 52 if (inMsg.endsWith("HELLO") ) { 53 // Serial.println ("Wifi says : Hello"); 54 sendData("Wifi says : Hello\ "); // Send confirmation of Command + CR 55 Send_inputs(); 56 } 57 else if (inMsg.endsWith("LEDON") ){ 58 digitalWrite(LEDPin,HIGH); // If just LEDON turn on Pin 13 59 sendData("Wifi says : LEDON\ "); // Send confirmation of Command + CR 60 Send_inputs(); // Send Analouge inputs 61 } 62 else if (inMsg.endsWith("LEDOFF") ){ 63 String msg = SETLEDS(0) ; // set all LEDs off. 64 sendData("Wifi says : LEDOFF\ "); // Send confirmation of Command + CR 65 Send_inputs(); // Send Analouge inputs 66 } 67 else if (inMsg.indexOf("LEDONN",0)>6 ){ // if Command is to set a Number 68 d = inMsg.indexOf("LEDONN",0)+6; // Set d to next letter after command // string starts with zero 69 int a = 0; // zero collected number 70 int dt = inMsg.length(); // find end of string (CR) 71 if ( d+3 < dt){ // if there is a 4 digit number after command 72 a = inMsg.substring(d, d+4).toInt(); // store 4 digits in number a 73 String msg ="ERROR"; 74 if ((a > 0) & (a < 1024) ) { // Command not used for zero (use LEDOFF), avoids no number available 75 msg = SETLEDS(a) ; // set all LEDs on or off, returns Binary string as confirmation 76 } 77 msg = "Wifi says : LEDONN="+msg+"\ "; 78 sendData(msg ); // Send confirmation of Command + CR 79 Send_inputs(); // Send Analouge inputs 80 } 81 } 82 // delay(9000); // deliberately large delay to help collect dump of serial port. 83} 84 inMsg=""; // Clear buffer 85 delay(800); // delay to stop duplicate messages 86} 87void sendToWifi(String command, const int timeout){ 88// only send command, not intereted in response 89 String response = ""; 90 // Serial.print("Sent command to ESP8266-E12: "); 91 // Serial.print(command); 92 delay(50); 93 portOne.println(command); 94 long int time = millis(); 95 while( (time+timeout) > millis()) 96 { 97 while(portOne.available()) 98 { 99 char c = portOne.read(); 100 response+=c; 101 } 102 } 103 104 // Serial.print("Response from ESP8266-E12: "); 105 // Serial.print(response); 106} 107 108String readFromWifi(){ 109 char arrayInMsg[100]; 110 String tempStr; 111 int count =0; 112 while( portOne.available() > 0 ){ 113 114 arrayInMsg[count]= portOne.read(); 115 delay(80); 116 // get out if you see CR or LF 117 if ((arrayInMsg[count]=='\ 118') or (arrayInMsg[count]=='\ ')) break; 119 // changed character array comparison or it causes errors 120 count++; 121 } 122 arrayInMsg[count] = '\\0'; // Null terminate finally 123 tempStr = String (arrayInMsg); // cast it to string 124 tempStr.trim(); // precaution 125 // Serial.print(tempStr); 126 return tempStr; 127} 128/* 129* Name: sendData 130* Description: Function used to send string to tcp client using cipsend 131* Params: 132* Returns: void 133*/ 134 void sendData(String str){ 135 String len=""; 136 len+=str.length(); 137 sendToWifi("AT+CIPSEND=0,"+len,responseTime); // Setup command to send str data length to channel 0 138 delay(100); 139 sendToWifi(str,responseTime);//Send String str with 0 closing the transmission and return to AT mode 140 delay(100); 141 do { 142 delay(100); 143 } while (portOne.available()==0); // wait untill response data "sent" or "failed" 144} 145 146String SETLEDS(int long pulse) { 147 pulse= pulse << 4; // move numbers up to pin 4 148 String response2 = "" ; 149 for ( int x = 13; x >3 ; x--) { // Pins 13 down to 4 150 if (pulse & (1 << x)) { // compare pulse with bit number of Digital output port 151 digitalWrite(x,HIGH); 152 response2 = response2 + "1"; 153 // Serial.print("1"); // just for testing 154 } 155 else { 156 digitalWrite(x,LOW); 157 response2 = response2 + "0"; 158 // Serial.print("0"); // just for testing 159 } 160 } 161 // Serial.println(""); // just for testing 162 return response2; // Return Binary string 163 } 164// Send Analouge data 165void Send_inputs(){ 166 delay (1000); 167 Msg = Sensor_Read(); // replace Msg with Analouge input string 168 Msg = "Analoue Reads 0-7:"+ Msg + "\ "; 169 sendData(Msg); // Send Analouge inputs + CR 170} 171 // Read all analouge inputs routine 172String Sensor_Read(){ 173 String response3 = "" ; 174 for (int y = 0; y < sensorPin; y++) { // read ALL analouge inputs 175 String an = "A" + String(y,DEC); // format for Pin numbers 176 int f = an.toInt(); // turn string into a Analouge Input pin number 177 response3 = response3 + String(analogRead(f),DEC); // add value to string 178 if (y < sensorPin-1){ 179 response3 = response3 +","; // add a comma if not last number 180 } 181 } 182 return response3; // return string of numbers to loop 183} 184[/code]
ESP-01 and UNO Webserver Remote Control
arduino
Control your Arduino board via WiFi and get a feedback
1[code] 2// You may use on board LED connected to pin 13 3// Elminated delays and serial monitor commands, reduced response time to 5 seconds. 4#include <SoftwareSerial.h> 5 6int serialRx = 2; // software serial RX TX 7int serialTx = 3; // ESP-01 RX goes to this port, TX goes to Port 2 8int d = 0; 9// ESP-01 CH_PD pin 7 Must go to 3.3v, not +5v to enable operation as UART (AP) 10// UNO R3 has enough power to make it work. 11// No other connections required 12String inMsg ; // variable to collect strings 13int sensorPin = 8; // set maximum number of pins for the Analouge inputs 14int LEDPin = 13 ; // variable to assign pin to LED. 15int responseTime = 10; //communication timeout 16SoftwareSerial portOne(serialRx, serialTx); // communications port to ESP-01 17 String Msg = ""; // to collect Analouge input data 18void setup() { 19 20// Serial.begin(19200); // for debugging only 21 22String msg ="" ; 23 portOne.begin(115200); // Start software serial port 24 delay(200); 25 26 sendToWifi("ATE0",100); // set Echo Off. Otherwise echo was ON. 27 sendToWifi("AT+CWMODE=2",responseTime); // configure as access point on Chinese version 28 // sendToWifi("AT+CWMODE_CUR = 2",responseTime); // configure as access point (AP) 29 sendToWifi("AT+CIPMUX=1",responseTime); // configure for multiple connections 30 // AT+CIPAP=<ip>[,<gateway>,<netmask>] 31 sendToWifi("AT+CIPAP_CUR=\\"192.168.5.1\\"",responseTime); // Set IP address for AP 32 sendToWifi("AT+CIPSERVER=1,80",responseTime); // Create and turn on server on port 80 33 sendToWifi("AT+CWDHCP_CUR=0,1",responseTime); // Set AP with DHCP ON (required) 34 //AT+CWSAP=<ssid>,<pwd>,<chl>,<ecn>[,<max conn>][,<ssid hidden>] 35 // Open AP Port 36 sendToWifi("AT+CWSAP=\\"ESP8266\\",\\"1234567890\\",5,3,1,0",responseTime); // Set Soft AP parameters problems sending " characters 37 sendToWifi("AT+CIPAP_CUR?",responseTime); 38 sendToWifi("AT+CIPSTO=600",responseTime); // Set timeout to 10 minutes 39 pinMode(LEDPin,OUTPUT); // Open Port to operate LED on board 40// //serial.println("Setup is done!"); 41 // delay(2000); 42} 43 44void loop() { 45 //Serial.println("Waiting...."); 46 if(portOne.available()>0){ 47 inMsg = readFromWifi(); 48 //serial.print("Received msg inside loop = "); 49 inMsg.toUpperCase(); 50 // Serial.println(inMsg); 51 // Messages in CAPITALS 52 if (inMsg.endsWith("HELLO") ) { 53 // Serial.println ("Wifi says : Hello"); 54 sendData("Wifi says : Hello\ "); // Send confirmation of Command + CR 55 Send_inputs(); 56 } 57 else if (inMsg.endsWith("LEDON") ){ 58 digitalWrite(LEDPin,HIGH); // If just LEDON turn on Pin 13 59 sendData("Wifi says : LEDON\ "); // Send confirmation of Command + CR 60 Send_inputs(); // Send Analouge inputs 61 } 62 else if (inMsg.endsWith("LEDOFF") ){ 63 String msg = SETLEDS(0) ; // set all LEDs off. 64 sendData("Wifi says : LEDOFF\ "); // Send confirmation of Command + CR 65 Send_inputs(); // Send Analouge inputs 66 } 67 else if (inMsg.indexOf("LEDONN",0)>6 ){ // if Command is to set a Number 68 d = inMsg.indexOf("LEDONN",0)+6; // Set d to next letter after command // string starts with zero 69 int a = 0; // zero collected number 70 int dt = inMsg.length(); // find end of string (CR) 71 if ( d+3 < dt){ // if there is a 4 digit number after command 72 a = inMsg.substring(d, d+4).toInt(); // store 4 digits in number a 73 String msg ="ERROR"; 74 if ((a > 0) & (a < 1024) ) { // Command not used for zero (use LEDOFF), avoids no number available 75 msg = SETLEDS(a) ; // set all LEDs on or off, returns Binary string as confirmation 76 } 77 msg = "Wifi says : LEDONN="+msg+"\ "; 78 sendData(msg ); // Send confirmation of Command + CR 79 Send_inputs(); // Send Analouge inputs 80 } 81 } 82 // delay(9000); // deliberately large delay to help collect dump of serial port. 83} 84 inMsg=""; // Clear buffer 85 delay(800); // delay to stop duplicate messages 86} 87void sendToWifi(String command, const int timeout){ 88// only send command, not intereted in response 89 String response = ""; 90 // Serial.print("Sent command to ESP8266-E12: "); 91 // Serial.print(command); 92 delay(50); 93 portOne.println(command); 94 long int time = millis(); 95 while( (time+timeout) > millis()) 96 { 97 while(portOne.available()) 98 { 99 char c = portOne.read(); 100 response+=c; 101 } 102 } 103 104 // Serial.print("Response from ESP8266-E12: "); 105 // Serial.print(response); 106} 107 108String readFromWifi(){ 109 char arrayInMsg[100]; 110 String tempStr; 111 int count =0; 112 while( portOne.available() > 0 ){ 113 114 arrayInMsg[count]= portOne.read(); 115 delay(80); 116 // get out if you see CR or LF 117 if ((arrayInMsg[count]=='\n') or (arrayInMsg[count]=='\r')) break; 118 // changed character array comparison or it causes errors 119 count++; 120 } 121 arrayInMsg[count] = '\\0'; // Null terminate finally 122 tempStr = String (arrayInMsg); // cast it to string 123 tempStr.trim(); // precaution 124 // Serial.print(tempStr); 125 return tempStr; 126} 127/* 128* Name: sendData 129* Description: Function used to send string to tcp client using cipsend 130* Params: 131* Returns: void 132*/ 133 void sendData(String str){ 134 String len=""; 135 len+=str.length(); 136 sendToWifi("AT+CIPSEND=0,"+len,responseTime); // Setup command to send str data length to channel 0 137 delay(100); 138 sendToWifi(str,responseTime);//Send String str with 0 closing the transmission and return to AT mode 139 delay(100); 140 do { 141 delay(100); 142 } while (portOne.available()==0); // wait untill response data "sent" or "failed" 143} 144 145String SETLEDS(int long pulse) { 146 pulse= pulse << 4; // move numbers up to pin 4 147 String response2 = "" ; 148 for ( int x = 13; x >3 ; x--) { // Pins 13 down to 4 149 if (pulse & (1 << x)) { // compare pulse with bit number of Digital output port 150 digitalWrite(x,HIGH); 151 response2 = response2 + "1"; 152 // Serial.print("1"); // just for testing 153 } 154 else { 155 digitalWrite(x,LOW); 156 response2 = response2 + "0"; 157 // Serial.print("0"); // just for testing 158 } 159 } 160 // Serial.println(""); // just for testing 161 return response2; // Return Binary string 162 } 163// Send Analouge data 164void Send_inputs(){ 165 delay (1000); 166 Msg = Sensor_Read(); // replace Msg with Analouge input string 167 Msg = "Analoue Reads 0-7:"+ Msg + "\ "; 168 sendData(Msg); // Send Analouge inputs + CR 169} 170 // Read all analouge inputs routine 171String Sensor_Read(){ 172 String response3 = "" ; 173 for (int y = 0; y < sensorPin; y++) { // read ALL analouge inputs 174 String an = "A" + String(y,DEC); // format for Pin numbers 175 int f = an.toInt(); // turn string into a Analouge Input pin number 176 response3 = response3 + String(analogRead(f),DEC); // add value to string 177 if (y < sensorPin-1){ 178 response3 = response3 +","; // add a comma if not last number 179 } 180 } 181 return response3; // return string of numbers to loop 182} 183[/code]
Downloadable files
1K resistor complusory
Use instead of 220 ohms colours shown in Fritzog.
1K resistor complusory

Comments
Only logged in users can leave comments