1. Hardware
2. Software
2.1 ESP8266 library installation
An open-source library is available on github : https://github.com/itead/ITEADLIB_Arduino_WeeESP8266
2.2 Library parameter
As we use the ESP8266 module with a SoftwareSerial connection, we need to edit the ESP8266.H file.
This file is available in :
C:\Users\MyUser\Documents\Arduino\libraries\ITEADLIB_Arduino_WeeESP8266-master :
Uncomment the following line as below :
#define ESP8266_USE_SOFTWARE_SERIAL
2.3 Program
/*
* Libraries
*/
#include "ESP8266.h"
#include
/*
* Definitions
*/
#define SSID "MySSID"
#define PASSWORD "MyKey"
#define HOST_NAME "www.timeapi.org"
#define HOST_PORT (80)
/*
* Declarations
*/
SoftwareSerial mySerial(3, 4); /* RX:D4, TX:D3 */
ESP8266 wifi(mySerial);
uint32_t len;
void setup() {
//Init debug serial connection
Serial.begin(9600);
Serial.println("Serial connection setup [DONE]");
//Init WiFi
while (true) {
Serial.println("WiFi setup [START]");
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("Join AP success\r\n");
if (wifi.disableMUX()) {
Serial.println("WiFi setup [DONE]");
break;
} else {
Serial.println("WiFi setup [MUX ERR]");
}
} else {
Serial.println("WiFi setup [AP ERR]");
}
//If init failed, wait 20s and try again
delay(20000);
}
}
void loop() {
uint8_t buffer[512] = {0};
char *request = "GET /utc/now HTTP/1.1\r\nHost: www.timeapi.org\r\nAccept-Encoding: gzip,deflate\r\nUser-Agent:ESP8266\r\nContent-Type:application/json\r\n\r\n";
if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
if (wifi.send((const uint8_t*)request, strlen(request))) {
Serial.println("Send data [OK]");
}
else {
Serial.println("Send data [ERR]");
}
}
else {
Serial.println("Create tcp [ERR]");
}
len = wifi.recv(buffer, sizeof(buffer), 10000);
if (len > 0) {
Serial.print("Received:[");
for (uint32_t i = 0; i < len; i++) {
Serial.print((char)buffer[i]);
}
Serial.print("]\r\n");
}
if (wifi.releaseTCP()) {
Serial.println("Release tcp [OK]");
} else {
Serial.println("Release tcp [ERR]");
}
delay(20000);
}
hello i have esp8266 module and arduino ethernet and i need to connect from my android mobile direct via wifi to esp8266 and after connection send values from my mobile to open one led from my mobile…i have esp8266 AP mode my code working but after press sometimes one button from my android app the led is light on and light of but sometimes stuck…. can help me? my code is:
#include
#define DEBUG true
SoftwareSerial esp8266(11,10); // make RX Arduino line is pin 11, make TX Arduino line is pin 10.
// This means that you need to connect the TX line from the esp to the Arduino’s pin 11
// and the RX line from the esp to the Arduino’s pin 10
void setup()
{
Serial.begin(9600);
esp8266.begin(115200); // your esp’s baud rate might be different
pinMode(8,OUTPUT);
digitalWrite(8,LOW);
pinMode(6,OUTPUT);
digitalWrite(6,LOW);
pinMode(9,OUTPUT);
digitalWrite(9,LOW);
Serial.println(sendData(“AT+RST\r\n”,2000,DEBUG)); // reset module
Serial.println(sendData(“AT+CWMODE=2\r\n”,1000,DEBUG)); // configure as access point AP=2, STA=1, 3=BOTH
Serial.println(sendData(“AT+CIFSR\r\n”,1000,DEBUG)); // get ip address
Serial.println(sendData(“AT+CIPMUX=1\r\n”,1000,DEBUG)); // configure for multiple connections
Serial.println(sendData(“AT+CIPSERVER=1,80\r\n”,1000,DEBUG)); // turn on server on port 80
}
void loop()
{
/* String t;
while (esp8266.available())
{
t+=(char)esp8266.read();//read byte,convert to char and append it to string
}//end while
Serial.println(t);
delay(2000);
if (t.length())
{ // if string is not empty
if (t==”/?on”)
{
digitalWrite(9, HIGH);
Serial.write(“LED is ON \n”);
}//end if
else if (t==”+IPD,0,457=GET /?off HTTP/1.1″)
{
digitalWrite(9, LOW);
Serial.write(“LED is OFF \n”);
}
}//end if
*/
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find(“+IPD,”))//+IPD,
{
//delay(1000); // wait for the serial buffer to fill up (read all the serial data)
// get the connection id so that we can then disconnect
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
//**************************************************
String t;
esp8266.find(“/?”); // advance cursor to “pin=”
while (esp8266.available())
{
t+=(char)esp8266.read();//read byte,convert to char and append it to string
if (t.length()==2){break;}
}
// t=(char)esp8266.read();
// t+=(char)esp8266.read();
Serial.println(t);
if(t==”on”)
{
digitalWrite(8, HIGH);
Serial.write(“LED is ON \n”);
}
else if(t==”of”)
{
digitalWrite(8, LOW);
Serial.write(“LED is OFF \n”);
}
if(t==”6o”)
{
digitalWrite(6, HIGH);
Serial.write(“LED is ON \n”);
}
else if(t==”6f”)
{
digitalWrite(6, LOW);
Serial.write(“LED is OFF \n”);
}
//***************************************************
/*
esp8266.find(“pin=”); // advance cursor to “pin=”
int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin
*/
// make close command
String closeCommand = “AT+CIPCLOSE=”;
closeCommand+=connectionId; // append connection id
closeCommand+=”\r\n”;
sendData(closeCommand,60,DEBUG); // close connection.
}
}
}//loop
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command – the data/command to send; timeout – the time to wait for a response; debug – print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = “”;
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
Something is missing at the begining of this code:
#include … ???
Hi Leopoldo,
No, nothing is missing.
I followed your tutorial to the letter, but it doesn’t work for me. My monitor keeps repeating itself:
16:07:30.087 -> Serial connection setup [DONE]
16:07:30.087 -> WiFi setup [START]
16:07:40.106 -> WiFi setup [AP ERR]
16:08:00.128 -> WiFi setup [START]
16:08:10.163 -> WiFi setup [AP ERR]
16:08:30.144 -> WiFi setup [START]
16:08:40.174 -> WiFi setup [AP ERR]
The SSID and the password are correct (I double-double checked). What could be wrong? Thanks