WEEK-10
AIM: Write a program to create UDP Server on cloud using Arduino and Respond with
humidity data to UDP Client when requested.
The UDP protocol has a small overhead of 8 bytes which makes it more
suitable for use in the Internet of Things. In this project, the application of UDP
protocol in IoT will be demonstrated. In this project, an ESP8266 Wi-Fi modem will
be configured as UDP server and a laptop will be used as UDP Client. Both Client and
server will be co-located communicating through same Wi-Fi router so, the ESP board
will act as a local serverThe ESP module working as server checks for the UDP
packet received from the client on a particular port. When a valid packet is arrived, an
acknowledge packet is sent back to the client to the same port it has been sent out. In
response to receiving packet and sending acknowledgement, the ESP modem switches
on an LED as visual indication of successful Client-Server Communication. This
application is very useful as it demonstrates server/client communication over UDP
protocol.
It can be said that the IOT device designed in this experiment is a simple UDP
server with LED indicator. It is designed by interfacing an LED with the ESP-8266
Wi-Fi module. The Wi-Fi module as well as LED light are powered continuously with
the help of a USB to Serial Converter. The Wi-Fi module needs to loaded with a
firmware that could receive data over UDP protocol and respond with an
acknowledgement. The Arduino UNO is used to flash the firmware code on the
ESP8266 module. The ESP module can also be flashed with code using a FTDI
converter like CP2102. The firmware itself is written in the Arduino IDE.
Software Required –
• ThingSpeak server
• Arduino IDE
ESP8266 board needs to be loaded with the firmware code. In this tutorial, the
firmware code is written using Arduino IDE. It is loaded to the ESP8266 board using
the Arduino UNO. A generic ESP8266 board is used in this project. This board does
not have any bootstrapping resistors, no voltage regulator, no reset circuit and no
USB-serial adapter. The ESP8266 module operates on 3.3 V power supply with
current greater than or equal to 250 mA. So, CP2102 USB to serial adapter is used to
provide 3.3 V voltage with enough current to run ESP8266 reliably in every situation.
The ESP8266 Wi-Fi Module is a self contained SOC with integrated TCP/IP protocol
stack that can access to a Wi-Fi network. The ESP8266 is capable of either hosting an
application or off loading all Wi-Fi networking functions from another application
processor. Each ESP8266 module comes pre-programmed with an AT command set
firmware.
UDP TERMINAL APP
Pin connection:
GND-GND
DATA- D5
VCC-VIN
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "DHT.h"
#define DHTTYPE DHT11
// Set WiFi credentials
#define WIFI_SSID "V2029"
#define WIFI_PASS "123456789"
#define UDP_PORT 4210
DHT dht(D5, DHT11);
// UDP
WiFiUDP UDP;
char packet[255];
char reply[] = "Packet received!";
void setup() {
// Setup serial port
Serial.begin(115200);
Serial.println();
Serial.println(F("DHTxx test!"));
dht.begin();
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
// Loop continuously while WiFi is not connected
while (WiFi.status() != WL_CONNECTED)
{
delay(2000);
Serial.print(".");
}
// Connected to WiFi
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Begin listening to UDP port
UDP.begin(UDP_PORT);
Serial.print("Listening on UDP port ");
Serial.println(UDP_PORT);
void loop() {
int h = dht.readHumidity();
int t = dht.readTemperature();
//float h = dht.readHumidity();
delay(2000);
// Send return packet
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(reply);
//UDP.write(Humidity);
// client.print("humidity :");
// client.println(h);
UDP.endPacket();
UDP.println(t);
UDP.println(h);
Serial.println(F("Humidity:\n "));
Serial.println(h);
}
Output:
Connected IP Address :192.168.137.191
Listening on UDP port : 4210
Humidity : 32
Receive at 192.168.137.69 : 4210
[192.168.137.69 : 4210]:31
33
Packet received.