Assignments

Group Assignment

Networking and communication.

Communication Types

Wired Communication

Wired communication refers to data transmission over physical wires. It's also known as wireline communication.

SPI (Serial Peripheral Interface)

SPI is a synchronous, full duplex interface between a main device and peripheral subnodes like sensors or memory chips. Image source

I2C Communication

I2C allows multiple masters and slaves on the same bus. It uses only two lines:Image source

Wireless Communication

Wireless communication transmits data without using physical wires or cables.

WiFi

Controllers like ESP have built-in WiFi modules. Others like Arduino and ATtiny need external modules. Communication is done using WiFi SSID and password.Image source

Bluetooth

Bluetooth allows communication between devices in master-slave mode:Image source

Communicating Between Boards

We can use many methods to let boards communicate with each other. For example, ESP boards can communicate using WiFi or Bluetooth for multi-board projects. So we decided to communicate the boards made by Tsheltrim and Tsheyang, as they were the only ones equipped with the ESP32-C3 Xiao microcontroller and had custom boards ready. After that, we connected the antenna that comes with the microcontroller by inserting it into the designated antenna slot on the board. Image source

Now we can start programming our boards and then start with this weeks assignments

//Tsheltrims boards code

#include <WiFi.h>

#include <WiFiClient.h>

const char* ssid = "ESP_AP";
const char* password = "123456789";
const char* host = "192.168.4.1"; // IP address of the server

WiFiClient client;

void setup() {
  Serial.begin(115200);

  Serial.println();
  Serial.println("Connecting to WiFi");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (!client.connected()) {
    Serial.println("Connecting to server...");
    if (client.connect(host, 80)) {
      Serial.println("Connected to server");
    } else {
      Serial.println("Connection failed");
      delay(5000);
      return;
    }
  }

  if (client.available()) {
    String message = client.readStringUntil('\n');
    Serial.println("Message received from server: " + message);
  }
}
  1. Connects your device to a WiFi network called "ESP_AP" with password "123456789".
  2. Acts as a client trying to connect to a server at IP address 192.168.4.1 on port 80 (the common HTTP port).
  3. Once connected to the server, it listens for messages sent from the server.
  4. When a message is received from the server, it prints the message to the Serial Monitor.
//Tsheyangs boards code
#include <WiFi.h>


const char* ssid = "ESP_AP";
const char* password = "123456789";

WiFiServer server(80);
WiFiClient client;

const int buttonPin = 3;  // Button pin

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  WiFi.softAP(ssid, password);
  server.begin();

  Serial.print("Server IP address: ");
  Serial.println(WiFi.softAPIP());
}

void loop() {
  if (!client.connected()) {
    client = server.available();
    if (client) {
      Serial.println("Client connected");
    }
  }

  if (client.connected()) {
    if (client.available()) {
      String message = client.readStringUntil('\n');
      Serial.println("Message received: " + message);
      // LED control removed
    }
  }

  if (digitalRead(buttonPin) == HIGH) {
    client.println("ButtonPressed");
    delay(1000);  // Debounce delay
  }

  printWiFiInfo(); // Print host WiFi info
}

void printWiFiInfo() {
  Serial.print("Host Name: ");
  Serial.println(WiFi.softAPgetHostname());
  Serial.print("Host IP: ");
  Serial.println(WiFi.softAPIP());
  Serial.print("Host IPV6: ");
  Serial.println(WiFi.softAPIPv6());
  Serial.print("Host SSID: ");
  Serial.println(WiFi.SSID());
  Serial.print("Host Broadcast IP: ");
  Serial.println(WiFi.softAPBroadcastIP());
  Serial.print("Host MAC Address: ");
  Serial.println(WiFi.softAPmacAddress());
  Serial.print("Number of Host Connections: ");
  Serial.println(WiFi.softAPgetStationNum());
  Serial.print("Host Network ID: ");
  Serial.println(WiFi.softAPNetworkID());
  Serial.print("Host Status: ");
  Serial.println(WiFi.status());
  Serial.print("Server IP address: ");
  Serial.println(WiFi.softAPIP());
  delay(1000);
}
  1. Defines the device as a WiFi Access Point (AP) with the SSID "ESP_AP" and password "123456789".
  2. Sets up a WiFi server listening on port 80.
  3. Configures pin 3 as an input to read a button press.
  4. Starts the serial communication for debugging and prints the server's IP address.
  5. In the loop, waits for a client to connect to the server.
  6. If a client is connected and sends data, it reads the message and prints it to the Serial Monitor.
  7. When the button connected to pin 3 is pressed, it sends the message "ButtonPressed" to the connected client, with a debounce delay.
  8. Calls a function to print detailed WiFi Access Point information to the Serial Monitor every second.
  9. The printWiFiInfo() function prints information such as host name, IP addresses (IPv4 and IPv6), SSID, MAC address, number of connections, network ID, and WiFi status for debugging purposes.