Skip to content
Snippets Groups Projects
Commit c163f6d4 authored by Zhaoliang Zheng's avatar Zhaoliang Zheng
Browse files

upload the esp32 p2p communication code

parent e90d42fb
No related merge requests found
//-----------------------------------------------------------
//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 = "";
}
}
}
* /
*/
//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()
{
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment