Skip to content

Instantly share code, notes, and snippets.

@beniroquai
Last active November 22, 2024 10:26
Show Gist options
  • Select an option

  • Save beniroquai/c23c87d4725b1301cf4c1203b628f848 to your computer and use it in GitHub Desktop.

Select an option

Save beniroquai/c23c87d4725b1301cf4c1203b628f848 to your computer and use it in GitHub Desktop.
ESP-NOW for UC2-ESP?
#include <esp_now.h>
#include <WiFi.h>
struct Command {
char type; // 'M' for motor, 'L' for light
uint8_t id; // Motor or light ID
float value; // Speed, position, or intensity
char mode; // 'R' for relative, 'A' for absolute (for motors)
};
struct PositionNotification {
uint8_t motor_id;
float position;
char status[10]; // e.g., "Reached"
};
void onDataRecv(const uint8_t *mac, const uint8_t *data, int len) {
if (len == sizeof(PositionNotification)) {
PositionNotification notification;
memcpy(&notification, data, len);
Serial.printf("Motor %d reached position %.2f. Status: %s\n",
notification.motor_id, notification.position, notification.status);
}
}
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_recv_cb(onDataRecv);
esp_now_peer_info_t broadcastInfo = {};
memset(broadcastInfo.peer_addr, 0xFF, 6); // Broadcast MAC
broadcastInfo.channel = 0;
broadcastInfo.encrypt = false;
esp_now_add_peer(&broadcastInfo);
}
void loop() {
// Example of sending motor command
Command cmd = {'M', 1, 100.0, 'A'}; // Motor 1, target position 100
esp_now_send(NULL, (uint8_t *)&cmd, sizeof(cmd)); // Broadcast command
delay(1000);
}
#include <esp_now.h>
#include <WiFi.h>
struct Command {
char type; // 'M' for motor, 'L' for light
uint8_t id; // Motor or light ID
float value; // Speed, position, or intensity
char mode; // 'R' for relative, 'A' for absolute (for motors)
};
struct PositionNotification {
uint8_t motor_id;
float position;
char status[10]; // e.g., "Reached"
};
float currentPosition = 0.0; // Simulated motor position
float targetPosition = 0.0;
void onDataRecv(const uint8_t *mac, const uint8_t *data, int len) {
if (len == sizeof(Command)) {
Command cmd;
memcpy(&cmd, data, len);
if (cmd.type == 'M') {
Serial.printf("Motor %d -> Target Position: %.2f, Mode: %c\n", cmd.id, cmd.value, cmd.mode);
targetPosition = cmd.value;
// Start moving the motor to the target position here
}
}
}
void notifyMaster(const uint8_t *master_mac, uint8_t motor_id, float position) {
PositionNotification notification = {motor_id, position, "Reached"};
esp_now_send(master_mac, (uint8_t *)&notification, sizeof(notification));
}
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_recv_cb(onDataRecv);
}
void loop() {
// Simulate motor position reaching the target
if (currentPosition < targetPosition) {
currentPosition += 0.1; // Simulated movement
delay(10); // Simulate time delay for movement
}
if (currentPosition >= targetPosition) {
static bool notified = false;
if (!notified) {
uint8_t master_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Broadcast back to master
notifyMaster(master_mac, 1, currentPosition);
notified = true; // Avoid repeated notifications
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment