Motor control using two ESP8266 (Slow sample rate)
Posted: Thu Mar 03, 2016 2:39 pm
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) -
SERVER (SOFTAP)
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);
}