Week 13: Networking and communications

  
            assignment
            individual assignment:
               design, build, and connect wired or wireless node(s)
               with network or bus addresses
            group assignment:
               send a message between two projects

            INDIVIDUAL ASSIGNMENT
            This week I worked on networking,  transmitting data from the PCB board to another place. 
            


            

Hardware setup (on PCB Board)

Here is my schematic Here is my PCB Design Generating G-Code PCB milling PCB after milling PCB after soldering In this assignment we try to do Full Duplex Transmission Mode is a mode of transmission in which the flow of data (temperature, humidity) is bi-directional. But here, the devices can send as well as receive data simultaneously at the same time. It means that the receiver can receive as well as send data at the very same time with no hitch. Programming process The programming process started by scanning the MAC address of each board and programming them with a specific message to be send in each board.
           
            #include< WiFi.h>

            void setup() {
              Serial.begin(115200);
              
              // initialize WiFi
              WiFi.mode(WIFI_STA);
              WiFi.disconnect();
                
              // print the MAC address of the ESP32
              Serial.print("MAC address: ");
              Serial.println(WiFi.macAddress());
            }
            
            void loop() {
              // print the MAC address of the ESP32
              Serial.print("MAC address: ");
              Serial.println(WiFi.macAddress());
              delay(2000);
            }
               
           
Then, as seen in the following images, the MAC address is displayed in the serial monitor: The first board's MAC address is seen in the image above. Using a second board attached to the other port with the same code,
           
           #include< mWiFi.h>
  
           void setup() {
             Serial.begin(115200);
             
             // initialize WiFi
             WiFi.mode(WIFI_STA);
             WiFi.disconnect();
               
             // print the MAC address of the ESP32
             Serial.print("MAC address: ");
             Serial.println(WiFi.macAddress());
           }
           
           void loop() {
             // print the MAC address of the ESP32
             Serial.print("MAC address: ");
             Serial.println(WiFi.macAddress());
             delay(2000);
           }
    
           
           
I got the MAC address of the second board as shown with the immage below I then obtained the MAC addresses for my two boards from there. The boards will then be programmed with a message to be sent. Keep in mind that each board will be sending to and receiving from another at the same time if I put the address of another in the code of each board as its receiver. Sender Codes
           
           #include < esp_now.h >
           #include < WiFi.h >
           
           #include < Wire.h >
           
           
           // REPLACE WITH THE MAC Address of your receiver 
           uint8_t broadcastAddress[] = {0x7C, 0x9E, 0xBD, 0x49, 0x4E, 0x28};
           
           // Define variables to store BME280 readings to be sent
           float temperature;
           float humidity;
           float pressure;
           
           // Define variables to store incoming readings
           float incomingTemp;
           float incomingHum;
           float incomingPres;
           
           // Variable to store if sending data was successful
           String success;
           
           //Structure example to send data
           //Must match the receiver structure
           typedef struct struct_message {
               float temp;
               float hum;
               float pres;
           } struct_message;
           
           // Create a struct_message called BME280Readings to hold sensor readings
           struct_message BME280Readings;
           
           // Create a struct_message to hold incoming sensor readings
           struct_message incomingReadings;
           
           esp_now_peer_info_t peerInfo;
           
           // 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(&incomingReadings, incomingData, sizeof(incomingReadings));
             Serial.print("Bytes received: ");
             Serial.println(len);
             incomingTemp = incomingReadings.temp;
             incomingHum = incomingReadings.hum;
             incomingPres = incomingReadings.pres;
           }
            
           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;
             }
           
             // 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;
             }
             // Register for a callback function that will be called when data is received
             esp_now_register_recv_cb(OnDataRecv);
           }
            
           void loop() {
             getReadings();
             delay(1000);  
             // Set values to send
             BME280Readings.temp = temperature;
             BME280Readings.hum = humidity;
             BME280Readings.pres = pressure;
           
             // Send message via ESP-NOW
             esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
              
             if (result == ESP_OK) {
               Serial.println("Sent with success");
             }
             else {
               Serial.println("Error sending the data");
             }
           
             // Display Readings in Serial Monitor
             Serial.println("INCOMING READINGS");
             Serial.print("Temperature: ");
             Serial.print(incomingReadings.temp);
             Serial.println(" ºC");
             Serial.print("Humidity: ");
             Serial.print(incomingReadings.hum);
             Serial.println(" %");
             Serial.print("Pressure: ");
             Serial.print(incomingReadings.pres);
             Serial.println(" hPa");
             Serial.println();
           }
           void getReadings(){
             temperature = 50.1;
             humidity = 40;
             pressure = 80;
           }
    
           
           
The temperature, humidity, and pressure values are all included in the up codes, and they are hard-coded straight into the source code that is sent to other boards. temperature = 50.1, humidity = 40, and pressure =80 Let's imagine that the video below is of a transmitter. As it is sending, It also receing the message from other board and the receiving status is shown in the following image: In the screenshot above, you can see that the most recent packet's status is Delivery Success, which also displays the incoming readings. Receiver Codes
           
             #include < esp_now.h >
             #include < WiFi.h >
             
             #include < Wire.h >
             
             
             // REPLACE WITH THE MAC Address of your receiver 
             uint8_t broadcastAddress[] = {0x94, 0xE6, 0x86, 0x04, 0x89, 0x3C};
             
             // Define variables to store BME280 readings to be sent
             float temperature;    
             float humidity;
             float pressure;
             
             // Define variables to store incoming readings
             float incomingTemp;
             float incomingHum;
             float incomingPres;
             
             // Variable to store if sending data was successful
             String success;
             
             //Structure example to send data
             //Must match the receiver structure
             typedef struct struct_message {
                 float temp;
                 float hum;
                 float pres;
             } struct_message;
             
             // Create a struct_message called BME280Readings to hold sensor readings
             struct_message BME280Readings;
             
             // Create a struct_message to hold incoming sensor readings
             struct_message incomingReadings;
             
             esp_now_peer_info_t peerInfo;
             
             // 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(&incomingReadings, incomingData, sizeof(incomingReadings));
               Serial.print("Bytes received: ");
               Serial.println(len);
               incomingTemp = incomingReadings.temp;
               incomingHum = incomingReadings.hum;
               incomingPres = incomingReadings.pres;
             }
              
             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;
               }
             
               // 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;
               }
               // Register for a callback function that will be called when data is received
               esp_now_register_recv_cb(OnDataRecv);
             }
              
             void loop() {
               getReadings();
               delay(1000);  
               // Set values to send
               BME280Readings.temp = temperature;
               BME280Readings.hum = humidity;
               BME280Readings.pres = pressure;
             
               // Send message via ESP-NOW
               esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
                
               if (result == ESP_OK) {
                 Serial.println("Sent with success");
               }
               else {
                 Serial.println("Error sending the data");
               }
             
               // Display Readings in Serial Monitor
               Serial.println("INCOMING READINGS");
               Serial.print("Temperature: ");
               Serial.print(incomingReadings.temp);
               Serial.println(" ºC");
               Serial.print("Humidity: ");
               Serial.print(incomingReadings.hum);
               Serial.println(" %");
               Serial.print("Pressure: ");
               Serial.print(incomingReadings.pres);
               Serial.println(" hPa");
               Serial.println();
             }
             void getReadings(){
               temperature = 20.1;
               humidity = 99;
               pressure = 120;
             }
      
             
             
The above codes include the message about temperature, humidity and pressure that are being sent to other board. The final packet sent status is shown as Delivery Success in the screenshot above, which also displays the incoming readings. The below video shows how the Receiver works The below video shows how the transmitter works