Pretty json print, added parsing functionality

This commit is contained in:
vmasdani
2019-01-18 15:42:11 +07:00
parent 1808c55200
commit 6a542f4ead
5 changed files with 148 additions and 14 deletions

View File

@@ -1,3 +1,19 @@
/*
This is an example sketch to publish MQTT data to your
Antares IoT Platform project device via ESP8266.
MQTT server & port:
platform.antares.id, port 1338
MQTT topic:
/oneM2M/req/your-access-key/antares-cse/json
This sketch will deploy data to your Antares device
and publish to the MQTT topic simultaneously.
For more information, please visit https://antares.id/id/docs.html
*/
#include <AntaresESP8266MQTT.h>
#define ACCESSKEY "your-access-key"
@@ -15,14 +31,31 @@ void setup() {
antares.wifiConnection(WIFISSID, PASSWORD);
antares.setMqttServer();
}
void loop() {
/*
Check if we're still connected to the MQTT broker/server.
If disconnected, the device will try to reconnect.
*/
antares.checkMqttConnection();
antares.add("temperature", 30);
antares.add("humidity", 75);
antares.add("message", "Hello World!");
antares.add("temperature", 45);
antares.printData();
// Variable init
int temp = random(25,30) ;
int hum = random(75,90);
float windsp = float(random(20, 30))/3.33;
float rainlv = float(random(0, 20))/6.99;
String lat = "-6.8718189";
String lon = "107.5872477";
// Add variable to data storage buffer
antares.add("temperature", temp);
antares.add("humidity", hum);
antares.add("wind_speed", windsp);
antares.add("rain_level", rainlv);
antares.add("latitude", lat);
antares.add("longitude", lon);
// Publish and print data
antares.publish(projectName, deviceName);
delay(5000);
}

View File

@@ -1,3 +1,20 @@
/*
This is an example sketch to subscribe to MQTT data on ESP8266
via the Antares IoT Platform.
MQTT server & port:
platform.antares.id, port 1338
MQTT topic:
/oneM2M/req/your-access-key/antares-cse/json
The main function in this sketch is the callback function,
which will be fired every time a new message is published
to the topic.
For more information, please visit https://antares.id/id/docs.html
*/
#include <AntaresESP8266MQTT.h>
#define ACCESSKEY "your-access-key"
@@ -10,12 +27,23 @@
AntaresESP8266MQTT antares(ACCESSKEY);
void callback(char topic[], byte payload[], unsigned int length) {
String topicString = String(topic);
String payloadString = antares.byteToString(payload, length);
/*
Get the whole received data, including the topic,
and parse the data according to the Antares data format.
*/
antares.get(topic, payload, length);
Serial.println("[ANTARES] New Mesage: ");
Serial.println(topicString);
Serial.println(payloadString);
Serial.println("New Message!");
// Print topic and payload
Serial.println("Topic: " + antares.getTopic());
Serial.println("Payload: " + antares.getPayload());
// Print individual data
Serial.println("Temperature: " + String(antares.getInt("temperature")));
Serial.println("Humidity: " + String(antares.getInt("humidity")));
Serial.println("Wind speed: " + String(antares.getFloat("wind_speed")));
Serial.println("Rain level: " + String(antares.getFloat("rain_level")));
Serial.println("Latitude: " + antares.getString("latitude"));
Serial.println("Longitude: " + antares.getString("longitude"));
}
void setup() {
@@ -26,5 +54,9 @@ void setup() {
antares.setCallback(callback);
}
void loop() {
/*
Check if we're still connected to the MQTT broker/server.
If disconnected, the device will try to reconnect.
*/
antares.checkMqttConnection();
}