mirror of
				https://github.com/gabrielkheisa/antares-esp8266-mqtt.git
				synced 2025-11-03 22:39:21 +00:00 
			
		
		
		
	Pretty json print, added parsing functionality
This commit is contained in:
		@@ -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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								keywords.txt
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								keywords.txt
									
									
									
									
									
								
							@@ -19,6 +19,16 @@ ipToString	KEYWORD2
 | 
			
		||||
setMqttServer	KEYWORD2
 | 
			
		||||
checkMqttConnection	KEYWORD2
 | 
			
		||||
printdata	KEYWORD2
 | 
			
		||||
add	KEYWORD2
 | 
			
		||||
get	KEYWORD2
 | 
			
		||||
printData	KEYWORD2
 | 
			
		||||
publish	KEYWORD2
 | 
			
		||||
getTopic	KEYWORD2
 | 
			
		||||
getPayload	KEYWORD2
 | 
			
		||||
getInt	KEYWORD2
 | 
			
		||||
getFloat	KEYWORD2
 | 
			
		||||
getDouble	KEYWORD2
 | 
			
		||||
getString	KEYWORD2
 | 
			
		||||
 | 
			
		||||
######################################
 | 
			
		||||
# Constants (LITERAL1)
 | 
			
		||||
 
 | 
			
		||||
@@ -127,8 +127,16 @@ void AntaresESP8266MQTT::publish(String projectName, String deviceName) {
 | 
			
		||||
    String topic = "/oneM2M/req/" + _accessKey + "/antares-cse/json";
 | 
			
		||||
    String finalData;
 | 
			
		||||
 | 
			
		||||
    if(_debug) {
 | 
			
		||||
        DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
        JsonObject& object = jsonBuffer.parseObject(_jsonDataString);
 | 
			
		||||
        printDebug("[ANTARES] PUBLISH DATA:\n\n");
 | 
			
		||||
        object.prettyPrintTo(Serial);
 | 
			
		||||
        Serial.println("\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    _jsonDataString.replace("\"", "\\\"");
 | 
			
		||||
    Serial.println(_jsonDataString);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    finalData += "{";
 | 
			
		||||
    finalData += "\"m2m:rqp\": {";
 | 
			
		||||
@@ -161,6 +169,30 @@ void AntaresESP8266MQTT::publish(String projectName, String deviceName) {
 | 
			
		||||
    client.publish(topicChar, finalDataChar);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int AntaresESP8266MQTT::getInt(String key) {
 | 
			
		||||
    DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
    JsonObject& object = jsonBuffer.parseObject(_jsonSubDataString);
 | 
			
		||||
    return object[key];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float AntaresESP8266MQTT::getFloat(String key) {
 | 
			
		||||
    DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
    JsonObject& object = jsonBuffer.parseObject(_jsonSubDataString);
 | 
			
		||||
    return object[key];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
double AntaresESP8266MQTT::getDouble(String key) {
 | 
			
		||||
    DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
    JsonObject& object = jsonBuffer.parseObject(_jsonSubDataString);
 | 
			
		||||
    return object[key];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
String AntaresESP8266MQTT::getString(String key) {
 | 
			
		||||
    DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
    JsonObject& object = jsonBuffer.parseObject(_jsonSubDataString);
 | 
			
		||||
    return object[key];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AntaresESP8266MQTT::setCallback(std::function<void(char*, uint8_t*, unsigned int)> callbackFunc) {
 | 
			
		||||
    client.setCallback(callbackFunc);
 | 
			
		||||
}
 | 
			
		||||
@@ -169,12 +201,28 @@ bool AntaresESP8266MQTT::setDebug(bool trueFalse) {
 | 
			
		||||
    _debug = trueFalse;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
String AntaresESP8266MQTT::byteToString(byte* payload, unsigned int length) {
 | 
			
		||||
String AntaresESP8266MQTT::get(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
    _receivedTopic = String(topic);
 | 
			
		||||
 | 
			
		||||
    String payloadString;
 | 
			
		||||
    for(int i = 0; i < length; i++) {
 | 
			
		||||
        payloadString += char(payload[i]);
 | 
			
		||||
    }
 | 
			
		||||
    return payloadString;
 | 
			
		||||
 | 
			
		||||
    DynamicJsonBuffer jsonBuffer;
 | 
			
		||||
    JsonObject& object = jsonBuffer.parseObject(payloadString);
 | 
			
		||||
    String parsedString = object["m2m:rqp"]["pc"]["m2m:cin"]["con"];
 | 
			
		||||
    _jsonSubDataString = parsedString;
 | 
			
		||||
 | 
			
		||||
    return _jsonSubDataString;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
String AntaresESP8266MQTT::getTopic() {
 | 
			
		||||
    return _receivedTopic;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
String AntaresESP8266MQTT::getPayload() {
 | 
			
		||||
    return _jsonSubDataString;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
String AntaresESP8266MQTT::ipToString(IPAddress ip) {
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,9 @@ private:
 | 
			
		||||
    char* _wifiPass;
 | 
			
		||||
    String _accessKey;
 | 
			
		||||
    String _jsonDataString = "{}";
 | 
			
		||||
    String _jsonSubDataString;
 | 
			
		||||
    String _subscriptionTopic;
 | 
			
		||||
    String _receivedTopic;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    AntaresESP8266MQTT(String accessKey);
 | 
			
		||||
@@ -33,11 +35,20 @@ public:
 | 
			
		||||
    /* Overloaded functions end */
 | 
			
		||||
    void printData();
 | 
			
		||||
    void publish(String projectName, String deviceName);
 | 
			
		||||
    /* Get subscription callback data*/
 | 
			
		||||
    int getInt(String key);
 | 
			
		||||
    float getFloat(String key);
 | 
			
		||||
    double getDouble(String key);
 | 
			
		||||
    String getString(String key);
 | 
			
		||||
    /* Get data end */
 | 
			
		||||
 | 
			
		||||
    String getTopic();
 | 
			
		||||
    String getPayload();
 | 
			
		||||
 | 
			
		||||
    void setMqttServer();
 | 
			
		||||
    void checkMqttConnection();
 | 
			
		||||
    void setCallback(std::function<void(char*, uint8_t*, unsigned int)> callbackFunc);
 | 
			
		||||
    String byteToString(byte* payload, unsigned int length);
 | 
			
		||||
    String get(char* topic, byte* payload, unsigned int length);
 | 
			
		||||
    void setSubscriptionTopic();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user