1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Role of the Arduino
It acts as a Server: a device that listens for requests on a network and sends back a response.
Role of the mobile app
It acts as a Client: a device that sends a request to a server and waits for its response.
Communication model
A 'request-reply' model. The app (client) sends a 'request' (a 'knock'), and the Arduino (server) sends a 'reply' (the gas data).
Port 8888
The specific 'door' or 'channel' the server is listening on. It's like a doorbell for a specific service.
Serial.begin(9600)
The hardware serial port for debugging messages, connected to the computer via USB.
SoftwareSerial on pins 4 & 5
To create a virtual serial port ('walkie-talkie') to talk to the ESP WiFi module, keeping the main serial port free for debugging.
AT+CIOBAUD=9600 and AT+RST commands
They are AT commands used to tell the ESP WiFi module to change its speed to a more stable 9600 baud and then reset to apply the change.
Udp.begin(local_port)
It 'opens the door.' It tells the Arduino to start listening for UDP packets on port 8888, officially starting the server.
First check inside the loop
int packetSize = Udp.parsePacket(); It checks if any client has 'knocked' (sent a UDP packet). If packetSize is 0, it does nothing.
Math analogRead(analogInPin)*100/1024
To convert the ADC's 10-bit reading (a number from 0 to 1023) into a simple percentage (a number from 0 to 99).
Creating a String and converting to char[]
String is easy to build (e.g., 'Gas Level: ' + 35). char[] is the raw data format required by Udp.write() to send over the network.
How Arduino knows where to send reply
It uses Udp.remoteIP(), a function that automatically gets the IP address of the client that sent the last packet.
Postcard vs. phone call analogy
UDP (postcard): Fast, simple, 'fire-and-forget.' No guarantee of delivery. TCP (phone call): Slow, reliable, requires a connection and handshake.
Why UDP is a good choice
It's fast, simple, and missing one sensor reading isn't critical. The app can just ask again.
Hardcoding WiFi credentials problem
Solution: Use a WiFi Manager library. It creates a temporary hotspot and a web page (captive portal) to let you configure WiFi from your phone.
Using String class on Arduino problem
Memory fragmentation, which can lead to random, unexplained crashes over time.
Safer alternative to String class
Use a char[] (character array) directly with the sprintf() function to format the text safely.
While(true); loops problem
They are 'blocking code' that freezes the Arduino. A better solution is to use a timeout and blink an LED to give visual feedback that something is wrong.