🎉 Black Friday is Here! Enjoy Up to 35% Off ~ Offer Ends November 30th!
In this guide, we will explore how to configure the CodeCell's ESP32-C3 to be used as a WiFi remote, communicating between two devices.
In this example, we will pair two CodeCell devices. Device 1 will gather sensor data and send it to Device 2 via WiFi using the ESP-NOW protocol. We'll start with a simple setup where the proximity sensor on Device 1 controls the onboard LED on Device 2. In the second example, Device 1 will send angular data, and Device 2 will process the data to adjust motor speeds.
Before you can establish communication between the two CodeCell devices, you first need to get the MAC address of the receiver. This MAC address will be used in the sender's code to ensure the correct device receives the data.
Follow these steps to obtain the MAC address of the receiver:
XX:XX:XX:XX:XX:XX
. Copy this address, as you'll need it for the sender's code.This example demonstrates how to send proximity sensor data from Device 1 to Device 2, which will use the data to turn on or off its onboard LED.
#include <esp_now.h>
#include <WiFi.h>
#include <CodeCell.h>
CodeCell myCodeCell;
uint8_t receiverMAC[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX }; // Replace with receiver's MAC address
void setup() {
Serial.begin(115200);
myCodeCell.Init(LIGHT); // Initializes Light Sensing
// Initialize WiFi in Station mode
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverMAC, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if (myCodeCell.Run()) {
uint16_t ProxRead = (myCodeCell.Light_ProximityRead()) >> 4; // Get Proximity Value and Divide by 16
Serial.println(ProxRead);
esp_err_t result = esp_now_send(receiverMAC, (uint8_t *)&ProxRead, sizeof(ProxRead));
if (result == ESP_OK) {
Serial.println("Data sent successfully");
} else {
Serial.println("Sending Error");
}
}
}
#include <esp_now.h>
#include <WiFi.h>
#include <CodeCell.h>
CodeCell myCodeCell;
void setup() {
Serial.begin(115200);
myCodeCell.Init(LIGHT); // Initializes Light Sensing
// Initialize WiFi in Station mode
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the receive callback
esp_now_register_recv_cb(onDataRecv);
}
// Receive callback function
void onDataRecv(const esp_now_recv_info *recvInfo, const uint8_t *incomingData, int len) {
uint16_t Remote_Value;
memcpy(&Remote_Value, incomingData, sizeof(Remote_Value));
Serial.println(Remote_Value);
myCodeCell.LED(0, Remote_Value, 0); // Control onboard LED brightness
}
void loop() {
// Nothing to do here
}
In this second example, we connect two motors with a two DriveCells to the receiver. Device 1 reads angular data from its motion sensors and sends it to Device 2, which adjusts the speed of two motors based on the received data.
If you are using different devices for this example, remember to read the new MAC address of the receiver and replace the placeholder MAC address in the sender's code.
#include <esp_now.h>
#include <WiFi.h>
#include <CodeCell.h>
CodeCell myCodeCell;
uint8_t receiverMAC[] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX}; // Replace with receiver's MAC address
int Roll_Control = 0;
float Roll = 0.0;
float Pitch = 0.0;
float Yaw = 0.0;
void setup() {
Serial.begin(115200);
myCodeCell.Init(MOTION_ROTATION); // Initialize motion sensing
// Initialize WiFi in Station mode
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverMAC, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if (myCodeCell.Run()) {
myCodeCell.Motion_RotationRead(Roll, Pitch, Yaw);
Roll = Roll + 180;
Roll = (Roll * 100) / 180;
Roll = constrain(Roll, 0, 200) / 2;
Roll_Control = (uint8_t)Roll;
Serial.println(Roll_Control);
esp_now_send(receiverMAC, (uint8_t *)&Roll_Control, sizeof(Roll_Control));
}
}
#include <esp_now.h>
#include <WiFi.h>
#include <CodeCell.h>
#include <DriveCell.h>
#define IN1_pin1 2
#define IN1_pin2 3
#define IN2_pin1 5
#define IN2_pin2 6
CodeCell myCodeCell;
DriveCell Motor1(IN1_pin1, IN1_pin2);
DriveCell Motor2(IN2_pin1, IN2_pin2);
void setup() {
Serial.begin(115200);
myCodeCell.Init(LIGHT); // Initialize Light Sensing
Motor1.Init();
Motor2.Init();
// Initialize WiFi in Station mode
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the receive callback
esp_now_register_recv_cb(onDataRecv);
}
void onDataRecv(const esp_now_recv_info *recvInfo, const uint8_t *incomingData, int len) {
int Roll_Speed = 0;
memcpy(&Roll_Speed, incomingData, sizeof(Roll_Speed));
if (Roll_Speed > 50) {
Motor1.Drive(1, Roll_Speed);
Motor2.Drive(1, Roll_Speed);
} else {
Roll_Speed = 100 - Roll_Speed;
Motor1.Drive(0, Roll_Speed);
Motor2.Drive(0, Roll_Speed);
}
Serial.println(Roll_Speed);
}
void loop() {
if (myCodeCell.Run()) {}
}
By following these examples, you can configure two CodeCell devices to communicate over WiFi using ESP-NOW. The examples highlight how to send proximity and angular data between devices and utilize the data for real-time control of LEDs and motors.
Feel free to expand on these projects by incorporating more sensors or additional features to enhance the functionality of your remote system!
Be the first to know about new projects and get exciting deals!
© 2024 Microbots.