Implementation of CoAP on ESP32 for IoT Communication

Objective

To implement CoAP (Constrained Application Protocol) on ESP32 for lightweight, efficient IoT communication. CoAP is optimized for low-power and resource-constrained devices, making it ideal for IoT applications.

Methodology

  • Set up an ESP32 with a CoAP client.
  • Use the CoAP Simple library to communicate with a CoAP server.
  • Send sensor data to a CoAP cloud server (e.g., CoAPthon, Eclipse Californium, or ThingsBoard).

Implementation Steps

Hardware Requirements

1ESP32 Development Board
2DHT11 or DHT22 Sensor (for temperature & humidity)
3Wi-Fi connectivity
4Jumper wires

Step 1: Install Required Libraries

1Arduino IDE
2Install ESP32 board support (https://dl.espressif.com/dl/package_esp32_index.json)
3Install CoAP Simple Library
4Install DHT Sensor Library

Step 2: CoAP Client Code for ESP32

This code reads sensor values from DHT11 and sends it to the CoAP server every 5 seconds.

1#include <WiFi.h>
2  #include <coap-simple.h>
3  #include <DHT.h>
4  
5  #define WIFI_SSID "YourWiFiSSID"
6  #define WIFI_PASSWORD "YourWiFiPassword"
7  
8  #define DHTPIN 4  // Pin where the DHT sensor is connected
9  #define DHTTYPE DHT11
10  
11  DHT dht(DHTPIN, DHTTYPE);
12  WiFiUDP udp;
13  Coap coap(udp);
14  
15  void callback_response(CoapPacket &packet, IPAddress ip, int port) {
16      Serial.println("Response received from CoAP Server");
17  }
18  
19  void setup() {
20      Serial.begin(115200);
21      WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
22      
23      while (WiFi.status() != WL_CONNECTED) {
24          delay(500);
25          Serial.print(".");
26      }
27      
28      Serial.println("Connected to WiFi");
29  
30      coap.start();
31      coap.response(callback_response);
32  }
33  
34  void loop() {
35      float temperature = dht.readTemperature();
36      float humidity = dht.readHumidity();
37  
38      if (!isnan(temperature) && !isnan(humidity)) {
39          String payload = "{\"temperature\":" + String(temperature) + ", \"humidity\":" + String(humidity) + "}";
40  
41          coap.put(IPAddress(192, 168, 1, 100), 5683, "sensor/data", payload.c_str());
42          Serial.println("Data Sent: " + payload);
43      } else {
44          Serial.println("Failed to read from DHT sensor");
45      }
46  
47      coap.loop();
48      delay(5000); // Send data every 5 seconds
49  }

Step 3: CoAP Server Setup

Choose either a local CoAP server or a cloud-based one like ThingsBoard.

1**Option 1: Local CoAP Server (CoAPthon)**
2`pip install coapthon3`
3Run this server script in Python:
4from coapthon.server.coap import CoAP
5  from coapthon.resources.resource import Resource
6  
7  class SensorResource(Resource):
8      def __init__(self, name="sensor"):
9          super(SensorResource, self).__init__(name)
10          self.payload = "Waiting for data"
11  
12      def render_PUT(self, request):
13          self.payload = request.payload
14          print("Received Data: ", self.payload)
15          return self
16  
17  server = CoAP(("0.0.0.0", 5683))
18  server.add_resource("sensor/data/", SensorResource())
19  server.listen(10)
20**Option 2: Cloud CoAP Server (ThingsBoard)**
21Use the CoAP endpoint: `coap://demo.thingsboard.io/api/v1/YOUR_ACCESS_TOKEN/telemetry`
22Replace IP in ESP32 code with `demo.thingsboard.io`.

Applications

  • Smart Agriculture: Send real-time field data using CoAP with minimal power usage.
  • Home Automation: Control or monitor sensors in a local network using CoAP.
  • Industrial IoT: Use CoAP for low-bandwidth, high-efficiency telemetry.

Future Research Concepts

  • Secure CoAP (DTLS-based) communication with ESP32.
  • Implementing CoAP Observe for real-time push updates.
  • Comparing CoAP vs MQTT in resource-constrained environments.