Hey Leute, ich arbeite mit zwei ESP32 über ESPNOW und würde gerne mit meinem Programm über strings zahlen für links und Buchstaben für rechts ausgegeben z.B. 5pf bekommen. Ansicht hat mein Programm keine Fehlermeldungen, aber ich bekomme im seriellen monitor die Ausgabe nur am Sender obwohl es beim Receiver angezeigt werden sollte.
Ich glaube das Problem liegt im typedef bei den Strings ich müsste anscheinen arrays of char verwenden. Ich habe zwar recherchiert, aber kaum was gefunden was mir helfen konnte. Ich würde mich freuen, wenn mir jemand das erklären könnte.
//esp32 sender
#include <esp_now.h>
#include <WiFi.h>
int lesel;//speicher für fitting
int leser;
int i; //zähler
int maxl; //gefitteter Wert
int maxr;
int Fitl; //werte für switch
int Fitr;
int wertl;//einzelmessungen
int wertr;
String Links;
String Rechts;
//mac address 94:B9:7E:DA:4C:74
uint8_t broadcastAddress[] = {0x94, 0xB9, 0x7E, 0xDA, 0x4C, 0x74};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
String Links;
String Rechts;
} struct_message;
// Create a struct_message called myData
struct_message myData;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
//fitting(10 messungen werden über zeitraum von 2 sekunden abgenommen und der durchschnitt wird ausgerechnet.
delay(300);
for(i=0;i<=10;i++)
{
lesel= analogRead(34)+lesel;
leser= analogRead(32)+leser;
delay(20);
}
maxl=lesel/10;
maxr=leser/10;
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
else
{
Serial.println("Succes: Initialized ESP-NOW");
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
pinMode(34,INPUT);
pinMode(32,INPUT);
}
void loop() {
wertl= analogRead(34);
Fitl=map(wertl,0,maxl,0,9);
wertr= analogRead(32);
Fitr=map(wertr,0,maxr,0,9);
//steuerung links
Links=Fitl;
//steuerung rechts
switch(Fitr)
{
case 1: Rechts='a';break;//a
case 2: Rechts='b';break;//b
case 3: Rechts='c';break;//c
case 4: Rechts='d';break;//d
case 5: Rechts='e';break;//e
case 6: Rechts='f';break;//f
case 7: Rechts='g';break;//g
case 8: Rechts='h';break;//h
case 9: Rechts='i';break;//i
}
//Steuerung ++
String Steuerung=String('p'+Links+Rechts);//zusammenfügen in string(p ist pfuschlösung)
//send Steuerung
delay(10);
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(2000);
}
//ESP32 receiver
#include <esp_now.h>
#include <WiFi.h>
#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define CHA 0
#define ENA 13 // blau, this pin must be PWM enabled pin if Arduino board is used
#define IN1 5 // braun
#define IN2 4 //gelb
// motor 2 settings
#define IN3 2 // grau
#define IN4 15 // weiß
#define ENB 12// orange, this pin must be PWM enabled pin if Arduino board is used
#define CHB 1
const int CCW = 2; // do not change
const int CW = 1; // do not change
#define motor1 1 // do not change
#define motor2 2 // do not change
// use the line below for two motors
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN2, IN3, ENB, true);
char chama[4];
char Rechts;
char Links;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_data {
//String message ="";
char chama[4];
char Rechts;
char Links;
} struct_data;
// Create a struct_message called myData
struct_data myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
//Serial.begin(115200);
if (len == 0)
{
return;
}
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Rechts: ");
Serial.println(myData.Rechts);
Serial.print("Links: ");
Serial.println(myData.Links);;
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
motor.begin();
//BT.begin(9600);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void simpleMovements(){
//splitting
// char chama[4]="2d4";
Rechts=chama[1];//buchstabe
Links=chama[2];//Zahl
//Serial.println("message"); Serial.println(message); Serial.println("\n");
Serial.println("links"); Serial.println(Links); Serial.println("\n");
Serial.println("rechts"); Serial.println(Rechts);Serial.println("\n");
//richtung links
if(Links == '1') {
motor.rotate(motor1, 80, CW);
}
if(Links == '2') {
motor.rotate(motor1, 120, CW);
}
if(Links == '3') {
motor.rotate(motor1, 160, CW);
}
if(Links == '4') {
motor.rotate(motor1, 200, CW);
}
if(Links == '5') { //stand
motor.rotate(motor1, 0, CW);
}
if(Links == '6') {
motor.rotate(motor1, 200, CCW);
}
if(Links == '7') {
motor.rotate(motor1, 160, CCW);
}
if(Links == '8') {
motor.rotate(motor1, 120, CCW);
}
if(Links == '9') {
motor.rotate(motor1, 80, CCW);
}
//rechts
if(Rechts == 'a') {
motor.rotate(motor2, 80, CW);
}
if(Rechts == 'b') {
motor.rotate(motor2, 120, CW);
}
if(Rechts == 'c') {
motor.rotate(motor2, 160, CW);
}
if(Rechts == 'd') {
motor.rotate(motor2, 200, CW);
}
if(Rechts == 'e') { //stand
motor.rotate(motor2, 0, CW);
}
if(Rechts == 'f') {
motor.rotate(motor2, 200, CCW);
}
if(Rechts == 'g') {
motor.rotate(motor2, 160, CCW);
}
if(Rechts == 'h') {
motor.rotate(motor2, 120, CCW);
}
if(Rechts == 'i') {
motor.rotate(motor2, 80, CCW);
}
}
void loop() {
}