Using Esp8266 Thing to Read Data from Arduino

hello,

Im hosting an html file on Sparkfun esp8266 Thing. The page is refreshed each second.

Im trying to print the sensor value that i read from arduino in webpage. I made serial connection between them. esp8266 module connected to arduinos TX and RX pins (pin 0 and 1). I can send data via Serial.print to esp module. I verify that i read serial data from arduino with esp8266 thing using serial monitor.

but when i want to print it in webpage it comes empty. how can i display sensor data on my webpage in esp8266 which i read from arduino.

My Esp8266 Thing code is below:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "myintel";
const char *password = "12345678";

ESP8266WebServer server ( 80 );

String str;


void handleRoot() {

  String html = "<html>\
  <head>\
    <meta http-equiv='refresh' content='3'/>\
    <title>demo</title>\ 
  </head>\
  <body>";
   html+= "serial: ";

  if (Serial.available()) {
    str = Serial.read();  
  }

  Serial.println(str);
   html+= str;
   html+= "</body>\
</html>";

  server.send ( 200, "text/html", html );

  delay(1000);
}

void setup ( void ) {

  Serial.begin ( 9600 );
  WiFi.begin ( ssid, password );
  Serial.println ( "" );

  // Wait for connection
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  Serial.println ( "" );
  Serial.print ( "Connected to " );
  Serial.println ( ssid );
  Serial.print ( "IP address: " );
  Serial.println ( WiFi.localIP() );

  if ( MDNS.begin ( "esp8266" ) ) {
    Serial.println ( "MDNS responder started" );
  }

  server.on ( "/demo.html", handleRoot );
  server.begin();
  Serial.println ( "HTTP server started" );
}

void loop ( void ) {
  server.handleClient();
  
}

Arduino Code is below:

#include <SPI.h>


void setup() {

  pinMode(A3, INPUT);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("connecting...");
  

}


void loop()
{
 

  Serial.print("value A0: ");
  Serial.print(analogRead(A0));
  Serial.print("  value A1:");
  Serial.print(analogRead(A1));
  Serial.print("  value A2:");
  Serial.println(analogRead(A2));

  
delay(500);
}

I have been playing with a esp8266-01 for the past 4 days, i have been very successful to the extent that i created my own library to host a site via a access point with secure encryption and passwords etc. To start pin 0 and 1 are just a straight through from serial to rx tx so they will only work directly typing from a serial monitor. You need to use say pins 6 and 7 or 2 and 3. apart from that i am shocked that you need 4 libraries to do your task i just needed software serial and so do you, just use the AT commands, i will be very nice and share my basic pre library code i.e. standard arduino code in .ino format which uses far less resources than your code.

you will first need to change your esp8266 baud rate to 9600 from 115200, to do this have your eso8266 wired to pin 0 and 1 like you did and from the serial monitor type

AT+UART_DEF=9600,8,1,0,0

make sure you have no spaces before or after the cmd.

next then change the serial monitor baud rate to 9600 and type AT to confirm the swap worked you should get "OK"

You can run my code its very easy to follow i noted it a lot for you

/*
Arduino Access point Webserver using ESP8266
Displays generated stats in a webpage

*/

#include<SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(6,7); // pins 6 and 7 as RX and TX

String ssid = "ESP8266";
String password = "Password1";

float temp = 26;
float hum = 50;
int fan1 = 99;
int fan2 = 75;

void setup()
{
Serial.begin(115200); ///////For Serial monitor
esp8266.begin(9600); ///////ESP Baud rate, was at 115200 CHANGED TO 9600 MYSELF AS I HEAR 115200 IS TO HIGH !!!!!!!!!!!!!!!!!!!!!!!!!
sendData("AT+RST\r\n", 2000, DEBUG); // reset module
delay(5000);
sendData("AT+CWMODE=2\r\n", 1000, DEBUG); // configure as access point 2
delay(40);
sendData("AT+CWSAP="" + ssid + "","" + password + "",1,4\r\n", 1000, DEBUG); // configure as access point with secure connection
delay(40);
sendData("AT+CIFSR\r\n", 1000, DEBUG); // show ip address if one is assigned
delay(40);
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
delay(40);
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80
}

int connectionId;

void loop()
{
temp = random(1, 30); // generate some random values for testing
hum = random(30, 99);
fan1 = random(10, 99);
fan2 = random(10, 99);
if (esp8266.available())
{
Serial.print("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 style1 = "";
espsend(style1);

String refresh = "<meta http-equiv="refresh" content="120">"; // refresh every 2 mins
espsend(refresh);

String style2 = "body{background-color:#eef;}h1{color:#00f;text-align:center;font-family:"FS Lola Regular", sans-serif;}";
espsend(style2);

String style25 = "h2{color:#00f;text-align:center;font-family:"FS Lola Regular", sans-serif;}";
espsend(style25);

String style3 = "#di{width:800px;margin:50px auto;padding:20px 0px 30px 0px;background-color:#dff;}";
espsend(style3);

String style4 = "";
espsend(style4);

String webpage = "<div id="di">

Aerium Botanix Realtime Values

";
espsend(webpage);

String add1 = "

Temperature = "; String two = String(temp, 2); add1 += two; add1 += "&#x2103"; //////////Hex code for degree celcius add1 += "

"; espsend(add1);

String add2 = "

Humidity = ";
String two2 = String(hum, 2);
add2 += two2;
add2 += "%";
add2 += "

";
espsend(add2);

String add3 = "

Fan 1 = ";
String two3 = String(fan1);
add3 += two3;
add3 += "%";
add3 += "

";
espsend(add3);

String add4 = "

Fan 2 = ";
String two4 = String(fan2);
add4 += two4;
add4 += "%";
add4 += "

";
espsend(add4);

String fin = "";
espsend(fin);

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, 50, DEBUG); // the 50 represent the timout code , increase if you are loosing packets
sendData(d, 50, 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;
}

Why is the ESP thing writing to the Serial instance, when the Arduino is not reading anything from the Serial instance?

Every time handleRoot() is called - it is NOT clear how often that is - you read ONE character that the Arduino has sent, rather than ALL the data the Arduino has sent. Why?

KawasakiZx10r:
I have been playing with a esp8266-01 for the past 4 days,

And STILL haven't learned to post code properly. Read the damned stickies at the top of the forum BEFORE you post again. If you don't, you may get to enjoy a timeout.