ESP32 not reading inputs

I'm trying to send a struct with X,Y and K over ESP-NOW but whe I try to get readings from the joystik I dont get an valure. am I doing somthing wrong?






/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp32/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

//Großer/Rot:  08:3A:F2:AD:3C:B8
//Kleiner/Blau 08:3A:F2:A8:D6:9C
const int Y_pin = 0;
const int X_pin = 4;
#include <esp_now.h>
#include <WiFi.h>

#include <Wire.h>







// REPLACE WITH THE MAC Address of your receiver 
uint8_t broadcastAddress[] = {0x08, 0x3A, 0xF2, 0xA8, 0xD6, 0x9C}; //kleiner

// Define variables to store BME280 readings to be sent
float x;
float y;
float k;

// Define variables to store incoming readings
float incomingX;
float incomingY;
float incomingK;

// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    float XX;
    float YY;
    float KK;
} struct_message;

// Create a struct_message called BME280Readings to hold sensor readings
struct_message outgoingReadings;

// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;

// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  if (status ==0){
    success = "Delivery Success :)";
  }
  else{
    success = "Delivery Fail :(";
  }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incomingX = incomingReadings.XX;
  incomingY = incomingReadings.YY;
  incomingK = incomingReadings.KK;
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);


  

 
  // 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 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;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
  getReadings();
 
  // Set values to send
  outgoingReadings.XX = x;
  outgoingReadings.YY = y;
  outgoingReadings.KK = k;

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &outgoingReadings, sizeof(outgoingReadings));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  updateDisplay();
  //delay(10000);
}
void getReadings(){
  analogReadResolution(10);
  x = analogRead(X_pin); // HERE I DO NOT GET A VAL
  y = analogRead(Y_pin);// AND HERE
  k = 10.5;
}

void updateDisplay(){
  // Display Readings in Serial Monitor
  Serial.println("INCOMING READINGS");
  
  Serial.println(x);
  Serial.print("x: ");
  Serial.println(incomingReadings.XX);
  Serial.print("y: ");
  Serial.println(incomingReadings.YY);
  Serial.print("k: ");
  Serial.print(incomingReadings.KK);
  Serial.println();
}

What happens if you run a simple program that only prints the analog readings from the joystick to Serial Monitor?

I get the values then withSerial.println(analogRead(x)); but even when I put ths line anywhere in the code it dosen't work

Please post the complete code which you used on the ESP32 to verify that you can get an analog reading from the joystick.

What exact model of ESP32 are you using. Do you have reference to a detailed pin out map?
I don't like the use of IO0 as one of the analog pins as it is the boot mode pin.

const int Y_pin = 0;
const int X_pin = 4;

x = analogRead(X_pin); // HERE I DO NOT GET A VAL
y = analogRead(Y_pin);// AND HERE

1 Like

ESP32 Dev Kit C V2
can't post the reverence of the pinout :frowning:






/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp32/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

//Großer/Rot:  08:3A:F2:AD:3C:B8
//Kleiner/Blau 08:3A:F2:A8:D6:9C
const int Y_pin = 0;
const int X_pin = 4;
#include <esp_now.h>
#include <WiFi.h>

#include <Wire.h>







// REPLACE WITH THE MAC Address of your receiver 
uint8_t broadcastAddress[] = {0x08, 0x3A, 0xF2, 0xA8, 0xD6, 0x9C}; //kleiner

// Define variables to store BME280 readings to be sent
float x;
float y;
float k;

// Define variables to store incoming readings
float incomingX;
float incomingY;
float incomingK;

// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    float XX;
    float YY;
    float KK;
} struct_message;

// Create a struct_message called BME280Readings to hold sensor readings
struct_message outgoingReadings;

// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;

// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  if (status ==0){
    success = "Delivery Success :)";
  }
  else{
    success = "Delivery Fail :(";
  }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incomingX = incomingReadings.XX;
  incomingY = incomingReadings.YY;
  incomingK = incomingReadings.KK;
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);


  

 
  // 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 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;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
  getReadings();
 
  // Set values to send
  outgoingReadings.XX = x;
  outgoingReadings.YY = y;
  outgoingReadings.KK = k;

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &outgoingReadings, sizeof(outgoingReadings));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  updateDisplay();
  //delay(10000);
}
void getReadings(){
  analogReadResolution(10);
  x = analogRead(X_pin);
  y = analogRead(Y_pin);
  k = 10.5;
}

void updateDisplay(){
  // Display Readings in Serial Monitor
  Serial.println("INCOMING READINGS");
  
  Serial.println(x);
  Serial.print("x: ");
  Serial.println(incomingReadings.XX);
  Serial.print("y: ");
  Serial.println(incomingReadings.YY);
  Serial.print("k: ");
  Serial.print(incomingReadings.KK);
  Serial.println();
}

You have reposted your original code where you said you did not see analogRead values.
Can you now see values with that code?

If not, post the simplified code you mentioned worked as a test of the joystick and analogRead().

What happens if you run a simple program that only prints the analog readings from the joystick to Serial Monitor?
I get the values then withSerial.println(analogRead(x));

I tryed pin 34 and 35 an now it works thxs

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.