ESP8266 + Arduino serial read problem
Posted: Sat Apr 22, 2017 12:57 pm
Hello, super-friends!
I'm encountering a problem when trying to send single bytes from my esp 8266 to arduino over hardware or software serial.
The project is as follows : a webserver hosted on my esp retrieves data from the user (numerical values 0-255), converts to byte then sends over serial to arduino, which is meant to receive the data and adjust parameters on some programming for LEDs accordingly.
what happens: everything on the esp appears to work correctly, the output received through the serial monitor displays what it is supposed to display. however.....
once i try to send the data to my arduino, i hit problems.
The arduino will display two bytes received through serial, however the bytes received are almost always "255" or a random number between 100 and 255. If i set Serial.availiable()>1 to >0, it will display "255" ad infinitum
simple Arduino recieve code:
ESP code (pardon all the commented out print statements and using brightness through all the message handling, quite experimental code)
I'm excluding my wiring diagram, as that appears to not be the issue. i will note that the esp tx is directly connected to the arduino rx, as my understanding is that i don't need a logic-level shift in this direction.
I'm encountering a problem when trying to send single bytes from my esp 8266 to arduino over hardware or software serial.
The project is as follows : a webserver hosted on my esp retrieves data from the user (numerical values 0-255), converts to byte then sends over serial to arduino, which is meant to receive the data and adjust parameters on some programming for LEDs accordingly.
what happens: everything on the esp appears to work correctly, the output received through the serial monitor displays what it is supposed to display. however.....
once i try to send the data to my arduino, i hit problems.
The arduino will display two bytes received through serial, however the bytes received are almost always "255" or a random number between 100 and 255. If i set Serial.availiable()>1 to >0, it will display "255" ad infinitum
simple Arduino recieve code:
Code: Select all
void setup()
{
Serial.begin(9600);
Serial.println("arduino online");
Serial.println("");
}
void loop()
{
Serial.println("looping");
while(Serial.available()<1)
{
Serial.println("serial not yet recieved");
}
Serial.println("serial recieved");
byte b1=Serial.read();
byte b2=Serial.read();
Serial.println(b1);
Serial.println(b2);
}
ESP code (pardon all the commented out print statements and using brightness through all the message handling, quite experimental code)
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
String form = // String form to sent to the client-browser
"<p>"
"<center>"
"<h1>Sorry for the inconvenience, but you may only update one value at a time </h1>"
"<br>"
"<form action='msg1'><p> brightness (0-255) <input type='text' name='msg1' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg2'><p> red max (0-255) <input type='text' name='msg2' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg3'><p> green max (0-255) <input type='text' name='msg3' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg4'><p> blue max (0-255) <input type='text' name='msg4' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg5'><p> red min (0-255) <input type='text' name='msg5' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg6'><p> green min (0-255) <input type='text' name='msg6' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg7'><p> blue min (0-255) <input type='text' name='msg7' size=50 autofocus> <input type='submit' value='Submit'></form>"
"<form action='msg8'><p> Iteration Delay(milliseconds)<input type='text' name='msg8' size=50 autofocus> <input type='submit' value='Submit'></form>"
"</center>";
ESP8266WebServer server(80); //initiate webserver via esp8266webserver.h
#define SSID "SHAH-CLOUD"
#define PWORD "1234567890"
int brightness = 255;
int rMax = 255;
int bMax = 255;
int gMax = 255;
int bMin = 255;
int rMin = 255;
int gMin = 255;
void setup() {
Serial.begin(9600);
//Serial.print(" Welcome to LED Control ");
//Serial.print("Configuring AP");
WiFi.mode(WIFI_AP_STA);
IPAddress ip(1,2,3,4);
IPAddress gateway(1,2,3,1);
IPAddress subnet(255,255,255,0);
WiFi.softAPConfig(ip, gateway, subnet);
//start the accesspoint
WiFi.softAP(SSID, PWORD);
//Serial.print("AP Configured");
// Set up the endpoints for HTTP server, Endpoints can be written as inline functions:
//Serial.print("Configuring server");
server.on("/", []()
{
server.send(200, "text/html", form);
});
server.on("/msg1", handle_msg1); // And as regular external functions:
server.on("/msg2", handle_msg2);
server.on("/msg3", handle_msg3);
server.on("/msg4", handle_msg4);
server.on("/msg5", handle_msg5);
server.on("/msg6", handle_msg6);
server.on("/msg7", handle_msg7);
server.on("/msg8", handle_msg8);
server.begin(); // Start the server
//Serial.print("server configured & started");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
}
void handle_msg1() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg1");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write( byte(1)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
void handle_msg2() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg2");
////Serial.println(msg2);
brightness = msg.toInt();
//Serial.print("");
Serial.write(byte(2)); //writes the form number being submitted
//Serial.print("");
Serial.write( byte(brightness));
//Serial.print("");
//Serial.print(val);
}
void handle_msg3() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg3");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write(byte(3)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
void handle_msg4() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg4");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write(byte(4)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
void handle_msg5() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg5");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write(byte(5)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
void handle_msg6() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg6");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write(byte(6)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
void handle_msg7() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg7");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write(byte(7)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
void handle_msg8() {
server.send(200, "text/html", form); // Send same page so they can send another msg
String msg = server.arg("msg8");
////Serial.println(msg);
brightness = msg.toInt();
//Serial.println("");
Serial.write(byte(8)); //writes the form number being submitted
//Serial.println("");
Serial.write( byte(brightness));
//Serial.println("");
//Serial.println(val);
}
I'm excluding my wiring diagram, as that appears to not be the issue. i will note that the esp tx is directly connected to the arduino rx, as my understanding is that i don't need a logic-level shift in this direction.