Week 13

Networking and Communication

Brief Overview

In Networking and Communications week, I was bit confused about what should I do as a assignment. But I decided to use Thingspeak as I was familiar with it which I have used sometimes during my Bachelors. So In this assignment I have describe in brief about the Networking and Communication. I have also documented about how to connect the board and send the sensor data on cloud.

Heroshots






Group Assignment:-



In this assignment:-


Send a message between two projects

Visit our Group assignment page here


Individual Assignment:-


In this assignment:-

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

Networking and Communication Basics

Networks: Building Connections

Imagine a network as a digital highway that allows devices to share resources and information. These devices can be computers, printers, smartphones, or any gadget with communication capabilities. They connect through physical links like cables (Ethernet, fiber optic) or wireless signals (Wi-Fi, Bluetooth).


Network Types

Networks come in various sizes and purposes. A Local Area Network (LAN) connects devices within a limited area, like a home or office. Conversely, a Wide Area Network (WAN) spans a broader geographical region, connecting businesses or cities. Metropolitan Area Networks (MANs) cover a larger area within a city, often connecting LANs.


Communication Protocols: The Language of Networks

Think of protocols as the traffic rules on the network highway. These pre-defined rules govern how devices format, transmit, and receive data. Protocols ensure devices from different manufacturers can understand each other.


A Common Protocol: TCP/IP

A common protocol is TCP/IP (Transmission Control Protocol/Internet Protocol). TCP/IP breaks down data into packets, sends them across the network, and reassembles them at the destination.


Other Communication Protocols

Other protocols handle specific tasks: HTTP for web browsing, FTP for file transfer, and SMTP for emails.


Serial vs. Parallel Communication: Different Lanes on the Road

Data travels on the network in bits (0s and 1s). There are two main methods for transmitting this data:


Serial Communication

Sends data one bit at a time, like a single lane on a road. It's simpler and more efficient for long distances, commonly used in phone lines and internet connections.


Parallel Communication

Transmits multiple bits simultaneously, like multiple lanes on a highway. It's faster for short distances but requires more complex cables and is less common.


Transmission Modes: Sharing the Road

Networks can employ different modes for data flow:


Simplex Mode

One-way communication, like a radio broadcast. Information flows in one direction only (think TV stations sending signals).


Half-Duplex Mode

Devices can take turns transmitting and receiving, like a two-way radio. Only one device can transmit at a time (think using a walkie-talkie).


Full-Duplex Mode

Allows simultaneous transmission and receiving in both directions, like a phone conversation. Both parties can communicate at the same time.


Synchronous vs. Asynchronous Communication: Keeping Time on the Network

Serial communication can be further classified based on timing:


Synchronous Communication

Devices share a clock signal to synchronize data transmission. It's like a marching band where everyone keeps pace with a drumbeat. This method is efficient for large data transfers.


Asynchronous Communication

Devices don't rely on a shared clock. Instead, start and stop bits are added to data packets to indicate the beginning and end of transmission. This is more flexible but can be less efficient for large data transfers, commonly used in UART communication (explained later).


Common Communication Protocols: Specialized Languages

Networks use various protocols for specific purposes, like:

I2C Protocol

A simple protocol for short-distance communication between devices on a circuit board, often used to connect microcontrollers with sensors or displays. It requires only two wires for data and clock synchronization.

SPI Protocol

Another short-distance protocol ideal for high-speed data exchange between microcontrollers and peripheral devices like SD cards. It uses separate wires for data, clock, and device selection, offering faster data rates than I2C.

ThingSpeak


Source:https://thingspeak.com


About ThingSpeak :-

ThingSpeak is like a digital scrapbook for your Internet of Things (IoT) projects. Imagine a website where you can send data, like temperature readings from a sensor, and see it plotted on a graph. Created by MathWorks, a company that makes software for engineers and scientists, ThingSpeak is free for basic use and lets you easily track and understand what's happening with your project over time.


ThingSpeak is an online platform where you can send data from your project (like temperature readings) and visualize it over time. It allows you to analyze and understand how your project is performing remotely.:


Open Web Browser, to search ThingSpeak :



Search ThingSpeak :


ThingSpeak Web page :



Sign in here :


Click on New Channel :



Enter name of channel and select how many fields we want :



Save the channel :



Go on API keys :



Copy API keys :



Paste API keys in your code :



Conection diagram :



Temperature Data displaying on ThingSpeak:





How this communication works


ThingSpeak with ESP32 and DHT11

How Data is Sent from ESP32 to ThingSpeak


WiFi Connection

The ESP32 connects to a Wi-Fi network using the provided SSID and password. This is necessary for the ESP32 to communicate with the ThingSpeak server over the internet.


HTTP Protocol

The data is sent to ThingSpeak using the HTTP protocol. HTTP (HyperText Transfer Protocol) is a protocol used for transmitting data over the internet.


ThingSpeak API

ThingSpeak provides a RESTful API that allows you to send data to its servers. The data is sent using an HTTP GET request to a specific URL that includes the channel number and the write API key for authentication.


Detailed Explanation of the Communication Process


WiFi Initialization

  • The ESP32 initializes the Wi-Fi module and attempts to connect to the specified Wi-Fi network.
  • WiFi.begin(ssid, password); starts the connection process.
  • The code waits in a loop until the connection is established (WiFi.status() != WL_CONNECTED).

Reading Sensor Data


  • The DHT11 sensor is read using dht.readTemperature() and dht.readHumidity().
  • The sensor data is stored in variables temperature and humidity.

Preparing Data for Transmission


  • ThingSpeak expects data in specific fields. In this example, temperature is sent to field 1 and humidity to field 2.
  • ThingSpeak.setField(1, temperature); sets the value of field 1.
  • ThingSpeak.setField(2, humidity); sets the value of field 2.

Sending Data via HTTP


  • ThingSpeak.writeFields(myChannelNumber, writeAPIKey); sends the data to ThingSpeak.
  • This function constructs an HTTP GET request to the URL:
  • http://api.thingspeak.com/update?api_key=YOUR_WRITE_API_KEY&field1=temperature&field2=humidity
  • The request includes the channel number, write API key, and the data fields.


HTTP GET Request


  • An HTTP GET request is made to the ThingSpeak server. The URL includes:
  • The API endpoint (http://api.thingspeak.com/update)
  • The API key for authentication (api_key=YOUR_WRITE_API_KEY)
  • The data fields (field1=temperature&field2=humidity)
  • The server processes the request, verifies the API key, and stores the data in the specified channel.

Response Handling


  • ThingSpeak returns a response code (200 for success).
  • The code checks the response to ensure data was sent successfully:

int response = ThingSpeak.writeFields(myChannelNumber, writeAPIKey);
if (response == 200) {
    Serial.println("Data sent to ThingSpeak successfully");
} else {
    Serial.println("Failed to send data to ThingSpeak");
}

    

Visualization on ThingSpeak


Accessing the Channel

Once the data is sent to ThingSpeak, it is stored in the channel fields. You can access your channel on the ThingSpeak website to view the data.


Plotting Data

ThingSpeak automatically generates graphs for each field in the channel. You can customize the visualizations, add widgets, and analyze the data using built-in tools.


Communication Workflow

  • ESP32 Connects to WiFi: Ensures the device is online and can communicate with external servers.
  • DHT11 Reads Data: Gathers environmental data (temperature and humidity).
  • Data Formatting: Prepares data in the format expected by ThingSpeak.
  • HTTP GET Request: Sends the data to ThingSpeak via the internet.
  • ThingSpeak Receives Data: Stores the data in the channel and updates visualizations.
  • Response Handling: Confirms successful data transmission or handles errors.

This workflow illustrates how the ESP32 communicates with ThingSpeak, ensuring that the data is reliably transmitted and visualized.



Common Issues and Solutions


  • Problem 1: WiFi Connection Issues

    Solutions:

    • Check Credentials: Ensure that the SSID and password are correct.
    • Signal Strength: Make sure the ESP32 is within range of the Wi-Fi router.
    • WiFi Channel: Check if the Wi-Fi network is on a crowded channel and try switching to a less crowded one.
    • Firmware Update: Update the ESP32 firmware to the latest version to improve connectivity.

  • Problem 2: Sensor Reading Errors

    Solutions:

    • Wiring: Verify that the sensor is properly connected to the ESP32.
    • Power Supply: Ensure that the sensor is receiving a stable 3.3V power supply.
    • Sensor Initialization: Double-check the initialization code to ensure it matches the sensor type.
    • Timing: Ensure that there is sufficient delay between sensor readings (usually 2 seconds).

  • Problem 3: Data Transmission Failures

    Solutions:

    • API Key: Verify that the Write API Key is correct.
    • Internet Connectivity: Ensure that the ESP32 has an active internet connection.
    • ThingSpeak Limits: Ensure you are not exceeding ThingSpeak’s rate limits (maximum one update every 15 seconds for free accounts).
    • Error Handling: Implement error handling to retry sending data if the initial attempt fails.


Reference Files:-

  1. Source Code