PHYS-S12 Intro to Digital Fabrication
Radio Communication
Overview:
For this project I worked with Tiger Strake to be able to have our ESPs communicate. We wanted the ESPs to count, for example one serial monitor would show odd numbers while couting up, and the other serial monitor would show even numbers while counting up.
Process:
We connected the ESPs to our computers and then we first started by getting the MAC addresses for our ESPs. We had a small problem where our buad rate was wrong and we would get weird symbols but in the end we figured it out.
The #include "WiFi.h" directive at the beginning of the sketch includes the necessary library to enable Wi-Fi capabilities. The void setup() function is where the initial setup of the program occurs. It starts by initiating the serial communication at a baud rate of 115200 using Serial.begin(115200). The WiFi.mode(WIFI_MODE_STA) statement sets the Wi-Fi mode to Station mode. This means the device will connect to an existing Wi-Fi network as a client rather than acting as an access point. The line Serial.println(WiFi.macAddress()) prints the MAC address of the device to the serial monitor. The MAC address is a unique identifier assigned to network interfaces, including Wi-Fi interfaces. The void loop() function contains a single statement: Serial.println(WiFi.macAddress()). This continuously prints the MAC address to the serial monitor in an infinite loop, allowing you to observe the MAC address while the program is running.
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
}
void loop(){
Serial.println(WiFi.macAddress());
}
Next we uploaded the code shown below with the respective MAC addresses.
Code Snippet 1:
#include
#include
// REPLACE WITH THE MAC Address of your receiver. Code is the same for both boards, with the exception of the following line.
uint8_t broadcastAddress[] = {0x94, 0xB5, 0x55, 0x23, 0xF6, 0xD0}; // this is board no 1.
//uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xB0, 0x34, 0x8C}; this is board no 2
// Variable to store if sending data was successful
String success;
byte incomingByte;
byte outgoingByte;
// 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(&incomingByte, incomingData, sizeof(incomingByte));
Serial.print("Bytes received: ");
Serial.println(len);
}
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;
}
// Register for a callback function that will be called when data is received
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(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);
}
bool isPrime(int num) {
if(num < 2)
return false;
for(int i = 2; i <= sqrt(num); i++) {
if(num % i == 0)
return false;
}
return true;
}
void loop() {
if(Serial.available() > 0) {
int incomingByte = Serial.read();
Serial.println(incomingByte);
int outgoingByte = incomingByte;
do {
outgoingByte++;
} while(!isPrime(outgoingByte));
Serial.println(outgoingByte);
}
}
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &outgoingByte, sizeof(outgoingByte));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(1000);
}
#include
#include
// REPLACE WITH THE MAC Address of your receiver. Code is the same for both boards, with the exception of the following line.
uint8_t broadcastAddress[] = {0xC8, 0xF0, 0x9E, 0xB5, 0x0B, 0x9C}; // this is board no 1.
//uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xB0, 0x34, 0x8C}; this is board no 2
// Variable to store if sending data was successful
String success;
byte incomingByte;
byte outgoingByte;
// 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(&incomingByte, incomingData, sizeof(incomingByte));
Serial.print("Bytes received: ");
Serial.println(len);
}
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;
}
// Register for a callback function that will be called when data is received
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(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() {
Serial.println("Wireless communication using ESP32s");
//outgoingByte = incomingByte+1;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &outgoingByte, sizeof(outgoingByte));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(1000);
}
Final Product