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

Moderator: igrr

User avatar
By Ansh Verma
#42285 Hello People,

Presently I have two ESP8266 Sparkfun Thing boards (not the dev) - one acting as the softAP and the other as the Station. The SoftAP has a bend sensor (~flex sensor) attached to A0 with the appropriate bias pull-up resistor. I am trying to control a motor on the other ESP8266 using PWM (pin 12 and 13).

After sending each packet of data (or one bend value) wifi communication breaks away and the re-establishes itself when the loop runs again. As a result the rate at which the data is coming is like 1/sec.

I explored a lot of forums and came across some comments on how this may be a power supply issue, but again I added some pull-up resistors to the recommended pins (https://github.com/esp8266/Arduino/blob ... -and-usage) but of no use. I am stuck here real bad, not sure what is happening.

Can someone have some suggestions on what could be causing the sample rates to be soo slow. Even in the sparkfun's hookup guide's example, they comment that "//client disconnects" at the end of the loop. Is this is a hardware issue or some error in my manner of writing the server code .

SOFTAP-
Code: Select all#include <ESP8266WiFi.h>

const char WiFiAPPSK[] = "abcdefgh";
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);
}

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);
}


STATION-
Code: Select all#include <ESP8266WiFi.h>
#include<SPI.h>

char ssid[] = "tyui1E94";          //  your network SSID (name)
char pass[] = "abcdefgh";         // 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();
    //int SPEED = c.toInt();
    //SPEED = map(c,115, 40, 125,225);
    //SPEED = constrain(SPEED,125,225);
    //Serial.println(SPEED);
    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);
}