← Back to list

Embedded Networking and Communications

April 30, 2024

Embedded Networking and Communications

Challenge


Group Assignment:

This week assignment is pretty easy to do but is complicated when it comes to what we want to do, So I tried my best to stay as simple as I can and Just send the message between two projects.

So I decided to only use a system of identical MCU’s and one input and output so it will not require a lot of coding.

To check the assignment please refer to this LINK


Individual Assignment:

As for the assignment I have to send data between two devices and one of them has to be designed by me { MY XIAO ESP32C3 Board} and for the other device I’m thinking of using (Node MCU or Arduino UNO)–> Let’s Check below the journey of this communication message.

I will read the data from the DHT 22 sensor and then I will send it to NODEMCU to be staged on OLED display.

So the client has to be connected to the server in order to request data.

Let’s go Ahead

Creating the Server

This step is very easy to do as I already have the Board ready I just need to code it.

ESPAsyncWebServer Library


#include "WiFi.h" // WIFI library for the access point creation 
#include "ESPAsyncWebServer.h" // Web server library 
#include <DHT.h> // DHT Library 

#define PIN 8 // Where the DHT data pin is connected
#define TYPE DHT22 // DHT sensor Type.

DHT zaid(PIN,TYPE); // Constructor

// setting the access point network credentials
const char* ssid = "zaidesp";
const char* password = "12345678@";

// Creating AsyncWebServer object on port 80
AsyncWebServer server(80);

// Creating a function to return temperature 
String readTemp() {
  return String(zaid.readTemperature());
}
// Creating function to return Humidity
String readHumi() {
  return String(zaid.readHumidity());
}
void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  zaid.begin();
  Serial.println();
  
  // Setting the ESP as an access point
  Serial.print("Setting AP (Access Point)…");
  // Wifi Access point constructor 
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

// Creating HTTP get request under Temperature name on the server same will be also for the Humidity
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readTemp().c_str()); // Request the data from the sensor and then fetch it as a string through the get request on the server
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readHumi().c_str()); // Request the data from teh sensor and fetch it as string through the get request on the server 
  });
  
  // Start server
  server.begin();
}
 
void loop(){
  
}

Server IP address

Connect to Server

Checking server IP

Temperature page on server

Humidity page on server

Now the server side is ready.

Next is to set the client side.

Client side

For this I will be using NODEMCU board which have esp8266chip.

I will connect OLED display with it to view the data.

For more INformation about this display please refer to this LINK

Now let’s build the connection.

Node MCU with OLED Display

Client library

download the library

Include the library

Include the library by the Zip file


#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;

const char* ssid = "zaidesp"; // server ssid name
const char* password = "12345678@"; // server password (WIFI)

//Server side ip and page name 
const char* serverNameTemp = "http://192.168.4.1/temperature";
const char* serverNameHumi = "http://192.168.4.1/humidity";

#include <Wire.h>
#include <U8g2lib.h> 

U8G2_SSD1306_128X64_NONAME_F_HW_I2C DISPA(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

#define SDA 4 
#define SCL 5


String temperature;
String humidity;

unsigned long previousMillis = 0;
const long interval = 5000; 

void setup() {
  Serial.begin(115200);
  
   pinMode(SDA, OUTPUT);  
   pinMode(SCL, OUTPUT);

   Wire.setClock(100000); 
   DISPA.setBusClock(100000);
   DISPA.setI2CAddress(0x78);
   DISPA.begin();
   DISPA.setFont(u8g2_font_t0_13_tf);
   DISPA.clearBuffer();

  WiFi.begin(ssid, password); // Connecting to the server wifi 
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { // Checking the status of the connection 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval) {
     // Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED ){ 
      temperature = httpGETRequest(serverNameTemp); //get request from the server to return the temperature string value
      humidity = httpGETRequest(serverNameHumi); // get request from the srever to return the humidity string value
      Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity + " %");
      
     
  // Display functions to stage the data.
    DISPA.clearBuffer();
    DISPA.setCursor(0, 10);
    DISPA.print("Temp: ");
    DISPA.setCursor(40, 10);
    DISPA.print(temperature);
    DISPA.setCursor(0, 40);
    DISPA.print("Hum: ");
    DISPA.setCursor(40, 40);
    DISPA.print(humidity);
    DISPA.sendBuffer();
      
      // save the last HTTP GET Request
      previousMillis = currentMillis;
    }
    else {
      Serial.println("WiFi Disconnected");
    }
  }
}
// Get Request function 
String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;
    
  // Ip address with the page name 
  http.begin(client, serverName);
  
  // Post request and wait to hear back from the server
  int httpResponseCode = http.GET();
  
  String payload = "--"; 
  
  if (httpResponseCode>0) { // Checking the response value if there is value the request will return the value of the server
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {// No value means error in the server
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

Client works

full system

The communication I have chose is Wireless communication through making a server and a client reading data from the server.

This type of communication is so helpful when wires are not suitable to be between both the transmitter and receiver so this type of communication will solve this problem; It working principle is so easy as the transmitter initiate the communication through sending a current to the antenna to be transformed into a magnetic and electric field in the space around the antenna, then the receiver will catch these fields from the space around and convert them into current to be sent to the MCU and the MCU will do its processing and convert it to whatever the user want.