Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Ansh Verma
#42349 Hello All,

I wish to troubleshoot the issue to control the motor from the flex sensor. I am able to attach a flex sensor on an SOFTAP and then control the motor from a STATION, but the data rate is very slow (~1/sec). This makes the motor control with high latency. I am using the Sparkfun "Thing" board (not dev) for my prototype development.

I made some comments to debug the issue by placing some serial prints and figured out that after each packet the client has to reconnect with the SERVER. This is why the data rate is soo slow. I tried working on the circuit by putting pull-up resistors and pull-down resistors as recommended (https://github.com/esp8266/Arduino/blob ... -and-usage) but that made the SOFTAP not publish any data.

I was hoping I can get some help in this forum. Did anybody underwent through similar issues that I came across? It would be great if some could some insights.

CLEINT (STA) -
Code: Select all#include <ESP8266WiFi.h>
#include<SPI.h>

char ssid[] = "tyui1E94";          //  your network SSID (name)
char pass[] = "********";         // your network password

const int LED_PIN = 5; // Thing's onboard, green LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
#define Dir_Pin 12  // MOTOR +
#define PWM_Pin 13 // MOTOR -
String c;

int status = WL_IDLE_STATUS;
IPAddress server(192,168,4,1);  // ESP command

// Initialize the client library
WiFiClient client;

void setup() {
  Serial.begin(115200);
  connectWiFi();
  printWifiStatus();
  //WiFi.status = WiFi.begin(ssid, pass);
  client.connect(server, 80);
  pinMode( Dir_Pin, OUTPUT );
  digitalWrite( Dir_Pin, LOW );
  pinMode(PWM_Pin, OUTPUT);
}

void connectWiFi()
{
  byte ledStatus = LOW;

  // Set WiFi mode to station (as opposed to AP or AP_STA)
  WiFi.mode(WIFI_STA);

  // WiFI.begin([ssid], [passkey]) initiates a WiFI connection
  // to the stated [ssid], using the [passkey] as a WPA, WPA2,
  // or WEP passphrase.
  WiFi.begin(ssid, pass);

  // Use the WiFi.status() function to check if the ESP8266
  // is connected to a WiFi network.
  while (WiFi.status() != WL_CONNECTED)
  {
    // Blink the LED
    digitalWrite(LED_PIN, ledStatus); // Write LED high/low
    ledStatus = (ledStatus == HIGH) ? LOW : HIGH;

    // Delays allow the ESP8266 to perform critical tasks
    // defined outside of the sketch. These tasks include
    // setting up, and maintaining, a WiFi connection.
    delay(100);
    // Potentially infinite loops are generally dangerous.
    // Add delays -- allowing the processor to perform other
    // tasks -- wherever possible.
  }
  Serial.println("Connected to wifi");
  Serial.println("\nStarting connection...");
}

void loop() {

 if (!client.connected())
  {
    client.connect(server, 80);
    Serial.println("reconnecting");
    delay(100);
  }
 
  while (client.available())
  {
    c = client.readString();
    digitalWrite( Dir_Pin, LOW );
    analogWrite(PWM_Pin, (255-c.toInt()));
    Serial.println(c.toInt());
    Serial.println(c);
    yield();
  }
     
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}


SERVER (SOFTAP)
Code: Select all#include <ESP8266WiFi.h>

const char WiFiAPPSK[] = "*******";
String s;
String DataPacket;

const int LED_PIN = 5; // Thing's onboard, green LED
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read

WiFiServer server(80);

void setup()
{
  initHardware();
  setupWiFi();
  server.begin();
  printWifiStatus();
}

void loop()
{
 // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
  s = analogRead(ANALOG_PIN);
  //String k = String(s, HEX);
  client.println(s);
  Serial.println(s);
  delay(10);
  }
  //client.flush();
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);

  // Do a little work to get a unique-ish name. Append the
  // last two bytes of the MAC (HEX'd) to "Thing-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "tyui" + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i=0; i<AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
}

void initHardware()
{
  Serial.begin(115200);
  pinMode(DIGITAL_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  // Don't need to set ANALOG_PIN as input,
  // that's all it can be.
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}
User avatar
By SGTECH
#55626 Hey Ansh! I don't know if you have already found the solution to the lag problem with the ESP8266 in SOFTAP mode. I had a similar issue myself and after a lot of looking around in forums I came across this solution to the problem: http://stackoverflow.com/questions/2271 ... -very-slow
The original post is regarding the same issue, but for an arduino UNO & Wifi shield. Regardless, the same solution worked for me with an ESP8266. The user "rzcs" says you have to reduce the timeout for the read method of the stream class.
So you basically just have to add: client.setTimeout(200); right after the WiFiClient client = server.available(); before recieving data.
That should solve the issue. Anyway I hope this can be usefull either to you or anyone else having this problem.
Cheers!! :D