Networking and Communications
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 input and
/or output devices
Designing and building wired or wireless nodes with network or bus addresses and local input/output (I/O) involves multiple layers—hardware,
firmware, and communication protocols. Here's a structured approach tailored to your expertise in special-purpose machines, automation, and
embedded systems.
What is Networking and Communication?
Networking
Networking is the process of connecting multiple devices (or nodes) together so they can share data and resources. In embedded systems, this means
connecting microcontrollers, sensors, actuators, or machines to talk to each other.
Example: Connecting 10 sensors and motors in a factory floor using RS485 or Wi-Fi.
Communication
Communication is the method or process by which data is transferred between two or more devices.
It involves:
Sender (who sends data)
Receiver (who receives it)
Medium (wired/wireless channel)
Protocol (rules for data exchange)
Example: Sending a temperature reading from a sensor node to a control panel using Modbus.
Why is it Important in Embedded Systems?
In embedded systems:
Devices are usually "smart" but need to work together
Inputs from one device can trigger actions in another
Communication is needed for monitoring, control, data logging, and automation
Type |
Description |
Examples |
Wired |
Physical cables for communication |
UART,RS485, CAN, Ethernet |
Wireless |
Radio signals for communication |
Wi-Fi, Bluetooth, Zigbee, LoRa |
Types of Communication
Type |
Description |
Example |
Serial |
Bit-by-bit transfer over a single line |
UART, SPI, I2C |
Parallel |
Multiple bits at once over many lines |
Used in older systems |
Synchronous |
Data sent with clock signal |
SPI, I2C |
Asynchronous |
No clock; uses start/stop bits |
UART, RS232 |
Digital Protocols |
Use packets, error checks, IDs, etc. |
Modbus, CAN, MQTT, HTTP |
Example Use Cases
Industrial Automation (Wired)
RS485/Modbus to control machines
CAN Bus to connect motor controllers in robotics
🏠Home Automation (Wireless)
Wi-Fi-based nodes to control lights/fans via phone
MQTT protocol to send sensor data to cloud dashboard
Below is a complete example of how you can use a XIAO ESP32-S3 as a Wi-Fi-based node to control lights or fans via a smartphone. This design uses the
XIAO ESP32-S3’s built-in Wi-Fi to run a lightweight web server that listens for HTTP requests. When the board receives commands (like turning a light
or fan ON/OFF), it toggles a relay connected to the appropriate GPIO pin.
Hardware Overview
Microcontroller: XIAO ESP32-S3
Actuator Control: A relay module (or transistor driver circuit) to interface with high-voltage devices (lights/fans).
Power Supply: Appropriate voltage (ensure the relay driver circuit is compatible with the board’s output).
Smartphone: Uses a web browser (or a custom app) to send commands via HTTP.
Note: Always add proper electrical isolation (e.g., opto-isolators) and safety components when switching mains load
Circuit Setup
Relay Connection:
Input Signal: Connect the relay module’s control input to a designated GPIO pin on the XIAO ESP32-S3 (for example, GPIO 5).
Relay Power: Connect the relay’s VCC to the appropriate power supply (e.g., 5V if needed) and GND to common ground.
ESP32-S3 Connections:
Power the board from USB or a regulated supply.
Make sure your circuit has a common ground.
Relay Connection:
Input Signal: Connect the relay module’s control input to a designated GPIO pin on the XIAO ESP32-S3 (for example, GPIO 5).
Relay Power: Connect the relay’s VCC to the appropriate power supply (e.g., 5V if needed) and GND to common ground.
ESP32-S3 Connections:
Power the board from USB or a regulated supply.
Make sure your circuit has a common ground.
Firmware Arduino Code Example
You can program the XIAO ESP32-S3 using the Arduino IDE. (Make sure you have the board package installed for XIAO ESP32-S3.)
Below is a sample code that creates an HTTP server. The node listens for commands on the /control endpoint. Changing the URL parameters
(device and state) will toggle the relay.
#include
#include
// Replace with your own Wi-Fi credentials:
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";
// Define GPIO pin for Relay (adjust based on your wiring)
const int relayPin = 5;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
// Initialize the relayPin as an output:
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Assume LOW is OFF (or adjust if HIGH is off)
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Define route for control commands
server.on("/control", HTTP_GET, [](AsyncWebServerRequest *request){
// Retrieve query parameters if present
String device = request->hasParam("device") ? request->getParam("device")->value() : "";
String state = request->hasParam("state") ? request->getParam("state")->value() : "";
// Check for the appropriate command for the "light" or "fan"
if (device == "light" || device == "fan") {
if (state == "on") {
digitalWrite(relayPin, HIGH); // Turn device ON
request->send(200, "text/plain", String(device) + " is ON");
}
else if (state == "off") {
digitalWrite(relayPin, LOW); // Turn device OFF
request->send(200, "text/plain", String(device) + " is OFF");
}
else {
request->send(400, "text/plain", "Invalid state: must be 'on' or 'off'");
}
}
else {
request->send(400, "text/plain", "Invalid device: must be 'light' or 'fan'");
}
});
// Start the server
server.begin();
}
void loop() {
// No need for code in loop since AsyncWebServer handles the requests asynchronously.
}
Controlling via Smartphone
Connect to the Same Network:
Ensure your smartphone is connected to the same Wi-Fi network as your XIAO ESP32-S3.
Access via Browser:
Open a browser on your phone and enter the IP address printed in the Serial Monitor (e.g., http://192.168.1.100/control?device=light&state=on).
Replace light with fan if desired, and switch state between on and off.
Custom App:
You can also develop a simple mobile app (using platforms like MIT App Inventor, Flutter, or React Native) to send HTTP requests with better UI controls.
Additional Considerations
Security:
For production, consider adding authentication, HTTPS, or using secure tokens to prevent unauthorized access.
Robustness:
Implement error handling and possibly a watchdog timer to reset the device in case of network issues or software hangs.
Expandability:
The same setup can be extended to multiple GPIOs for controlling different devices. Use unique endpoints or MQTT topics if using an MQTT broker for larger projects.
OTA Updates:
If you plan to deploy many nodes, you might consider adding Over-The-Air (OTA) update functionality.