I am working on a project to provide WiFi capability for an Arduino using an ESP8266 module, which connects to the Ardiuino via serial. The Arduino has a simple command interface that allows the user to type a command in a terminal and the Arduino responds accordingly. It works fine over USB and Bluetooth.
As part of this WiFi connectivity, I have set up rudimentary TCP passthrough to the serial port. This does work, but I am having some difficulty with reliable transmission. I am using PuTTY for the terminal and there is an inconsistency in that characters get echoed when using a TCP connection although I have never had this happen over serial. I can deal with this by forcing echo off in PuTTY, but wondered whether I need to force echo off in the WiFi client somewhere?
Secondly, when typing in a TCP terminal, the first typed command sequence is always missed. It works fine from the second one onwards. This suggests that maybe one or more initial characters are being lost or the action of starting to type 'wakes up' the connection somehow and it only starts accepting input after the first CRLF.
My code is below. Can anyone see anything obviously wrong, or suggest why I get no response to the fist typed sequence?
This version uses buffering:
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
/***** Web server and client objects *****/
ESP8266WebServer *AR488srv = new ESP8266WebServer(AP.webp);
WiFiServer *passSrv = new WiFiServer(AP.gpibp);
WiFiClient passCli;
/***** Buffers *****/
// HTML page buffer
const uint16_t htmlSize = 4096;
char html[htmlSize];
// Serial buffer
const uint16_t sbSize = 64;
uint8_t sBuf[sbSize];
uint16_t sbPtr = 0;
/***** Buffers *****/
void setup() {
Serial.begin(BAUD);
#ifdef DEBUG_0
Serial.println();
Serial.println(F("Starting WiFi..."));
#endif
// Set defaults
setWifiDefault();
// Start WiFi
startWifi();
// Start webserver
#ifdef DEBUG_0
Serial.println(F("Starting webserver..."));
#endif
startWebServer();
passSrv->begin();
}
void loop() {
// Handle requests for web server
AR488srv->handleClient();
// Is GPIB passthrough enabled?
if (AP.gpib) {
// Handle incoming client connections
if (passCli.connected()) {
// Handle connection
memset(sBuf,'\0',64);
while(passCli.available()){
if (sbPtr<sbSize-1){
sBuf[sbPtr] = passCli.read();
sbPtr++;
}
}
Serial.write(sBuf, sbPtr);
sbPtr = 0;
memset(sBuf,'\0',64);
while(Serial.available()) {
if (sbPtr<sbSize-1){
sBuf[sbPtr] = Serial.read();
sbPtr++;
}
passCli.write((char*)sBuf, sbPtr);
sbPtr = 0;
}
}else{
// Wait for a connection
passCli = passSrv->available();
}
}
}
I have also tried this simpler unbuffered approach but with the same result:
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
***** Web server and client objects *****/
ESP8266WebServer *AR488srv = new ESP8266WebServer(AP.webp);
WiFiServer *passSrv = new WiFiServer(AP.gpibp);
WiFiClient passCli;
/***** Buffers *****/
// HTML page buffer
const uint16_t htmlSize = 4096;
char html[htmlSize];
// Serial buffer
const uint16_t sbSize = 64;
uint8_t sBuf[sbSize];
uint16_t sbPtr = 0;
/***** Buffers *****/
void setup() {
Serial.begin(BAUD);
#ifdef DEBUG_0
Serial.println();
Serial.println(F("Starting WiFi..."));
#endif
// Set defaults
setWifiDefault();
// Start WiFi
startWifi();
// Start webserver
#ifdef DEBUG_0
Serial.println(F("Starting webserver..."));
#endif
startWebServer();
passSrv->begin();
}
void loop() {
// Handle requests for web server
AR488srv->handleClient();
// Is GPIB passthrough enabled?
if (AP.gpib) {
// Handle incoming client connections
if (passCli.connected()) {
// Handle connection
while(passCli.available()){
Serial.write(passCli.read());
}
Serial.write(sBuf, sbPtr);
sbPtr = 0;
memset(sBuf,'\0',64);
while(Serial.available()) {
passCli.write(Serial.read());
}
}else{
// Wait for a connection
passCli = passSrv->available();
}
}
}
I have also searched the documentation, but can't find any reference to turning the echoing of characters off or character loss.