Sending continuous data in between packets ESPNOW

I'm sending data to the receiver side upon change, now I want to send the y2 joystick axis mapped value over espnow to the receiver side and write it as the position of the servo motor. I can't send the joystick value for servo upon change as it may cause fluctuations. How can I modify the code to acheive this.


//--------------------------------------------
//ESP32 remote via ESP-NOW Protocol
//Transmit joy vals- fsr val
//--------------------------------------------
#include <esp_now.h>
#include <WiFi.h>
#include <ESP32MX1508.h>

//------------------------------------------------------------
#define open_pin 12

//FEEDBACK MOTOR
#define ROTA 4
#define ROTB 15
#define CH1 0
#define CH2 1
#define RES 12

MX1508 motorA(ROTA, ROTB, CH1, CH2, RES); //object for yrot motor

int motorSpeedMin = 0;
int motorSpeedMax = 4095;

// Define FSR threshold and motor speed limits
const int fsrThreshold = 500;  // Adjust thresho
int fsrVal = 0;



//// Yrotation button pins
#define CW_BUTTON_PIN  25
#define ACW_BUTTON_PIN  26

//joystick pins
#define x  32
#define y  33
#define z 34
//#define y2 35


//STATES
#define FWD     2
#define REV     1
#define STOP    0
#define OPEN    0
#define CLOSE   1
#define CW      1
#define ACW     2

byte xState = 0;
byte yState = 0;
byte zState = 0;
byte jawState = 0;
byte rotState = 0;

byte lastxState = 0;
byte lastyState = 0;
byte lastzState = 0;
byte lastjawState = 0;
byte lastrotState = 0;

bool move = false;

#define dead_pos 700
#define dead_neg -700


int map_x = 0;
int map_y = 0;
int map_z = 0;
int map_y2 = 0;
int val_jaw = 0;
int val_cw = 0;
int val_acw = 0;

//------------------------------------------------------------
uint8_t RxMACaddress[] = {0xC0, 0x49, 0xEF, 0xD0, 0xDB, 0xB8};
//------------------------------------------------------------
typedef struct TxStruct
{
  byte xData = STOP;
  byte yData = STOP;
  byte zData = STOP;
  bool jaw = CLOSE;
  byte rot = STOP;

} TxStruct;
TxStruct sentData;
//------------------------------------------------------------
typedef struct RxStruct
{
  int fsr;

} RxStruct;
RxStruct receivedData;

 esp_now_peer_info_t peerInfo;
//------------------------------------------------------------
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
  memcpy(&receivedData, incomingData, sizeof(receivedData));

  haptics();
  
}

//===================functions===================================================================

//state change for x link
void change_x() {
  if (map_x < dead_neg || map_x > dead_pos) {
    if (map_x > dead_pos) {
      xState = FWD;
    } else if (map_x < dead_neg) {
      xState = REV;
    }
  }
  else {
    xState = STOP;
  }
  return;
}




//state change for y link
void change_y() {
  if (map_y < dead_neg || map_y > dead_pos) {
    if (map_y > dead_pos) {
      yState = FWD;
    } else if (map_y < dead_neg) {
      yState = REV;
    }
  }
  else {
    yState = STOP;
  }
  return;
}

//state change for z link
void change_z() {
  if (map_z < dead_neg || map_z > dead_pos) {
    if (map_z > dead_pos) {
      zState = FWD;
    } else if (map_z < dead_neg) {
      zState = REV;
    }
  }
  else {
    zState = STOP;
  }
  return;
}


//state change for jaw
void change_jaw() {
  if (val_jaw == LOW ) {
    jawState = OPEN;
  }
  else {
    jawState = CLOSE;
  }
  //return;
}


//state change for ROTATION
void change_rot() {
  if (val_cw == LOW and val_acw == HIGH ) {
    rotState = CW;
  }
  else if (val_cw == HIGH and val_acw == LOW ) {
    rotState = ACW;
  }
  else {
    rotState = STOP;
  }
  //return;
}

//state for haptics
void haptics() {
  fsrVal = receivedData.fsr;

  // Map FSR value to motor speed
  //int motorSpeed = map(fsrVal, fsrThreshold, 4095, motorSpeedMin, motorSpeedMax);

  // Limit motor speed within the defined range
  //motorSpeed = constrain(motorSpeed, motorSpeedMin, motorSpeedMax);

  // Set motor speed
  motorA.motorGo(fsrVal);

}

//======================================================================================

// preparing and sending data
void cast() {
  if (xState != lastxState || yState != lastyState || zState != lastzState || jawState != lastjawState || rotState != lastrotState) {            //This makes the async xTX data flow
    sentData.xData = xState;
    sentData.yData = yState;
    sentData.zData = zState;
    sentData.jaw = jawState;
    sentData.rot = rotState;//Cast the changed state to the xTX callback

    //sending
    esp_err_t result = esp_now_send(RxMACaddress, (uint8_t *) &sentData, sizeof(sentData));


    // getting feedback message
    //feedback();
    if (result == ESP_OK) {
      char x_buf[20];
      char y_buf[20];
      char z_buf[20];
      char j_buf[20];
      char r_buf[20];
      String sx, sy, sz, sj, sr = "";

      if (xState == FWD) {
        sx = "X_REV";
      } else if (xState == REV) {
        sx = "X_FWD";
      } else if (xState == STOP) {
        sx = "X_STOP";
      }

      if (yState == FWD) {
        sy = "Y_REV";
      } else if (yState == REV) {
        sy = "Y_FWD";
      } else if (yState == STOP) {
        sy = "Y_STOP";
      }

      if (zState == FWD) {
        sz = "Z_REV";
      } else if (zState == REV) {
        sz = "Z_FWD";
      } else if (zState == STOP) {
        sz = "Z_STOP";
      }

      if (jawState == OPEN) {
        sj = "OPEN";
      } else if (jawState == CLOSE) {
        sj = "CLOSED";
      }

      if (rotState == CW) {
        sr = "CW";
      } else if (rotState == ACW) {
        sr = "ACW";
      } else if (rotState == STOP) {
        sr = "NO ROT";
      }

      sprintf(x_buf, "Sending %s", sx);
      sprintf(y_buf, "Sending %s", sy);
      sprintf(z_buf, "Sending %s", sz);
      sprintf(j_buf, "Sending %s", sj);
      sprintf(r_buf, "Sending %s", sr);

      Serial.println(x_buf);
      Serial.println(y_buf);
      Serial.println(z_buf);
      Serial.println(j_buf);
      Serial.println(r_buf);//Only prints when packet is sent
    } else {
      Serial.println("Failed to send data over ESP-NOW");
    }
  }

}

//======================================================================================


void setup()
{
  Serial.begin(115200);
  pinMode(open_pin, INPUT_PULLUP);
  pinMode(CW_BUTTON_PIN, INPUT_PULLUP);
  pinMode(ACW_BUTTON_PIN, INPUT_PULLUP);
  motorA.motorBrake();

  //----------------------------------------------------------
  WiFi.mode(WIFI_STA);
  //----------------------------------------------------------
  if (esp_now_init() != ESP_OK)
  {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  //----------------------------------------------------------
 // esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, RxMACaddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  //----------------------------------------------------------
  if (esp_now_add_peer(&peerInfo) != ESP_OK)
  {
    Serial.println("Failed to add peer");
    return;
  }
  //----------------------------------------------------------
  esp_now_register_recv_cb(OnDataRecv);
}
//======================================================================================
void loop()
{
  //reading values
  val_jaw = digitalRead(open_pin);
  val_cw = digitalRead(CW_BUTTON_PIN);
  val_acw = digitalRead(ACW_BUTTON_PIN);
  int val_x = analogRead(x);
  int val_y = analogRead(y);
  int val_z = analogRead(z);
  //int val_y2 = analogRead(y2);

  //mapping
  map_x = map(val_x, 0, 4095, -1000, 1000);
  map_y = map(val_y, 0, 4095, -1000, 1000);
  map_z = map(val_z, 0, 4095, -1000, 1000);
  //map_y2 = map(val_y2, 0, 4095, -1000, 1000);
  //----------------------------------------------------------
  // observing state change
  change_x();
  change_y();
  change_z();
  change_jaw();
  change_rot();


  //casting, sending and getting feedback
  cast();
  //----------------------------------------------------------

  lastxState = xState;
  lastyState = yState;
  lastzState = zState;
  lastjawState = jawState;
  lastrotState = rotState;
  //----------------------------------------------------------
  //delay(1000);
}

This question is somehow specific. But still very generalised.
You just wrote a question posted your code and that is all of your effort.

You should show some more own effort by:

  • writing at least an attempt how you could

and describe where in your code you try to do so

from that question I have the impression you do not understand what your code is doing. You seem to have copy & pasted it and maybe expanded the code based on a visual-pattern (instead of understanding the logic)

best regards Stefan

Thanks for humiliating, but I do understand my code and can explain each line. Asking for doing more effort in asking a question has become a trend nowadays.

Instead of asking me to put more effort you should give the code a read and you'll get my point which I explained above. If you don't understand the code, you can ignore it.

Although it's less likely that you'll understand, I'll reiterate my problem: I'm sending data only on state changes, but now I've added a component (servo motor) at the receiver end which needs continuous data (writing the joystick value) not just a two or three state change like other data being sent.
So for that I'm asking a suggestion on how can I achieve it, without disrupting the whole code functioning (as if I do the visual pattern repeation, the continuously fluctuating joulystick value my block other functions.

ask yourself a question:
which brain did judge my words to be humiliating?

I have read your code and I do undestand the logic of your code.

Though I will not post ready to use code.

Well then it should be easy for you to

  • name the lines of code that need a modification to include your new y2-Data

You could have done an attempt to try it and my estimation is
The probalility that the added code blocks your other functions is pretty low.
And just in case it would block your other functions this new and modified code-version would make a very good base to give hints how to modify it to make it work without blocking.

Add the new y2-data to the data that you do send to the receiver

Expand the if-condition that checks if a value has changed to additionally check if y2 has changed to make your code send the data.

Now this is a very generalised worded suggestion. I have chosen to use these generalised words to put you in the situation that a lot of work is left for you. As you did put your potential helpers into the situation of doing all the work for you.

Again I emphasise that you should put some more own effort into this. From your initial question you put almost all work onto the shoulders of your potential helpers.

best regards Stefan

1 Like

where in you code is the joystick value read and how do you identify the different types of data (y2 and something else) when sending data

That doesn't make any sense. How will sending the same value continuously prevent "fluctuations"? Eventually the joystick will have a different value and you'll start sending that. How is that any different than sending each value only once and then waiting until it changes again?

1 Like

I don't understand that sentence. Please explain in more detail. I suspect you have no factual basis for it.

actually even if the joystick is still, it's values fluctuate and have a +/- of 10

even if the joystick is still, it's values fluctuate and have a +/- of 10

Sounds like a hardware, worn contacts or wiring problem. Try a different joystick.

It can be minimized to +- 3 but still it fluctuates anyway

All sensor data fluctuate, so that should not be a problem.

Good luck with your project.

How will continuously sending that "fluctuating" reading as you proposed help the situation?

Instead, add some hysteresis by setting a minimum threshold by which the reading must change before a new value is accepted. Only when the new value is accepted should you send it by ESPNOW. And, only send it once.

1 Like

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