Hello i have been researching the code needed to connect my arduino Uno project to the internet using a esp8266-01. I have found a nice code that shows a temperature on any web device via the IP, however it has nothing to setup the password and SSID so i have tried to add it myself but i am unsure if it is correct as my esp8266-01 does not arrive till wednesday but i thought i might aswell make a head start. can any one have a look at the code and tell me if it is correct please. i have put uppercase comments where i have added code or adjusted it. Thanks from Kawasaki
/*
Arduino Webserver using ESP8266
Displays temperature in a webpage
UNO pins 2 and 3 as RX and TX. This means that you need to connect the TX line
from the esp to to Arduino's pin 2 and the RX line from the ESP to the Arduino's pin 3.
*/
#include<SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(2, 3); // pins 2 and 3 as RX and TX
String ssid = "Superhub2g"; // ADDED MYSELF !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
String password = "Password90"; // ADDED MYSELF !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void setup()
{
Serial.begin(9600); ///////For Serial monitor
esp8266.begin(9600); ///////ESP Baud rate, was at 115200 CHANGED TO 9600 MYSELF AS I HEAR 115200 IS TO HIGH !!!!!!!!!!!!!!!!!!!!!!!!!
pinMode(11, OUTPUT); /////used if connecting a LED to pin 11
digitalWrite(11, LOW);
String info = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\""; // ADDED MYSELF !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sendData("AT+RST\r\n", 2000, DEBUG); // reset module
sendData("AT+CWMODE=2\r\n", 1000, DEBUG); // configure as access point
sendData("AT+CIFSR\r\n", 1000, DEBUG); // show ip address if one is assigned
sendData(info, 1000, DEBUG); // ADDED MYSELF !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80
}
float sensetemp() ///////function to sense temperature.
{
int val = analogRead(A0);
float mv = ( val / 1024.0) * 5000;
float celcius = mv / 10;
return (celcius);
}
int connectionId;
void loop()
{
if (esp8266.available())
{
/////////////////////Recieving from web browser to toggle led
if (esp8266.find("+IPD,"))
{
delay(3000);
connectionId = esp8266.read() - 48;
if (esp8266.find("pin="))
{
Serial.println("recieving data from web browser");
int pinNumber = (esp8266.read() - 48) * 10;
pinNumber += (esp8266.read() - 48);
digitalWrite(pinNumber, !digitalRead(pinNumber));
}
/////////////////////Sending data to browser
else
{
String webpage = "<h1>Hello World</h1>";
espsend(webpage);
}
if (sensetemp() != 0)
{
String add1 = "<h4>Temperature=</h4>";
String two = String(sensetemp(), 3);
add1 += two;
add1 += "&#x2103"; //////////Hex code for degree celcius
espsend(add1);
}
else
{
String c = "sensor is not conneted";
espsend(c);
}
String closeCommand = "AT+CIPCLOSE="; ////////////////close the socket connection////esp command
closeCommand += connectionId; // append connection id
closeCommand += "\r\n";
sendData(closeCommand, 3000, DEBUG);
}
}
}
//////////////////////////////sends data from ESP to webpage///////////////////////////
void espsend(String d)
{
String cipSend = " AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += d.length();
cipSend += "\r\n";
sendData(cipSend, 1000, DEBUG);
sendData(d, 1000, DEBUG);
}
//////////////gets the data from esp and displays in serial monitor///////////////////////
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp8266.available())
{
char c = esp8266.read(); // read the next character.
response += c;
}
}
if (debug)
{
Serial.print(response); //displays the esp response messages in arduino Serial monitor
}
return response;
}