14. Networking and communications¶
Group assignment¶
This week we played around with input devices, and measured the data that they send.
Software used this week¶
- Arduino IDE
- KiCad
Finished things¶
I had trouble thinking up castable object that would be needed in a pinball machine. Almost all parts would be more sturdy when cast, but the designs are not ready for that yet.
So I decided to create the following objects:
- Internal dialogue I made a PCB for bumper, and had it communicate with the main board with I2C. I2C
Code for the group work¶
Code used in group work
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0x34, 0x85, 0x18, 0x05, 0x0C, 0x7C};
uint8_t broadcastAddress2[] = {0x34, 0x85, 0x18, 0x03, 0x69, 0x24};
typedef struct test_struct {
int x;
int y;
} test_struct;
test_struct test;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
// register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// register second peer
memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if (Serial.available()) {
char byte = Serial.read();
int y = atoi(&byte);
test.y = y;
}
test.x = test.x +1;
if (test.x > 50) { test.x = 0; test.y = test.y +1; }
if (test.y > 50) test.y = 0;
esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));
if (result == ESP_OK) {
char strBuf[50];
sprintf(strBuf, "Sent with success %d, %d", test.x, test.y);
Serial.println(strBuf);
}
else {
Serial.println("Error sending the data");
}
delay(2000);
}
Original code was copied from: https://randomnerdtutorials.com/esp-now-one-to-many-esp32-esp8266/#more-95718
Changes include:
- Reducing the number of connections from three to two
- Writing the correct MAC addresses for the connections
- Reads data from Serial, and sends it through ESP NOW