我有一个 ESP8266 配置为服务器,将从其 ADC 引脚读取的电压值发送到 ESP8266 客户端,当打印到服务器上的串行终端时,电压变量显示正常,但在客户端显示为两个问号。有人对此有解决方案吗?两个设备的代码以及串行日志如下所示。
服务器草图代码:
代码:
全选/* 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
*/
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); //ini
tializing 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
}
}