Week11: Networking and Communications¶
Group Assignment
- send a message between two projects
This group assignment document was written by Naoki Hayashi.
1. Networking using Wi-Fi and internet (FMCU)¶
Firstly, we tried the FMCU, a button device that shows when a Fab Lab is active by sending a signal online. In addition to lab status, we added e-mail notifications to FabLab Kannai members when the button is pushed.
Communications and networking¶
- Push button signal is sent from ESP32 to local router via Wi-Fi (communications)
- The signal is sent to MQTT server and updates FMCU status via internet (networking)
- The signal is also sent to SMTP server and send an e-mail via internet (networking)
MQTT: A lightweight messaging protocol for sending data between devices over the internet using a publish/subscribe model.
SMTP: A protocol used to send emails from one server to another over the internet.
Hardware setup¶
- XIAO ESP32C3 for Wi-Fi connection
- LED: connected to D7 pin
- Resistor (1kΩ) for the LED
- Push button: connected to D6 pin
Programming¶
Based on the code fla-fmcu.ino, we added code to send e-mail.
- Open Arduino IDE and write the code below
- Add configuration file to set lab ID, Wi-Fi SSID and password, by adding config.h file
- Push “…” at the right of the sketch window
- A file is created at the same folder as the Arduino sketch
- Install libraries (Sketch > Include Library > Library manager)
- Adafruit_MQTT
- Adafruit_MQTT_Client
- ESP_Mail_Client
config.h
1 2 3 4 5 |
|
Arduino code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
FMCU site (at the time, only Fab Lab Kannai and Skylab Workshop were active)
Button pushed and LED turned on, FMCU site updated, Fab Lab Kannai became active (behind SkyLab) and e-mail sent
E-mail “Hello!”
2. I2C communication (Grove Vision AI)¶
Secondly, we tried to send the object detection results from the Grove Vision AI Module to the ESP32 via I2C. We then explored serial communication and OLED display, based on Examples for Grove Vision AI V2 and XIAO ESP32.
In this example, we use a camera attached on a servo motor to detect a face, measure its position within the field of view, and rotate a servo motor to follow it.
Hardware setup¶
- Grove Vision AI Module V2
- XIAO ESP32C3
- Seeed Studio Expansion Board Base for XIAO with Grove OLED
- Servo motor (360° continuous rotation)
Use 360° continuous rotation servo
Initially, we used standard servo motor which have a limited range of motion (typically around 180 degrees), and it didn’t work as we expected (continuously switched direction).
Communications in this setup¶
- Grove Vision AI Module send recognition results to XIAO ESP32 via I2C
- XIAO ESP32 sends text to OLED via I2C
- XIAO ESP32 sends text to PC (serial print) via USB
In this setup, both the OLED display and the Grove Vision AI V2 module connect to the XIAO ESP32 using the I2C protocol, which uses only two wires: SDA (data) and SCL (clock). Each device has a unique address, so the ESP32 can talk to them one at a time over the same wires.
In this case, for OLED display, the I2C communication is handled internally by the U8x8lib library once u8x8.begin()
is called.
Steps¶
- Load preset AI model to Grove Vision AI Module
- Visit Seeed Studio SenseCraft
- Connect Grove Vision AI Module to PC using USB
- Select AI models or upload custom model (in this case, we used “Face Detection”)
- Disconnect from PC
- Wiring
- Connect XIAO to the expansion board
- Wire Grove Vision module to expansion board via I2C connector
- Wire the servo motor to the expansion board Connect the servo motor’s control wire to the D6 pin on the XIAO ESP32. Power the servo using the 5V output from the XIAO ESP32.
-
Program XIAO ESP32
- Select board (If it is not downloaded, Tools > Board > Boards Manager > Search “ESP32”)
- Install libraries: U8x8 for the OLED display (Sketch > Include Library > Library manager > Search “U8x8” and install it)
- After uploading program, Arduino IDE says “Hard resetting with RTC WDT…” Then push the RESET button on XIAO ESP32.
Arduino code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <Arduino.h> #include <U8x8lib.h> // Include the U8x8 library for OLED display // Initialize the OLED display using I2C communication (128x64 resolution) U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); #include <Seeed_Arduino_SSCMA.h> // Include the library for the vision inference module SSCMA Infer; // Create an instance of the inference module // Function to stop the servo by sending a neutral pulse void stop_rotate(void) { digitalWrite(D6, HIGH); delayMicroseconds(1500); // Neutral pulse for stopping digitalWrite(D6, LOW); } // Function to rotate the servo // rtime: how long to rotate, revers: direction of rotation void servo_rotate(int rtime, bool revers = false) { uint32_t sleep; if (revers) { sleep = 2500; // Pulse for reverse direction } else { sleep = 500; // Pulse for forward direction } digitalWrite(D6, HIGH); delayMicroseconds(sleep); // Send pulse digitalWrite(D6, LOW); delay(30 * rtime); // Wait for motion stop_rotate(); // Stop the servo after motion } void setup() { Infer.begin(); // Start the inference module Serial.begin(9600); // Start serial communication pinMode(D6, OUTPUT); // Set pin D6 as output (connected to servo) u8x8.begin(); // Initialize the OLED display u8x8.setFlipMode(1); // Flip the display (180 degrees) } void loop() { // Check if inference is ready if (!Infer.invoke()) { // If at least one object is detected if (Infer.boxes().size() > 0) { // If object is on the left side, rotate servo in reverse if (Infer.boxes()[0].x < 160) { servo_rotate(1, true); delay(50); } // If object is on the right side, rotate servo forward else if (Infer.boxes()[0].x > 240) { servo_rotate(1, false); delay(50); } // Print detected coordinates to the serial monitor Serial.print("X==>>"); Serial.print(Infer.boxes()[0].x); Serial.print("Y==>>"); Serial.println(Infer.boxes()[0].y); // Display coordinates on OLED screen u8x8.setFont(u8x8_font_chroma48medium8_r); // Set font u8x8.setCursor(0, 0); // Set cursor to top-left u8x8.print("X="); // Print X coordinate u8x8.println(Infer.boxes()[0].x); u8x8.print("Y="); // Print Y coordinate u8x8.println(Infer.boxes()[0].y); } } }
Detects and tracks Shintaro-san’s face
OLED displaying X and Y position of detected face