I want to connect Arduino to my Home Wifi and make it serve as a server to access GPIO pins on Arduino from remote (like controlling LED connected to Arduino from remote).
I am using ESP8266 to connect to my wifi network.
I am facing some issues like:
- sometimes it doesn't connect to my wifi router.
- not connecting to my mobile Hotspot.
- not able to serve default and AP mode.
I am following tutorial from http://allaboutee.com/2014/12/30/esp8266-and-arduino-webserver/
Here is my code:
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
Serial.begin(9600);
esp8266.begin(115200); // your esp's baud rate might be different
sendData("AT+RST\r\n",2000,DEBUG); // reset module
// sendData("AT",2000,DEBUG); // send AT command
// sendData("AT+GMR\r\n",2000,DEBUG); // returns firmware
// Serial.println("checking mode");
// sendData("AT+CWMODE=?\r\n",5000,DEBUG); // check supported modes
//
// Serial.println("scanning APs");
// sendData("AT+CWLAP\r\n",21000,DEBUG); // scan list of access points
Serial.println("set mode 1");
sendData("AT+CWMODE=1\r\n",5000,DEBUG); // configure as both mode
// Serial.println("joining AP");
sendData("AT+CWJAP=\"<MY_WIFI_SSID>\",\"<WIFI_PASSWORD>\"\r\n", 16000, DEBUG); // connect to wifi
Serial.println("Testing CIFSR");
sendData("AT+CIFSR\r\n",7000,DEBUG); // get ip address
Serial.println("setting for multiple connection");
sendData("AT+CIPMUX=1",2000,DEBUG); // configure for multiple connections
Serial.println("print MAC address");
sendData("AT+CIPSTAMAC?\r\n",2000,DEBUG); // print current MAC address
Serial.println("set port 80 for server");
sendData("AT+CIPSERVER=1\r\n,80",2000,DEBUG); // turn on server on port 80
}
void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find("+IPD,"))
{
delay(2000);
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 webpage = "<h1>Tyagi IoT</h1><button>LED1</button>";
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
webpage="<button>LED2</button>";
cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,3000,DEBUG);
}
}
}
void sendData(String command, const int timeout, boolean debug)
{
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
Serial.write(esp8266.read());
}
}
}