client.readStringUntil not giving expected results
Posted: Mon Jan 01, 2018 11:14 pm
I have one ESP8266 configured as a server to send a voltage value read from its ADC pin to an ESP8266 client, The voltage variable appears ok when printed to the serial terminal on the server but comes through as two question marks on the client side. Does someone have a solution for this? Code for both devices as well as the serial logs are shown below.
Server sketch code:
Server serial log:
Client sketch code:
Client serial log:
Server sketch code:
Code: Select all
/* This sketch sets up the ESP8266 as a WiFi softAP, starts a WiFi server with a fixed IP and listens
* for a client's request, then sends the supply voltage taken from the ADC pin back to the client
*/
#include <ESP8266WiFi.h>
int adcvalue = 0;
float LSB = .011887383;
char ssid[] = "ESP07";
char pass[] = "xxxxxxxx";
WiFiServer server(80);
IPAddress ip(192,168,8,1); // IP address of the server
IPAddress gateway(192,168,8,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP); //switching to AP mode
WiFi.softAP(ssid, pass); //initializing the AP with ssid and password. This will create a WPA2-PSK secured AP
WiFi.config(ip, gateway, subnet); // forces to use the fixed IP
WiFi.begin();
Serial.println(".");
Serial.println("Wifi Started");
server.begin(); // starts the server
Serial.println("Server Started");
}
void loop () {
adcvalue = (analogRead(A0));
Serial.print("ADC Value is: ");
Serial.println(adcvalue);
float supplyvoltage = adcvalue*LSB;
Serial.print("Supply Voltage is: ");
Serial.println(supplyvoltage);
char strvoltage[15];
dtostrf(supplyvoltage,6,2,strvoltage);
Serial.print("strvoltage is: ");
Serial.println(strvoltage);
delay(1000);
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println(".");
String request = client.readStringUntil('\r'); // receives the message from the client
Serial.print("From client: "); Serial.println(request);
client.flush();
client.println(strvoltage + '\r'); // sends the answer to the client
}
client.stop(); // terminates the connection with the client
}
}
Server serial log:
Wifi Started
Server Started
ADC Value is: 904
Supply Voltage is: 10.75
strvoltage is: 10.75
ADC Value is: 904
Supply Voltage is: 10.75
strvoltage is: 10.75
ADC Value is: 903
Supply Voltage is: 10.73
strvoltage is: 10.73
ADC Value is: 900
Supply Voltage is: 10.70
strvoltage is: 10.70
ADC Value is: 902
Supply Voltage is: 10.72
strvoltage is: 10.72
ADC Value is: 896
Supply Voltage is: 10.65
strvoltage is: 10.65
ADC Value is: 902
Supply Voltage is: 10.72
strvoltage is: 10.72
ADC Value is: 903
Supply Voltage is: 10.73
strvoltage is: 10.73
ADC Value is: 894
Supply Voltage is: 10.63
strvoltage is: 10.63
ADC Value is: 892
Supply Voltage is: 10.60
strvoltage is: 10.60
ADC Value is: 896
Supply Voltage is: 10.65
strvoltage is: 10.65
ADC Value is: 900
Supply Voltage is: 10.70
strvoltage is: 10.70
ADC Value is: 893
Supply Voltage is: 10.62
strvoltage is: 10.62
ADC Value is: 892
Supply Voltage is: 10.60
strvoltage is: 10.60
ADC Value is: 898
Supply Voltage is: 10.67
strvoltage is: 10.67
ADC Value is: 893
Supply Voltage is: 10.62
strvoltage is: 10.62
ADC Value is: 897
Supply Voltage is: 10.66
strvoltage is: 10.66
.
From client: What is the p/s voltage?
ADC Value is: 893
Supply Voltage is: 10.62
strvoltage is: 10.62
ADC Value is: 891
Supply Voltage is: 10.59
strvoltage is: 10.59
ADC Value is: 893
Supply Voltage is: 10.62
strvoltage is: 10.62
.
From client: What is the p/s voltage?
ADC Value is: 894
Supply Voltage is: 10.63
strvoltage is: 10.63
ADC Value is: 890
Supply Voltage is: 10.58
strvoltage is: 10.58
ADC Value is: 893
Supply Voltage is: 10.62
strvoltage is: 10.62
.
Client sketch code:
Code: Select all
/* This sketch sets up the ESP8266 as a WiFi client with a fixed IP and sends a request to
* the server for it's power supply voltage every 2 seconds and outputs that to the serial
* port along with it's own RSSI. This info can be logged along with a timestamp via minicom.
*/
#include <ESP8266WiFi.h>
const char *ssid = "ESP07";
const char *pass = "xxxxxxxx"; //the password should be 8 char or more
IPAddress ip(192,168,8,2); // set the client's address
IPAddress gateway(192, 168, 8, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(192,168,8,1); // the fixed IP address of the server
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Waiting for connection to softAP...");
delay(1000);
}
Serial.println("Connected to wifi");
}
void loop () {
client.connect(server, 80); // Connection to the server
Serial.println(".");
client.println("What is the p/s voltage?\r"); // sends the message to the server
delay(1000);
String answer = client.readStringUntil('\r'); // receives the answer from the sever
Serial.print("Server Voltage: ");
Serial.println(answer);
client.flush();
Serial.print("RSSI: "); Serial.println(WiFi.RSSI());
delay(2000);
}
Client serial log:
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Waiting for connection to softAP...
Connected to wifi
.
Server Voltage: ��
RSSI: -55
.
Server Voltage: ��
RSSI: -55
.