Examples¶
This section contains practical examples for the use of the DaaSIoT wrapper for ESP32. To test any of this programs, simply create a new PlatformIO project and add the source code to you main.cpp
Simple Node (sender/receiver)¶
This example executes the following operations: 1. Connect to a wireless network with provided credentials 2. Create a node with parameters SID=100 and DIN=200 3. Map to a remote node with DIN=201 4. Send "Hello from ESP32!" to remote node 4. Waits for a reply
To test this example:
1. Set up the credentials on WIFI_SSID and WIFI_PASS
2. Set up second node with the expected parameters and connect it to the same network, some options could be:
* Test using GNU or Windows SDKs for C++ with daas_cli example
* Upload the program to a second ESP32 device, setting the DIN accordingly (200 for the first device and 201 for the second)
#include <Arduino.h>
#include <WiFi.h>
#include "dw_esp32.h"
const int localSid = 100;
const int localDin = 200;
const int remoteDin = 201;
const unsigned long RECONNECT_INTERVAL_MS = 5000;
unsigned long lastAttempt = 0;
const String WIFI_SSID = ""; // SSID
const String WIFI_PASS = ""; // PASS
const String remote_ip = ""; // Target's IP
daas::api::dw_esp32* node = nullptr;
class event_daas : public IDaasApiEvent {
public:
void dinAcceptedEvent(din_t din) {}
void frisbeeReceivedEvent(din_t din) {}
void nodeStateReceivedEvent(din_t din) {}
void atsSyncCompleted(din_t) {}
void ddoReceivedEvent(int payload_size, typeset_t typeset, din_t din) {
DDO *pk;
if (node->pull(din, &pk) == ERROR_NONE){
unsigned char * payload = pk->getPayloadPtr();
Serial.printf("Payload: %s\n", payload);
Serial.println("");
delete pk;
}
}
void frisbeeDperfCompleted(din_t, uint32_t packets_sent, uint32_t block_size) {}
};
// WiFI Helpers
void printWiFiStatus() {
wl_status_t s = WiFi.status();
Serial.printf("WiFi status: %d\n", (int)s);
if (s == WL_CONNECTED) {
Serial.printf("IP address: %s\n", WiFi.localIP().toString().c_str());
Serial.printf("RSSI: %d dBm\n", WiFi.RSSI());
}
}
void connectToWiFi(const String WIFI_SSID, const String WIFI_PASS) {
if (WiFi.isConnected()) return;
Serial.print("Trying to connect to... ");
Serial.println(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
}
// DaaS Helpers
void receiveMessages() {
node->doPerform(PERFORM_CORE_NO_THREAD);
DDO* inboundData = nullptr;
unsigned char buffer[128] = {0}; // +1 for null terminator
if (node->pull(remoteDin, &inboundData) == ERROR_NONE && inboundData != nullptr) {
uint32_t size = inboundData->getPayloadSize();
inboundData->getPayloadAsBinary(buffer, 0, size);
buffer[size] = '\0';
Serial.println((char*)buffer);
}
}
void sendMessages(din_t remoteDin, std::string msg) {
//std::string msg = "hello from ESP32!";
DDO out(msg.size());
out.allocatePayload(msg.size());
out.setPayload(msg.c_str(), msg.size());
out.setTimestamp(0);
int res = node->push(remoteDin, &out);
node->doPerform(PERFORM_CORE_NO_THREAD);
}
daas_error_t runNode(const std::string local_ip, const std::string remote_ip) {
daas_error_t err;
err = node->setupNode(localSid, localDin, _LINK_INET4, (local_ip + ":2020").c_str());
if(err != ERROR_NONE) return err;
err = node->map(remoteDin, _LINK_INET4, (remote_ip+"2022").c_str());
if(err != ERROR_NONE) return err;
err = node->locate(remoteDin);
if(err != ERROR_NONE) return err;
// err = node->doPerform(PERFORM_CORE_THREAD);
// if(err != ERROR_NONE) return err;
else return ERROR_NONE;
}
void node_setup(const std::string remote_ip) {
Serial.println("=== DaaS Wrapper Embedded Test ===");
Serial.println(node->getInfos());
Serial.println();
// Available drivers
auto availableDrivers = node->listAvailableDrivers();
Serial.println("Available Drivers:");
Serial.println(availableDrivers);
daas_error_t result = runNode(WiFi.localIP().toString().c_str(), remote_ip);
if(result != ERROR_NONE) return;
else Serial.println("Node Initialized successfully!");
delay(1000);
std::string message = "Hello from ESP32";
Serial.print("Sent to remote node: ");
Serial.println(message.c_str());
sendMessages(remoteDin, message);
}
void keepAlive() {
if (!WiFi.isConnected()) {
unsigned long now = millis();
if (now - lastAttempt >= RECONNECT_INTERVAL_MS) {
lastAttempt = now;
Serial.println("Not connected. Attempting reconnect...");
connectToWiFi(WIFI_SSID, WIFI_PASS);
}
} else {
static unsigned long lastPrint = 0;
if (millis() - lastPrint > 10000) {
lastPrint = millis();
Serial.println("Connected — ");
}
receiveMessages();
}
delay(10);
}
void setup() {
event_daas *handler = new event_daas();
node = new daas::api::dw_esp32(handler);
Serial.begin(115200);
delay(500);
connectToWiFi(WIFI_SSID, WIFI_PASS);
while (!WiFi.isConnected()) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi connected!");
printWiFiStatus();
node_setup(remote_ip.c_str());
}
void loop() {
keepAlive();
}
Save and Load Config¶
This example demonstrates the use of the helper class device_storage to allow the storage of node configurations on the microcontroller's filesystem, implemented with LittleFS.
1. Save Data¶
#include <Arduino.h>
#include <WiFi.h>
#include "dw_esp32.h"
#include "device_storage.h"
daas::api::dw_esp32* node = nullptr;
device_storage* device = nullptr;
void printDinList(const dinlist_t& list) {
Serial.print("[");
for (size_t i = 0; i < list.size(); i++) {
Serial.print(list[i]);
if (i < list.size() - 1) Serial.print(", ");
}
Serial.println("]");
}
void setup() {
node = new daas::api::dw_esp32(nullptr);
device = new device_storage();
Serial.begin(115200);
delay(500);
printDinList(node->listNodes()); // Prints available Nodes before initializing);
if(node->setupNode(100, 200, _LINK_NONE, "addr")) Serial.println("Node setup successfully!");
else Serial.println("Init operation failed!")
printDinList(node->listNodes()); // Print available Nodes after Loading;
if(node->storeConfiguration(device)) Serial.println("Data stored successfully!");
else Serial.println("Store operation failed!");
}
void loop() {
delay(10);
}
2. Load Data¶
#include <Arduino.h>
#include <WiFi.h>
#include "dw_esp32.h"
#include "device_storage.h"
daas::api::dw_esp32* node = nullptr;
device_storage* device = nullptr;
void printDinList(const dinlist_t& list) {
Serial.print("[");
for (size_t i = 0; i < list.size(); i++) {
Serial.print(list[i]);
if (i < list.size() - 1) Serial.print(", ");
}
Serial.println("]");
}
void setup() {
node = new daas::api::dw_esp32(nullptr);
device = new device_storage();
Serial.begin(115200);
delay(500);
printDinList(node->listNodes()); // Prints available Nodes before loading);
if(node->loadConfiguration(device)) Serial.println("Data loaded successfully!");
else Serial.println("Operation failed!");
printDinList(node->listNodes()); // Print available Nodes after Loading;
}
void loop() {
delay(10);
}