Week 11
Networking and Communications
Check out our Group Assignment to see how we connected two and more devices and what we learned about it GROUP PAGE →
// MAIN OBJECTIVE \\
Design, build and connect wired or wireless node(s) with network or bus addresses and a local input and/or output device(s).
My networking proyect
For this week, my goal is to connect to my microcontroller board. The purpose of this project is to learn how to test the wireless conneciton of my XIAO ESP32-C6 via Wi-Fi. This type of communications are crutial for making big proyects because you need to connect properly to send and recieve important data.
My Setup
Hardware
This week is so simple in terms of hardware, because all I needed was my XIAO ESP32-C6 and check the connections with the built-in LED, so thats all.
Note: the XIAO only works for 2.4g network connection, other types of networks like 5g won't work properly.
XIAO ESP32-C6 PINOUT.
My networking setup.
Programming with ESP-IDF and C
The logic is simple, I first connect my XIAO to Wi-Fi, and then access to a private fabacademy server via MQTT (mosquitto) broker, in which I can make my board to publish for sending data or subscribe for receiving data. For this proyect I will subscribe the XIAO to a topic called CMD, and then in my PC or phone terminal I will write 1 or 0 and then the XIAO receive this data and turns on and off a LED based on the command.
Logic and Libraries
Because I'm using ESP-IDF and C, this program needs some librares like wifi_sta.h or mqtt_client_app.h for connecting properly to the mqtt broker, and I divided all that is needed on separate files that the main program calls for using (you can do everything in the same program but it looks prettier).
Main Code
// main.c (ESP32-C6 / ESP-IDF)
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "wifi_sta.h"
#include "mqtt_client_app.h"
static const char *TAG = "APP_MAIN";
#ifndef WIFI_SSID
#define WIFI_SSID "HERE GOES YOUR WIFI 2.4G NAME"
#endif
#ifndef WIFI_PASS
#define WIFI_PASS "YOUR PASSWORD"
#endif
#ifndef CONFIG_MQTT_BROKER_URI
#define CONFIG_MQTT_BROKER_URI "mqtt://mqtt.fabcloud.org:1883" // fab server
#endif
#define MQTT_USER "user"
#define MQTT_PASS "pass (you need permision to use the private fab server, otherwise you use mqtt.test.org:1833)"
void app_main(void)
{
ESP_LOGI(TAG, "Initialating system for ESP32-C6...");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
} else {
ESP_ERROR_CHECK(ret);
}
const char *ssid =
#ifdef CONFIG_WIFI_SSID
CONFIG_WIFI_SSID;
#else
WIFI_SSID;
#endif
const char *pass =
#ifdef CONFIG_WIFI_PASSWORD
CONFIG_WIFI_PASSWORD;
#else
WIFI_PASS;
#endif
ESP_LOGI(TAG, "Connecting to Wi-Fi: %s", ssid);
esp_err_t wifi_err = wifi_sta_connect(ssid, pass, 30000); // 30s timeout
if (wifi_err != ESP_OK) {
ESP_LOGE(TAG, "Error connecting Wi-Fi: %s (0x%x)", esp_err_to_name(wifi_err), wifi_err);
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
ESP_LOGI(TAG, "Wi-Fi connected sucsessfully");
ESP_LOGI(TAG, "Initialazing MQTT client: %s", CONFIG_MQTT_BROKER_URI);
ESP_ERROR_CHECK(mqtt_app_start(CONFIG_MQTT_BROKER_URI, MQTT_USER, MQTT_PASS));
while (1) {
ESP_LOGI(TAG, "System working properly...");
vTaskDelay(pdMS_TO_TICKS(10000)); // hearbeat
}
}
Note: all the other codes like .c or .h will be included in the download files above.
Final Result
Here is a video demonstrating the operation of the XIAO, connecting to Wi-Fi and turning a LED on and of with the MQTT broker.
Final demonstration of the connection between XIAO and PC or Phone.
Files
Here you can download the original files generated for this week's project: