Embedded Networking and Communications
April 30, 2024
Challenge
-
Group assignment:
- Send a message between two projects.🤔
- Document your work to the group work page and reflect on your individual page what you learned.
-
Individual assignment:
- Design, build and connect wired or wireless node(s) with network or bus addresses and a local interface.
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.
-
I will do this through making the XIAO ESP32C3 as web server so I can access it through IP Address.
-
Then I will set the NODEMCU as a client requesting the data from the server through IP Address.
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.
-
I will start By downloading the required Libraries.
-
{ESPAsyncWebServer} And {WIFI} Libraries in addition to (DHT Library).
- Then I will Write the code and Upload it.
#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(){
}
- Now After uploading the code we should receive the IP address of the server through the serial monitor.
- Then to test this we need to connect to XIAO network and access this IP.
- Then I will go to the browser and check the IP.
- Then moving on to the temperature page.
- Then moving to humidity page.
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.
- For the connection of the OLED and the NODEMCU.
-
Then I will code it.
-
Before we need to Download the libraries needed.
- This is the needed library but we will have to download the entire library.
- Then we will have to include it by the Arduino IDE
- Then I will choose the downloaded ZIP file and Arduino IDE will take it from here and finalize everything for me.
- The code below will read the data from the server through get request and then post the data to the OLED display through I2C protocol.
#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;
}
- And here we go, after uploading the code it worked as perfect as I want.
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.