Hi All,
I'm at the very last step of a project to build a remote control car for my son, and I'm at a bit of a loss. I'm using ESP8266-E12s and ESP-NOW to send signals from the controller ESP8266 to the car ESP8266. Since the ESP8266s only have one analog in (the A0 on the controller is used for acceleration), I've hooked up a Nano to interpret the analog signal from a steering input potentiometer and send it over to the master ESP8266 via TX/RX. I can read out the steering signal from the Nano on the controller ESP8266. I can also send individual values (tested 10 and 180 for example to send over to the steering servo on the car from the controller and it operates as expected). I think the trouble is one of two things:
- Since it's only 1 byte, I might not have set things up to store in a way that I can use the incoming data, and it is just stuck reading the incoming signal.
- The TX/RX signal may not be properly converting to an int and instead could be sending/reading as an unsigned character (I'm sending values 0-255). I've tried to convert this to an integer on the controller side before sending things over to the car via ESP-NOW. I've also tried converting to integer on the car side, and neither seems to work. I'm not sure this is the main issue though as I have a print running on the car and while it is getting acceleration potentiometer information, the steering potentiometer input is either -1, 0, or 255, but those values are all static and do not change when adjusting the potentiometer.
I am unable to further troubleshoot, because when I connect the serial from the Nano to the controller ESP8266, my Serial Monitor gets flooded with the values from the Nano's potentiometer and my serial prints don't work. I tried a method to remedy this, but failed. Anyway, I really wanted to make this work, but this very last step is really throwing me for a loop.
Here's the controller ESP8266 code, I left in some comments that had previous attempts:
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Servo.h>
#include <SoftwareSerial.h>
int rawSpeed; //This is the raw input from the ESP8266 acceleration pot
int potpin; // this is my steering potentiometer
Servo myservo;
byte incomingData; // define a variable to store 8 bits of data<<This is used for the TX/RX Signal
uint8_t broadcastAddress[] = { 0x30, 0x83, 0x98, 0xA5, 0x75, 0x80 };
typedef struct struct_message {
int speeddata; //this is sending properly
int turndata; //this is not (steering data)
} struct_message;
struct_message myData;
unsigned long lastTime = 0;
unsigned long timerDelay = 1;
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
/*Serial.print("\r\nLast Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
*/
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
// put your main code here, to run repeatedly:
rawSpeed = analogRead(A0);
//THIS IS THE SECTION THAT IS THROWING ME---
incomingData = Serial.read();
potpin = incomingData;
/* THIS IS AN ATTEMPT VARIATION--- I've tried converting to int on this side as well as the car side, but the car serial print is just not getting this data, although it prints fine on the controller side 0-255 according to potentiometer movement
if (Serial.available() > 0) { // check if there is 1 byte available to read
incomingData = Serial.read(); // read 1 byte of data and store it in the `incomingData` variable
potpin = int(incomingData);
}
incomingData = Serial.read();
potpin = incomingData;
*/
Serial.println("raw speed");
Serial.println(rawSpeed);
delay(100);
Serial.println("turn data");
Serial.println(potpin);
delay(300);
if ((millis() - lastTime) > timerDelay) {
myData.speeddata = rawSpeed;
myData.turndata = potpin;
esp_now_send(0, (uint8_t *)&myData, sizeof(myData));
lastTime = millis();
}
}
Any help is greatly appreciated, thank you!