Week 13

Home Assignment

Networking And Communication

#_Tasks_:)

1. Group assignment:

send a message between two projects.

I interface MQ2 sensor to my first board of Xiao's ESP32 C3 board and a LED to another board of Xiao's ESP32C3, if the value of the sensor(MQ2 gas sensor) gives value greater than 20 then the led will get turn on on the another board.

#_Transmitter_Node... :)

I used the Arduino IDE for programming the First board (Transmitter Board), which is connected with MQ2 gas sensor.

This is the Connection Diagram of Xiao's ESP32 C3 with MQ2 gas sensor.

#_Programming_Transmitter_Board... :)

#include < esp_now.h>
#include < WiFi.h>
#include "MQ135.h"

#define MQ135_pin 2
MQ135 gasSensor = MQ135(MQ135_pin);
int okay =0;
float MQ135_input = 0;
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xEC, 0xDA, 0x3B, 0xBE, 0x91, 0x38};
/*struct __attribute__((packed)) dataPacket {
int state ;
};*/

esp_now_peer_info_t peerInfo;

// callback when data is sent - I CAN CHANGE THIS FUNCTION BELOW
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");
}

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;
//pinMode(pushDown, INPUT);
}


pinMode(MQ135_pin,INPUT);



// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);

// Register peer
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;
}
}

void loop() {



MQ135_input = gasSensor.getPPM();
Serial.println(MQ135_input);
if( MQ135_input > 20)
{

okay=!okay;
Serial.println("CO2 Detected..!");
delay(500);
}

esp_now_send(broadcastAddress, (uint8_t *) &okay, sizeof(okay));

}

#_Code_Explanation... :)

The code first includes the necessary libraries: esp_now.h for ESP-NOW functionality and WiFi.h for WiFi communication (although WiFi is not directly used in this code).
It then defines a constant akash that is set to the pin number 10. This pin is later used to control the LED.
An integer variable okay is declared to store the state of the LED (0 for off, 1 for on).
The OnDataRecv function is defined as a callback function. This function is called whenever data is received from another ESP32 device using ESP-NOW. The function takes three arguments:
mac_addr: The MAC address of the sender device.
incomingData: A pointer to the incoming data.
len: The length of the incoming data.
The memcpy function is used to copy the incoming data into the okay variable. The Serial.print function is used to print the received data to the serial monitor. The digitalWrite function is used to set the LED pin to the value stored in the okay variable, effectively turning the LED on or off based on the received data.
The setup function is called once when the program starts. It performs the following tasks:
Initializes the serial communication at a baud rate of 115200.
Sets the pin mode of pin akash to output, as it will be used to control the LED.
Sets the device mode to Wi-Fi station. While WiFi is not directly used in this code, setting the mode to station might be necessary for proper ESP-NOW functionality on some ESP32 boards.
Initializes ESP-NOW. If the initialization fails, an error message is printed to the serial monitor and the program exits.
Registers the OnDataRecv function as the callback function for receiving ESP-NOW data.
The loop function is called repeatedly after the setup function finishes. However, as it is empty in this code, it doesn't do anything. This means that the program waits for incoming ESP-NOW data after the setup is complete.

#_Receiver_Node... :)

I used the Arduino IDE for programming the Second board (Receiver Board), which is connected with LED.

This is the Connection Diagram of Xiao's ESP32 C3 with LED.

The code sets up an ESP32 to receive data via ESP-NOW. When data is received, the OnDataRec callback function is triggered, which copies the received data into the okay variable, prints it to the Serial Monitor, and sets the output pin (GPIO 10) accordingly. This could be used to control an LED or any other digital output device based on the received data.

#_Programming_Receiver_Board... :)

#include < esp_now.h>
#include < WiFi.h>

const int akash =10;

int okay;



void OnDataRec(const uint8_t *mac_addr, const uint8_t *incomingData, int len) {
memcpy(&okay, incomingData, sizeof(okay));
Serial.print("led ");
Serial.println(okay);
digitalWrite(akash, okay);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
pinMode(akash, OUTPUT);

// 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 receive callback function
esp_now_register_recv_cb(OnDataRecv);
}

void loop() {
// Nothing to do here
}

#_Code_Explanation... :)

Libraries and Global Variables:
#include < esp_now.h> and #include < WiFi.h>: These include the necessary libraries for ESP-NOW and Wi-Fi functionalities.
const int akash = 10; : Defines a constant akash representing GPIO pin 10, which will be used to control an LED or any other output device.
int okay; : A variable to store the received data.
OnDataRec Callback Function:
void OnDataRec(const uint8_t *mac_addr, const uint8_t *incomingData, int len): A callback function that gets called when data is received via ESP-NOW.
memcpy(&okay, incomingData, sizeof(okay)); : Copies the received data into the okay variable.
Serial.print("led "); Serial.println(okay); : Prints the received value to the Serial Monitor.
digitalWrite(akash, okay); : Sets the digital pin defined by akash (pin 10) to the value of okay. This will turn an LED on or off based on the received value (assuming okay is 0 or 1).
Setup Function:
Serial.begin(115200);: Initializes the Serial Monitor at a baud rate of 115200.
pinMode(akash, OUTPUT);: Sets the GPIO pin defined by akash as an output.
WiFi.mode(WIFI_STA);: Sets the ESP32 to station mode (Wi-Fi client).
if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; }: Initializes ESP-NOW and checks if the initialization was successful. If not, it prints an error message and exits the setup function.
esp_now_register_recv_cb(OnDataRec);: Registers the OnDataRec callback function to handle incoming ESP-NOW data.
Loop Function
void loop() {}: The loop function is empty because all the work is done in the OnDataRec callback function when data is received.

#_Final_Output... :)

This is the Output I got when the sensor value is greater than 20 then LED will glow continuously.



Click here to get more about the assignment.

Downloads

Code : client.ino.
Code : server.ino.
Code : Receiver.ino.
Code : Transmitter.ino.



FAB ACADEMY 2024 😎