Data Truncation when sending over Wi-Fi from client to host ESP8266 HUZZAH

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
RalphieRaccoon
 
Posts: 2
Joined: Fri Mar 31, 2023 4:09 pm

Data Truncation when sending over Wi-Fi from client to host ESP8266 HUZZAH

Post by RalphieRaccoon »

Hi,

I have two Adafruit HUZZAH ESP8266 Breakout boards, and I'm trying to send data between them and to a PC. What I currently have is one set as the "host" and the other at the "client". Both receive data in the same format via serial from two Teensy 4.1's, and the client ESP8266 sends the data over Wi-Fi to the host ESP8266, which then combines the data which is retrieved when a PC on the host Wi-Fi network sends a request.

The program on the host works fine, but the data it receives from the client is truncated, with some of the first half of the data missing.

Here's the section of the code from each Teensy 4.1 which sends the data to each ESP8266 via hardware serial 7 (all variables are floats, apart from shoe, which is a string of no more than 5 characters):

Code: Select all

String dataString = "";

dataString += shoe + ","+ String(myTime) + "," + String(T1)+ "," + String(T2)+ "," + String(T3)+ "," + String(T4) + "," + String(T5)+ "," + String(T6)+ "," + String(T7)+ "," + String(T8)+ "," + String(F1)+ "," + String(F2) + "," + String(F3)+ "," + String(F4)+ "," + String(F5)+ "," + String(F6)+ "," + String(F7)+ "," + String(F8) + "," + String(strainout[1])+ "," + String(strainout[2])+ "," + String(strainout[3])+ "," + String(strainout[4])+ "," + String(strainout[5]) + "," + String(strainout[6])+ "," + String(strainout[7])+ "," + String(strainout[8]) + "," + String(strainout[9]) + "," + String(state);

// WIFI print
Serial7.println(dataString);
Here's the ESP8266 "host" program:

Code: Select all

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

#ifndef APSSID
#define APSSID "HappierFeetWifi_Hub"
#endif

/* Set these to your desired credentials. */
const char *ssid = APSSID;

String hubdata;
String clientdata;

ESP8266WebServer server(80);
WiFiServer serialserver(160);

void handleData() {
  server.send(200, "application/json", hubdata + clientdata);
  delay(1);
}

void setup() {
  delay(1000);
  Serial.begin(921600);
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid);

  IPAddress myIP = WiFi.softAPIP();
  server.on("/data", handleData);
  server.begin();
  serialserver.begin();

}

void loop() {

  //hubdata = "";

  if ( Serial.available() ) {
    hubdata = Serial.readStringUntil('\n');  
  }

 WiFiClient client = serialserver.available();

 if (client) {
   while (client.connected()) {
     if (client.available()) {
       clientdata = client.readStringUntil('\n');      
     }
   }
   client.stop();
 }

server.handleClient();

}
And here's the ESP8266 "client" program:

Code: Select all

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <UrlEncode.h>

const char *hub_ssid = "HappierFeetWifi_Hub";
const char *ssid = "HappierFeetWifi_Client";

String data;

WiFiClient client;

void setup() {
  delay(1000);
  Serial.begin(921600);
  WiFi.mode(WIFI_STA);   //station mode
  WiFi.begin(hub_ssid);    //Connect to this SSID.

while (WiFi.status() != WL_CONNECTED) {      //Wait for getting IP assigned by Access Point/ DHCP. 
    delay(500);
  }
}

void loop() {

if ( Serial.available() ) {

    data = Serial.readStringUntil('\n');

    if (client.connect("192.168.4.1", 160)) {

      client.println(data);
      client.stop();
      
    }
  }

 delay(10);

}

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Data Truncation when sending over Wi-Fi from client to host ESP8266 HUZZAH

Post by adafruit_support_mike »

It looks like you're using the simplest possible transmission protocol, which doesn't guarantee complete data transmission.

If you don't want to implement a reliable protocol on your own, it would probably be easiest to use a protocol with reliability built in, like HTTP.

User avatar
RalphieRaccoon
 
Posts: 2
Joined: Fri Mar 31, 2023 4:09 pm

Re: Data Truncation when sending over Wi-Fi from client to host ESP8266 HUZZAH

Post by RalphieRaccoon »

I did try using HTTP GET requests, but the data truncation still occurred.

Here's the code for the host:

Code: Select all

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

#ifndef APSSID
#define APSSID "HappierFeetWifi_Hub"
#endif

/* Set these to your desired credentials. */
const char *ssid = APSSID;

String hubdata;
String clientdata;


ESP8266WebServer server(80);

void handleData() {
  server.send(200, "application/json", hubdata + "\r\n" + clientdata);
}

void handleClientData() {
  clientdata = server.arg(0);
}

void setup() {
  delay(1000);
  Serial.begin(921600);
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid);

  IPAddress myIP = WiFi.softAPIP();
  server.on("/data", handleData);
  server.on("/remote", handleClientData);
  server.begin();
}

void loop() {

  hubdata = "";

  if ( Serial.available() ) {
    hubdata = Serial.readStringUntil('\n');  
  }

  Serial.flush();

  server.handleClient();
}
And here's the code for the client:

Code: Select all

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>

const char *hub_ssid = "HappierFeetWifi_Hub";
const char *ssid = "HappierFeetWifi_Client";

String data;

WiFiClient client;
HTTPClient http;

void setup() {
  delay(1000);
  Serial.begin(921600);
  WiFi.mode(WIFI_STA);   //station mode
  WiFi.begin(hub_ssid);      //Connect to this SSID.

while (WiFi.status() != WL_CONNECTED) {      //Wait for getting IP assigned by Access Point/ DHCP. 
    delay(500);
  }
}

void loop() {

  data = "";

  if ( Serial.available() ) {
    data = Serial.readStringUntil('\n');  
  }

  if (http.begin(client, "http://192.168.4.1/remote?data=" + urlEncode(data))) {
      http.GET();
      http.end();
  }

  delay(10);

}

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Data Truncation when sending over Wi-Fi from client to host ESP8266 HUZZAH

Post by adafruit_support_mike »

Can you post an example of a transmission with truncation please?

It should be nearly impossible to send a GET request where the GET itself arrives but the data just beyond it is missing.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”