Latest Secure UDP NetCoap Version 2.0.2 is available
Latest Secure UDP NetCoap Version 2.0.2 is available
Please refer to NetCoap github for more details on installation, building, and execution
Let's say that we want to publish weather data such as (1) precipitation (2) temperature, and (3) humidity using CBOR. However, now that we are only interested in the temperature and humidity (filtering).
Note: You MUST complete the installation and building steps as described in the GitHub NetCoap before proceeding to the next steps.
string g_NetCoapCONFIG_FILE = "..." ;
Client* g_client = nullptr;
UdpClientDtlsIo g_dtls;
JsonPropTree g_cfg;
g_cfg.fromJsonFile(g_NetCoapCONFIG_FILE);
g_client = new Client(g_cfg, g_dtls);
bool connected = g_client->connect();
if (!connected) {
exit(1);
}
string g_dataUriPath = "/www/topic/ps/weather";
string g_topicName = "Weather";
string g_topicUriPath = "/www/topic/ps";
// Create topic and register the below callback
g_client->createTopic(
g_topicName, g_topicUriPath, g_dataUriPath,
"weather", Message::CONTENT_FORMAT::APP_CBOR,
createTopicCb);
void createTopicCb(
Client::STATUS status,
const shared_ptr<Message> respMsg) {
// Output helpful info return from creating topic
Helper::syncOut() << "***** Create Topic Helpful Info *****\n";
JsonPropTree jsonTree;
jsonTree.fromCborStr(*respMsg->getPayload());
jsonTree.print();
}
// Subscribe to Weather topic and register the below callback
// We are only interested in temperature and humidity
g_client->subscribe(g_dataUriPath, subscribeCb, "temperature&humidity");
void subscribeCb(
Client::STATUS status,
const shared_ptr<Message> respMsg) {
shared_ptr<string> payLoad = respMsg->getPayload();
JsonPropTree jsonPropTree;
jsonPropTree.fromCborStr(*payLoad);
float temperature = jsonPropTree.get<float>("temperature");
Helper::syncOut() << "Temperature: " << fixed << setprecision(2)
<< temperature << "\n";
float humidity = jsonPropTree.get<int>("humidity");
Helper::syncOut() << "Humidity: " << humidity << "\n";
}
// ===== Publish Temperature and Humidity =====
float temperature = 70.5; // Assume Fahrenheit
int humidity = 100; // Assume percent. Air is saturated
string jsonTempHumidity =
string("{") +
"\"" + "humidity" + "\"" + ':' + to_string(humidity) + "," +
"\"" + "temperature" + "\"" + ':' + to_string(temperature) +
string("}");
shared_ptr<string> tempHumidityDat(new string());
jsonPropTree.fromJsonStr(jsonTempHumidity);
jsonPropTree.toCborStr(*tempHumidityDat);
// Publish data using CBOR and reliable transmit
// The humidity&temperature param allows the subscriber
// to query for keywords
g_client->publish(
g_dataUriPath, tempHumidityDat,
Message::CONTENT_FORMAT::APP_CBOR,
true, "humidity&temperature");
// ===== Publish Precipitation =====
// ===== May be subscribe later but not using it now.
// ===== The data will be delivered to the CoapBroker and
// ===== discarded because there is no subscriber
// ===== to query for keyword precipitation
float precipitation = .25; // Rain .25 inch
string jsonPrecipitation =
string("{") +
"\"" + "precipitation" + "\"" + ':' + to_string(precipitation) +
string("}");
shared_ptr<string> precipitationDat(new string());
jsonPropTree.fromJsonStr(jsonPrecipitation);
jsonPropTree.toCborStr(*precipitationDat);
// Publish data using CBOR and reliable transmit
g_client->publish(
g_dataUriPath, precipitationDat,
Message::CONTENT_FORMAT::APP_CBOR,
true, "precipitation");
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.