From c163f6d426d7ad7f82eedaa93056851067f671d5 Mon Sep 17 00:00:00 2001 From: Zhaoliang <zhz03@g.ucla.edu> Date: Thu, 14 Oct 2021 23:31:02 -0700 Subject: [PATCH] upload the esp32 p2p communication code --- .../simple_test/ESP32_master/ESP32_master.ino | 115 ++++++++++++++++++ .../simple_test/ESP32_slave/ESP32_slave.ino | 39 ++++++ 2 files changed, 154 insertions(+) create mode 100644 Code/ESP32_communication/simple_test/ESP32_master/ESP32_master.ino create mode 100644 Code/ESP32_communication/simple_test/ESP32_slave/ESP32_slave.ino diff --git a/Code/ESP32_communication/simple_test/ESP32_master/ESP32_master.ino b/Code/ESP32_communication/simple_test/ESP32_master/ESP32_master.ino new file mode 100644 index 0000000..d06a0bb --- /dev/null +++ b/Code/ESP32_communication/simple_test/ESP32_master/ESP32_master.ino @@ -0,0 +1,115 @@ +//----------------------------------------------------------- +//ESP-NOW: Transmitter +//Ref: Random Nerd Tutorials https://randomnerdtutorials.com +//----------------------------------------------------------- +#include <esp_now.h> +#include <WiFi.h> +//------------------------------------------------------------------------------------- +uint8_t RxMACaddress[] = {0xC4, 0xDD, 0x57, 0x9E, 0x91, 0x74}; +String strdata = ""; +//------------------------------------------------------------------------------------- +typedef struct TxStruct +{ + int potVal; +}TxStruct; +TxStruct sentData; +//------------------------------------------------------------------------------------- +void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) //callback function +{ + Serial.print("\r\nLast Packet Send Status:\t"); + Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); +} +//====================================================================================== +void setup() +{ + Serial.begin(9600); + WiFi.mode(WIFI_STA); + //------------------------------------------------------------------------------------ + if(esp_now_init() != ESP_OK) + { + Serial.println("Error initializing ESP-NOW"); + return; + } + //------------------------------------------------------------------------------------- + esp_now_register_send_cb(OnDataSent); + //------------------------------------------------------------------------------------- + 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; + } +} +//====================================================================================== +void loop() +{ + while (Serial.available()>0){ + int inChar = Serial.read(); + /* + sentData.potVal = inChar - '0'; + delay(10); + esp_now_send(RxMACaddress, (uint8_t *) &sentData, sizeof(sentData)); + delay(500); + */ + strdata += char(inChar); + + if (inChar == '\n'){ + sentData.potVal = strdata.toInt(); + Serial.println(sentData.potVal); + esp_err_t result = esp_now_send(RxMACaddress, (uint8_t *) &sentData, sizeof(sentData)); + //------------------------------------------------------------------------------------- + if (result == ESP_OK) Serial.println("Sent with success"); + else Serial.println("Error sending the data"); + //------------------------------------------------------------------------------------- + delay(500); + strdata = ""; + } + + } + + //------------------------------------------------------------------------------------- + +} + + +/* + * String inString = ""; // string to hold input + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // send an intro: + Serial.println("\n\nString toInt():"); + Serial.println(); +} + +void loop() { + // Read serial input: + while (Serial.available() > 0) { + int inChar = Serial.read(); + if (isDigit(inChar)) { + // convert the incoming byte to a char and add it to the string: + inString += (char)inChar; + } + // if you get a newline, print the string, then the string's value: + if (inChar == '\n') { + Serial.print("Value:"); + Serial.println(inString.toInt()); + Serial.print("String: "); + Serial.println(inString); + // clear the string for new input: + inString = ""; + } + } +} + + * / + */ diff --git a/Code/ESP32_communication/simple_test/ESP32_slave/ESP32_slave.ino b/Code/ESP32_communication/simple_test/ESP32_slave/ESP32_slave.ino new file mode 100644 index 0000000..9680137 --- /dev/null +++ b/Code/ESP32_communication/simple_test/ESP32_slave/ESP32_slave.ino @@ -0,0 +1,39 @@ +//ESP-NOW: Receiver +//Ref: Random Nerd Tutorials https://randomnerdtutorials.com +//----------------------------------------------------------- +#include <esp_now.h> +#include <WiFi.h> +#include <Wire.h> + +//------------------------------------------------------------------------------------- + +//------------------------------------------------------------------------------------- +typedef struct RxStruct +{ + int potVal; +}RxStruct; +RxStruct receivedData; +//------------------------------------------------------------------------------------- +void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) +{ + memcpy(&receivedData, incomingData, sizeof(receivedData)); + Serial.println(receivedData.potVal); +} +//====================================================================================== +void setup() +{ + Serial.begin(11520); + + WiFi.mode(WIFI_STA); + if (esp_now_init() != ESP_OK) + { + Serial.println("Error initializing ESP-NOW"); + return; + } + esp_now_register_recv_cb(OnDataRecv); +} +//====================================================================================== +void loop() +{ + +} -- GitLab