简体   繁体   中英

How to send orders from Arduino to Esp32 and make it keypress

I need to send orders from Arduino to ESP32.

I have one joystick button to test.

Arduino nano is sender Esp32 is receiver

Esp32 receives the joystick button information from Arduino (each time I push the button).

I need the Esp32 to Serial.write according to the data, for example:

If I press the button in Arduino: Send the data to Esp32 and turn bluetooth on (or turn a led on).

These are my codes:

//Arduino NANO sender
byte j = 45;
#define boton_joystick A1

void setup() {
Serial.begin(9600);
pinMode(boton_joystick, INPUT_PULLUP);
}

int boton_joystick_state;

void loop() {
  //Serial.println("100");
  //Serial.write("BOTON EN GRANDE");
  //delay(1500);

if(!digitalRead(boton_joystick)) {
 boton_joystick_state = 1;
  delay(170);
  } else {
    boton_joystick_state = 0;
    }

  if (boton_joystick_state) {
    Serial.println(j);
    Serial.print(" ");
    Serial.write(j);
    Serial.println();
    }
//ESP-32 receiver
#define RXD2 16
#define TXD2 17

byte j = 45;

int comdata;

void setup() {

  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
  //Serial.print("LEYENDO ARDUINO");
  Serial.println(Serial2.readString());

if (Serial2.available() >0) {
  char comdata = char(Serial2.read());
  if (comdata == 'j') {
    Serial.println("joystick activado");
    }
  }
}

am not sure but am using Nano 33 BLE with UART and Nano has also Serial1 no need to serial2 no need to Softwearserial. Sensd on serial 1 and recive in Serial 1 but also you have to connect it Via USB. so your serial is USB and your serial 1 is TX RX.

for me it work so you can try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM