Hi,
I am creating a remort control toy using ESP8266. I am able to send two value (X,Y) using an android app. When I received the value in serial monitor it prints like:
16:11:03.125 -> 0.01915,78.555
16:13:30.002 -> 0.01915,78.555
16:13:31.050 -> 0.01915,78.555
16:13:32.073 -> 0.01915,78.555
16:13:33.031 -> 0.01915,78.555
16:14:00.983 -> 0.01915,78.555
16:14:01.995 -> 0.01915,78.555
I need to split this string and assign it to
example: x= 0.01915 and Y = 78.555
But I cannot do this. I am attaching my code here:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
const char *ssid = "ESP-Car1"; //change ssid
const char *password = "12345678"; //change Password
const byte pinBuiltinLed PROGMEM = D4;
unsigned int localPort = 8888; //Port from which we receive UDP packets
const int NTP_PACKET_SIZE = 48; // Buffer size for received packets
char packetBuffer[ NTP_PACKET_SIZE]; //Packet storage buffer
WiFiUDP udp;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(pinBuiltinLed, OUTPUT);
// We start operations with WIFI
Serial.print("Configuring access point...");
Serial.println(ssid);
WiFi.mode(WIFI_OFF); //turn off WIFI
WiFi.mode(WIFI_AP); //Put WIFI In Access Point Mode
WiFi.softAP(ssid, password,11,0); //Run the access point with the given ssid and password on channel 11 (do not interfere with others)
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("HTTP server started");
Serial.println("Starting UDP");
udp.begin(localPort); // initialize UDP
Serial.print("Local port: ");
Serial.println(udp.localPort());
//-----------------------------------------//
}
void loop()
{
int cb = udp.parsePacket();
if (!cb) {}
else {
udp.read(packetBuffer, NTP_PACKET_SIZE);
Serial.println(packetBuffer);
String req=(packetBuffer);
}
}
Thanks in advance.
Something to experiment with
char packetBuffer[] = {"0.01915,78.555"};
char * ptr;
void setup()
{
Serial.begin(115200);
while (!Serial);
ptr = strtok(packetBuffer, ",");
float X = atof(ptr);
Serial.println(X, 5);
ptr = strtok(NULL, "");
float Y = atof(ptr);
Serial.println(Y, 5);
}
void loop()
{
}
Thank you,
I have solved this problem as below. Do you have any suggestion?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#define P_ID 1
unsigned int l_speed = 0;
unsigned int r_speed = 0;
const char *ssid = "ESP-Car1"; //change ssid
const char *password = "12345678"; //change Password
const byte pinBuiltinLed PROGMEM = D4;
unsigned int localPort = 8888; //Port from which we receive UDP packets
const int NTP_PACKET_SIZE = 48; // Buffer size for received packets
char packetBuffer[ NTP_PACKET_SIZE]; //Packet storage buffer
WiFiUDP udp;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(pinBuiltinLed, OUTPUT);
// We start operations with WIFI
Serial.print("Configuring access point...");
Serial.println(ssid);
WiFi.mode(WIFI_OFF); //turn off WIFI
WiFi.mode(WIFI_AP); //Put WIFI In Access Point Mode
WiFi.softAP(ssid, password,11,0); //Run the access point with the given ssid and password on channel 11 (do not interfere with others)
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("HTTP server started");
Serial.println("Starting UDP");
udp.begin(localPort); // initialize UDP
Serial.print("Local port: ");
Serial.println(udp.localPort());
//-----------------------------------------//
}
void loop()
{
int cb = udp.parsePacket();
if (!cb) {}
else {
udp.read(packetBuffer, NTP_PACKET_SIZE);
// Serial.println(packetBuffer);
String req=(packetBuffer);
// String input = "123,456";
int firstVal, secondVal;
for (int i = 0; i < req.length(); i++) {
if (req.substring(i, i+1) == ",") {
firstVal = req.substring(0, i).toInt();
secondVal = req.substring(i+1).toInt();
// Serial.println(firstVal);
// Serial.println(secondVal);
if (firstVal < 0 ){
Serial.print("left:");
Serial.println(secondVal);
Serial.print("right:");
Serial.println(secondVal - firstVal);
}
else {
Serial.print("left:");
Serial.println(secondVal + firstVal );
Serial.print("right:");
Serial.println(secondVal);}
break;
}
}
}
}
Do you have any suggestion?
Why convert packetBuffer to a String when it is perfectly usable as an array of chars ?
"I have solved this problem as below. Do you have any suggestion?"
Sure, use what works and what you understand. Use the String functions to find the location of the ">" and the "," and you are good to go on parsing out the two data sets.
UKHeliBob:
Why convert packetBuffer to a String when it is perfectly usable as an array of chars ?
actually I want to control 2 motor's speed. when I use analogWrite() motor become flicker. I mean it stops and again starts. Here is my code.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#define P_ID 1
#define L_MOTOR 5
#define R_MOTOR 4
unsigned int l_speed = 0;
unsigned int r_speed = 0;
const char *ssid = "ESP-Car1"; //change ssid
const char *password = "12345678"; //change Password
const byte pinBuiltinLed PROGMEM = D4;
unsigned int localPort = 8888; //Port from which we receive UDP packets
const int NTP_PACKET_SIZE = 10; // Buffer size for received packets
char packetBuffer[ NTP_PACKET_SIZE]; //Packet storage buffer
WiFiUDP udp;
void setup()
{
analogWriteRange(255);
pinMode(L_MOTOR, OUTPUT);
pinMode(R_MOTOR, OUTPUT);
analogWrite(L_MOTOR,0);
analogWrite(R_MOTOR,0);
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(pinBuiltinLed, OUTPUT);
// We start operations with WIFI
Serial.print("Configuring access point...");
Serial.println(ssid);
WiFi.mode(WIFI_OFF); //turn off WIFI
WiFi.mode(WIFI_AP); //Put WIFI In Access Point Mode
WiFi.softAP(ssid, password,11,0); //Run the access point with the given ssid and password on channel 11 (do not interfere with others)
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("HTTP server started");
Serial.println("Starting UDP");
udp.begin(localPort); // initialize UDP
Serial.print("Local port: ");
Serial.println(udp.localPort());
//-----------------------------------------//
}
void loop()
{
int cb = udp.parsePacket();
if (!cb) {
analogWrite(L_MOTOR,0);
analogWrite(R_MOTOR,0);
}
else {
udp.read(packetBuffer, NTP_PACKET_SIZE);
// Serial.println(packetBuffer);
String req=(packetBuffer);
// String input = "123,456";
int firstVal, secondVal;
for (int i = 0; i < req.length(); i++) {
if (req.substring(i, i+1) == ",") {
firstVal = req.substring(0, i).toInt();
secondVal = req.substring(i+1).toInt();
// Serial.println(firstVal);
// Serial.println(secondVal);
if (firstVal < 0 ){
l_speed = secondVal;
r_speed = secondVal - firstVal;
analogWrite(L_MOTOR,l_speed);
analogWrite(R_MOTOR,r_speed);
Serial.print("left:");
Serial.println(secondVal);
Serial.print("right:");
Serial.println(secondVal - firstVal);
}
else {
l_speed = secondVal + firstVal;
r_speed = secondVal ;
analogWrite(L_MOTOR,l_speed);
analogWrite(R_MOTOR,r_speed);
Serial.print("left:");
Serial.println(secondVal + firstVal );
Serial.print("right:");
Serial.println(secondVal);}
break;
}
}
}
}
Do I need to use delay(); if I use delay(); it works but I received UDP response every 1 sec. It works but seems slow in respons
actually I want to control 2 motor's speed.
That has nothing to do with converting the input to a String
What will you be sending to set the speed of the two motors ?
Thank you for your reply. I am new in this section. That is why I do not understand your suggestion clearly.
my complete code is here:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#define P_ID 1
#define L_MOTOR 5
#define R_MOTOR 4
unsigned int l_speed = 0;
unsigned int r_speed = 0;
const char *ssid = "ESP-Car1"; //change ssid
const char *password = "12345678"; //change Password
const byte pinBuiltinLed PROGMEM = D4;
unsigned int localPort = 8888; //Port from which we receive UDP packets
const int NTP_PACKET_SIZE = 48; // Buffer size for received packets
char packetBuffer[ NTP_PACKET_SIZE]; //Packet storage buffer
WiFiUDP udp;
void setup()
{
analogWriteRange(255);
pinMode(L_MOTOR, OUTPUT);
pinMode(R_MOTOR, OUTPUT);
analogWrite(L_MOTOR,0);
analogWrite(R_MOTOR,0);
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(pinBuiltinLed, OUTPUT);
// We start operations with WIFI
Serial.print("Configuring access point...");
Serial.println(ssid);
WiFi.mode(WIFI_OFF); //turn off WIFI
WiFi.mode(WIFI_AP); //Put WIFI In Access Point Mode
WiFi.softAP(ssid, password,11,0); //Run the access point with the given ssid and password on channel 11 (do not interfere with others)
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("HTTP server started");
Serial.println("Starting UDP");
udp.begin(localPort); // initialize UDP
Serial.print("Local port: ");
Serial.println(udp.localPort());
//-----------------------------------------//
}
void loop()
{
int cb = udp.parsePacket();
if (!cb) {
analogWrite(L_MOTOR,0);
analogWrite(R_MOTOR,0);
}
else {
udp.read(packetBuffer, NTP_PACKET_SIZE);
Serial.println(packetBuffer);
String req=(packetBuffer);
// String input = "123,456";
int firstVal, secondVal;
for (int i = 0; i < req.length(); i++) {
if (req.substring(i, i+1) == ",") {
firstVal = req.substring(0, i).toInt();
secondVal = req.substring(i+1).toInt();
// Serial.println(firstVal);
// Serial.println(secondVal);
l_speed = (unsigned int)firstVal;
r_speed = (unsigned int)secondVal;
analogWrite(L_MOTOR,l_speed);
analogWrite(R_MOTOR,r_speed);
delay(200);
Serial.print("left:");
Serial.println(l_speed);
Serial.print("right:");
Serial.println(r_speed);
}
}
}
}
I am getting output like this:
17:03:33.222 -> 202,235
17:03:33.222 -> left:202
17:03:33.222 -> right:235
17:03:33.391 -> 202,235
17:03:33.391 -> left:202
17:03:33.391 -> right:235
17:03:33.527 -> 202,234
17:03:33.527 -> left:202
17:03:33.527 -> right:234
17:03:33.662 -> 202,233
17:03:33.662 -> left:202
17:03:33.662 -> right:233
17:03:33.797 -> 202,232
But my motor stops and starts again. The delay() function is not working. I tried millis() also, but out of luck. No idea what to do? do I need to rewrite the code again? Is it for string conversion? Again thank you for your time.
Are the values you get for left and right integers ?
Earlier in this thread you had examples such as
16:11:03.125 -> 0.01915,78.555
which are floats
UKHeliBob:
Are the values you get for left and right integers ?
Earlier in this thread you had examples such as
16:11:03.125 -> 0.01915,78.555
which are floats
From my android apps I remove floats. yes Now they are integers.
Now they are integers.
OK, but the main problem is that the motors are set to the values sent but if there are no values available to be read then 0 is written to them so they stop again immediately
Try this (untested)
void loop()
{
int cb = udp.parsePacket();
if (cb)
{
udp.read(packetBuffer, NTP_PACKET_SIZE);
Serial.println(packetBuffer); //print what we received
char * ptr;
ptr = strtok(packetBuffer, ","); //get first value
unsigned int l_speed = atol(ptr); //convert to unsigned int
Serial.print("left : "); //print the converted value
Serial.println(l_speed);
ptr = strtok(NULL, ""); //get next value
unsigned int r_speed = atol(ptr); //convert to unsigned int
Serial.print("right : "); //print the converted value
Serial.println(r_speed);
analogWrite(L_MOTOR, l_speed); //use the values
analogWrite(R_MOTOR, r_speed);
}
}
Thank you for your suggestions. I will try and let you know
UKHeliBob:
OK, but the main problem is that the motors are set to the values sent but if there are no values available to be read then 0 is written to them so they stop again immediately
Try this (untested)
void loop()
{
int cb = udp.parsePacket();
if (cb)
{
udp.read(packetBuffer, NTP_PACKET_SIZE);
Serial.println(packetBuffer); //print what we received
char * ptr;
ptr = strtok(packetBuffer, ","); //get first value
unsigned int l_speed = atol(ptr); //convert to unsigned int
Serial.print("left : "); //print the converted value
Serial.println(l_speed);
ptr = strtok(NULL, ""); //get next value
unsigned int r_speed = atol(ptr); //convert to unsigned int
Serial.print("right : "); //print the converted value
Serial.println(r_speed);
analogWrite(L_MOTOR, l_speed); //use the values
analogWrite(R_MOTOR, r_speed);
}
}
Seems it is working partially. I need to change Android apps code for the final check. After doing this I will let you know the situation
Thank you