I’m trying to set up a wireless TCP server, using an Elegoo Arduino Uno (equivalent to the SparkFun RedBoard) and a SparkFun ESP8266 shield. I pieced together the code from the examples that came with the ESP8266 and some other stuff I found on the Web (danger, Will Robinson!). It mostly works, except for the really important part.
From the client, I can send characters to the server, things like “Hello” and “abcdefgh” and “X”. I want the server to print the received characters to my 16x2 LCD (and, for diagnostic purposes, to the Serial Monitor), and to echo the characters back to the client. But it’s not receiving, or decoding, or displaying, the characters properly.
I would settle for it printing an “X” to the LCD every time it receives something, printing the hex value(s) of the received bytes(s) to the serial monitor, and then sending a “Y” back to the client. It does the “X”, it does the “Y”, but the output values all wrong. Am I using the client.read() command wrong?
Here’s the code:
/*
* TCP server
*
* Make sure this is the Elegoo/breadboard,
* at 192.168.0.131 and COM4.
* It would also be useful to have the 16x2 LCD on.
*
* Procedure:
* 1. Get this running on the COM4 Arduino first.
* 2. Open a serial monitor on COM4, press a key and Enter
* to make sure it's working.
* 3. Send one or more characters using a TCP client.
* 5. Anything the client sends should appear in the server's LCD.
*
*/
#include <SoftwareSerial.h> // Include software serial library, ESP8266 library dependency
#include <SparkFunESP8266WiFi.h> // Include the ESP8266 AT library
#include <LiquidCrystal.h> // For the LCD display
const int rs = 12, en = 11, d4 = 6, d5 = 5, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
ESP8266Server server = ESP8266Server(3230);
void setup() {
Serial.begin(9600);
Serial.println("TCP Server");
serialTrigger(F("Press any key to begin."));
lcd.begin(16,2);
lcd.clear();
if (esp8266.begin()) // Initialize the ESP8266 and check its return status
Serial.println("ESP8266 ready to go!"); // Communication and setup successful
else
Serial.println("Unable to communicate with the ESP8266 :(");
int retVal;
retVal = esp8266.connect("Name_of_my_WifiAP", "supersecretpassword");
if (retVal < 0)
{
Serial.print(F("Error connecting: "));
Serial.println(retVal);
}
else
{
server.begin();
IPAddress myIP = esp8266.localIP(); // Get the ESP8266's local IP
Serial.print(F("Server started! IP is: ")); Serial.println(myIP);
lcd.setCursor(0,0); // First line
lcd.print("Server started");
lcd.setCursor(0,1); // Second line
lcd.cursor(); // Turn on cursor
}
}
void loop() {
ESP8266Client client = server.available(500); // 500 ms timeout
if (client)
{
Serial.println(F("Client connected"));
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.print(c, HEX);
lcd.print("X");
client.write("Y");
}
}
// close the connection:
client.stop();
Serial.println(F("Client disconnected"));
}
}
// serialTrigger prints a message, then waits for something
// to come in from the serial port.
// I swiped this from the ESP8266 demo programs. Useful for debugging.
void serialTrigger(String message)
{
Serial.println();
Serial.println(message);
Serial.println();
while (!Serial.available())
;
while (Serial.available())
Serial.read();
}
When the client sends “Hello”, the server prints “XX” to the LCD, successfully responds to the client with a “Y”, and prints hex DD to the serial monitor:
TCP Server
Press any key to begin.
ESP8266 ready to go!
Server started! IP is: 192.168.0.131
Client connected
DD
Client disconnected
If I replace
char c = client.read();
Serial.print(c, HEX);
with
Serial.println(client.read());
… then my serial monitor output looks like this:
TCP Server
Press any key to begin.
ESP8266 ready to go!
Server started! IP is: 192.168.0.131
Client connected
13
13
Client disconnected
… which looks more like a pair of carriage returns (Ctl-D) than hex DD.
This is probably a really simple question that’s been answered a million times. I looked, and couldn’t find the answer. Any ideas?