Week 13: NETWORKING AND COMMUNICATIONS



Topic Summary (generated by ChatGPT)

This week, I delved into a comprehensive study of networking and communication technologies, covering both wired and wireless systems. I began by understanding the fundamental purposes of networking, such as enhancing location tracking, supporting parallel processing, enabling modularity, and managing interference. These concepts are crucial for building efficient and resilient communication infrastructures in various applications.

In wired communication, I focused on serial communication protocols like UART, USART, SERCOM, and PIO. I examined the differences between asynchronous serial communication protocols, including RS-232, RS-422, and RS-485, and synchronous serial protocols like SPI and I2C. These protocols are essential for effective data transmission in electronic systems. Additionally, I explored USB interfaces, which are widely used for their high-speed data transfer capabilities and versatility in numerous applications.

On the wireless side, I explored a variety of technologies and modules such as the ESP32 and ESP8266 for Wi-Fi and Bluetooth, and radio modules like the nRF24L01+ for ISM band communication. I studied the principles of radio frequency design, including antenna theory and various modulation techniques. I also delved into communication protocols and their layers, such as the TCP/IP stack used for internet communications, and used tools like Wireshark for network analysis. This week's study has provided me with a solid understanding of both wired and wireless communication systems, which is essential for designing and troubleshooting modern electronic devices.

Assignments

This week's assignment is about understanding Embedded Networking and communication

Group assignment

  • send a message between two projects

For details on this assignment head to our Group assignment page

Individual assignment

  • design, build, and connect wired or wireless node(s) with network or bus addresses and local input &/or output device(s)

For this assignment I want to explore wired, wireless and server connection to my final project board. Details on how I made the board are found here.

Wired Communication (I2C)

Here I connected an I2C based Real Time Clock (RTC) module to my board.

Pin connection from the module to my board:

  • VCC to 3.3V
  • GND to GND
  • SCL to SCL
  • SDA to SDA
  • I used RTClib to communicate and access time from DS1307 chip on the module

    Here is a video demo:

    Wireless communication

    Bluetooth Low energy (BLE)

    To experiment with BLE connection I followed the documentation on Seed Xiao website for ESP32s3 bluetooth connection.

    I want to send the data collected from the RTC module to my phone through BLE.

    To archive this I implemented the bluetooth code as follows:

    I have installed nRF Connect for mobile to receive the DateTime date from my board.


    After scanning and connecting to the board, I read the DateTime values on the app.

    WiFi

    Here I connect my board to a WiFi network and create a web server. The goal is to use this feature in my final project. I want to serve a page that can display date and time from the RTC module. I used the Arduino HelloServer example as the basis for achieving this.

    Code:

        // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
        #include "RTClib.h"
        #include <WiFi.h>
        #include <WiFiClient.h>
        #include <WebServer.h>
        #include <ESPmDNS.h>
    
    
        const char* ssid = "Ati";
        const char* password = "ati@1987";
    
    
        WebServer server(80);
    
    
        RTC_DS1307 rtc;
    
    
        char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    
    
        void handleRoot() {
            DateTime  now = rtc.now();
            String datetime = "The time is:  "+String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " ( "+ daysOfTheWeek[now.dayOfTheWeek()] +" ) " +
                    String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC);
        
        server.send(200, "text/plain", datetime.c_str());  
        }
        void handleNotFound() {
        String message = "File Not Found\n\n";
        message += "URI: ";
        message += server.uri();
        message += "\nMethod: ";
        message += (server.method() == HTTP_GET) ? "GET" : "POST";
        message += "\nArguments: ";
        message += server.args();
        message += "\n";
        for (uint8_t i = 0; i < server.args(); i++) {
            message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
        }
        server.send(404, "text/plain", message);
        
        }
        void setup () {
        Serial.begin(57600);
        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid, password);
        if (! rtc.begin()) {
            Serial.println("Couldn't find RTC");
            Serial.flush();
            while (1) delay(10);
        }
    
    
        if (! rtc.isrunning()) {
            Serial.println("RTC is NOT running, let's set the time!");
            // When time needs to be set on a new device, or after a power loss, the
            // following line sets the RTC to the date & time this sketch was compiled
            rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
        }
        // Wait for connection
        while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
        }
        Serial.println("");
        Serial.print("Connected to ");
        Serial.println(ssid);
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());
    
    
        if (MDNS.begin("esp32")) {
            Serial.println("MDNS responder started");
        }
    
    
        server.on("/", handleRoot);
        server.onNotFound(handleNotFound);
    
    
        server.begin();
        Serial.println("HTTP server started");
        }
    
    
        void loop () {
            server.handleClient();
            delay(2);
        }
    
                                            

    After running the code the board connected to my WiFi and is assigned with an IP address:

    I used the IP address to access the page on a web browser.

    Result:

    Networking (using MQTT)

    There is an mqtt server hosted on the fabcloud. I would be using the server to experiment sending and receiving the DateTime between my board and my PC.

    I used MQTT explorer to view the topics and the values sent to the topics:

    The topics available at the moment:

    Code:

        // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
        #include "RTClib.h"
        #include <WiFi.h>
        #include <WiFiClient.h>
        #include <WebServer.h>
        #include <ESPmDNS.h>
        #include "EspMQTTClient.h"
        
        const char* ssid = "Ati";
        const char* password = "ati@1987";
        
        WebServer server(80);
        
        RTC_DS1307 rtc;
        
        char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        
        
        EspMQTTClient client(
            "Ati",
            "ati@1987",
            "3.128.220.209",  // MQTT Broker server ip
            "fabacademy",   // Can be omitted if not needed
            "fabacademy",   // Can be omitted if not needed
            "fabacademy",     // Client name that uniquely identify your device
            1883              // The MQTT port, default to 1883. this line can be omitted
        );
        
        DateTime now; 
        String datetime;   
        void onConnectionEstablished()
        {
            now = rtc.now(); 
            datetime = "The time is:  "+String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " ( "+ daysOfTheWeek[now.dayOfTheWeek()] +" ) " +
                        String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC);
        
            client.subscribe("fabacademy/input", [](const String & payload) {
            
                now = rtc.now(); 
                datetime = "The time is:  "+String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " ( "+ daysOfTheWeek[now.dayOfTheWeek()] +" ) " +
                        String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC);
        
                client.publish("fabacademy/datetime", datetime.c_str()); // You can activate the retain flag by setting the third parameter to true
        
            });
            client.publish("fabacademy/datetime", datetime.c_str()); // You can activate the retain flag by setting the third parameter to true
        
        
        }
        void setup () {
            Serial.begin(115200);
            if (! rtc.begin()) {
            Serial.println("Couldn't find RTC");
            Serial.flush();
            while (1) delay(10);
            }
        
            if (! rtc.isrunning()) {
            Serial.println("RTC is NOT running, let's set the time!");
            // When time needs to be set on a new device, or after a power loss, the
            // following line sets the RTC to the date & time this sketch was compiled
            rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
            }
            client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
            client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password").
            client.enableOTA(); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port).
            client.enableLastWillMessage("fabacademy/lastwill", "I am going offline");  // You can activate the retain flag by setting the third parameter to true
        
        }
        
        void loop () {
        
            client.loop();
        }
                                                
                                            

    The code creates a mqtt client. Then subscribe to “fabacademy/input” topic and publish the date and time to “fabacademy/datetime” topic.

    Sources