Devices & Components
Arduino Uno Rev3
LED (generic)
Jumper wires (generic)
Resistor 330 ohm
Capacitor 10 µF
AMS1117
ESP8266 ESP-01
Project description
Code
Arduino Code
arduino
1#include <SoftwareSerial.h> 2 3#define DEBUG true 4 5SoftwareSerial esp8266(2,3); // make TX Arduino line is pin 2, make RX Arduino line is pin 3. 6 7boolean alreadyConnected = false; 8 9// This means that you need to connect the TX line from the esp to the Arduino's pin 2 10 // and the RX line from the esp to the Arduino's pin 3 11void setup() 12{ 13 14 Serial.begin(9600); 15 esp8266.begin(9600); // your esp's baud rate might be different 16 17 pinMode(6,OUTPUT); 18 digitalWrite(6,LOW); 19 20 pinMode(7,OUTPUT); 21 digitalWrite(7,LOW); 22 23 sendData("AT+RST\ \ 24",2000,DEBUG); // reset module 25 sendData("AT+CWMODE=2\ \ 26",1000,DEBUG); // configure as access point 27 sendData("AT+CIFSR\ \ 28",1000,DEBUG); // get ip address 29 sendData("AT+CIPMUX=1\ \ 30",1000,DEBUG); // configure for multiple connections 31 sendData("AT+CIPSERVER=1,80\ \ 32",1000,DEBUG); // turn on server on port 80 33 34} 35 36void loop() 37{ 38 39 String inString = ""; 40 if(esp8266.available()) // check if the esp is sending a message 41 { 42 if (!alreadyConnected) { 43 // clear out the input buffer: 44 esp8266.flush(); 45 Serial.println("We have a new client"); 46 esp8266.println("Hello, client!"); 47 alreadyConnected = true; 48 } 49 50 if(esp8266.find("+IPD,")) 51 { 52 delay(10); // wait for the serial buffer to fill up (read all the serial data) 53 54 int pinNumber = 0; 55 String tempS = ""; 56 esp8266.find("pin="); // advance cursor to "pin=" 57 char temp = esp8266.read(); 58 tempS = temp; 59 60 pinNumber = tempS.toInt(); 61 Serial.println(pinNumber); 62 digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin 63 64 } 65 } 66 67} 68 69 70/* 71* Name: sendData 72* Description: Function used to send data to ESP8266. 73* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no) 74* Returns: The response from the esp8266 (if there is a reponse) 75*/ 76String sendData(String command, const int timeout, boolean debug) 77{ 78 String response = ""; 79 80 esp8266.print(command); // send the read character to the esp8266 81 82 long int time = millis(); 83 84 while( (time+timeout) > millis()) 85 { 86 while(esp8266.available()) 87 { 88 89 // The esp has data so display its output to the serial window 90 char c = esp8266.read(); // read the next character. 91 response+=c; 92 } 93 } 94 95 if(debug) 96 { 97 Serial.print(response); 98 } 99 100 return response; 101 }
Downloadable files
Schematics
Schematics

Schematics
Schematics

Comments
Only logged in users can leave comments