From 64c709e37e374af4f07c6027aef86d817d0b4e55 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 10 Dec 2017 20:43:55 +0100 Subject: [PATCH] Added support for float values --- examples/simple_pubblish/simple_pubblish.ino | 2 +- .../simple_pubblish_dht.ino | 75 +++++++++++++++++++ src/Emoncms.cpp | 13 ++++ src/Emoncms.h | 4 +- 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 examples/simple_pubblish_dht/simple_pubblish_dht.ino diff --git a/examples/simple_pubblish/simple_pubblish.ino b/examples/simple_pubblish/simple_pubblish.ino index 0fa1153..e0c8419 100644 --- a/examples/simple_pubblish/simple_pubblish.ino +++ b/examples/simple_pubblish/simple_pubblish.ino @@ -54,4 +54,4 @@ void loop() { lastRequest = now; } delay(10); -} +} \ No newline at end of file diff --git a/examples/simple_pubblish_dht/simple_pubblish_dht.ino b/examples/simple_pubblish_dht/simple_pubblish_dht.ino new file mode 100644 index 0000000..7cb5a05 --- /dev/null +++ b/examples/simple_pubblish_dht/simple_pubblish_dht.ino @@ -0,0 +1,75 @@ +/* + * This sketch is a basic example to send data on emoncms.org due to the + * Arduino_EmonCMS library. + * + * at the moment the data published are random value, + * substitute the data1 and data2 with your measure + * written by Giacomo Leonzi + */ + +// Define DHT Sensor Libraries +#include "DHT.h" +#define DHTTYPE DHT21 // Define the temperature sensor type +#define DHTPIN 14 +DHT dht(DHTPIN, DHTTYPE, 30); + +#include +// Include framework code and libraries +#include +#include +#include +#include +#include +#include + +#define APIKEY "xxxxx" // replace your emoncms apikeywrite here +#define HTTPPORT 80 // change this value to point at a local +#define HOST "emoncms.org" //installation of the platform emoncms.org + +const char* ssid = "xxx"; // SSID name +const char* password = "xxx"; // SSID password + +// set up net client info: +const unsigned long postingInterval = 10000; //delay between updates to emoncms.com +unsigned long lastRequest = 0; // when you last made a request + +// Temperature and humidity +float h=0,t=0; +WiFiClient client; +Emoncms emoncms; //Initialize the object + +void setup() { + emoncms.begin(HOST, HTTPPORT, APIKEY,client); + Serial.begin(115200); + delay(10); + + Serial.print("Connecting to "); + Serial.println(ssid); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void loop() { + + int data1 = int(random(0, 30)); //replace this value with your + int data2 = int(random(50, 100)); //measure + long now = millis(); + if (now - lastRequest >= postingInterval) { + // Reading temperature or humidity takes about 250 milliseconds! + h = dht.readHumidity(); + t = dht.readTemperature(); + + emoncms.addValue("h", h); + emoncms.addValue("t", t); + Serial.println(emoncms.publish()); //open the connection and publish + lastRequest = now; + } + delay(10); +} diff --git a/src/Emoncms.cpp b/src/Emoncms.cpp index 6226c55..2772621 100644 --- a/src/Emoncms.cpp +++ b/src/Emoncms.cpp @@ -48,6 +48,19 @@ void Emoncms::addValue(String key, int value) { _jsonString += value; } +/************************************************************************************* +* addValue - function to build the string json format to comunicate with emoncms.org* +* you need to give a name value colled key and it's value in a float variable * +************************************************************************************/ + +void Emoncms::addValue(String key, float value) { + + if(_jsonString!="")_jsonString +=",";//add the comma only when is not the first or only data added + _jsonString += key; + _jsonString += ":"; + _jsonString += String(value); +} + /************************************************************************************* * publish - function to publish on emoncms.org the data measured * * this function build the string to send, open the comunication through the server * diff --git a/src/Emoncms.h b/src/Emoncms.h index c8d9c4d..c67eace 100644 --- a/src/Emoncms.h +++ b/src/Emoncms.h @@ -33,7 +33,9 @@ Emoncms(); void begin(String host, int port, String APIKEY,Client& _client); - void addValue(String, int); //need key, name of parameter measured, and its value + void addValue(String, int); //need key, name of parameter measured, and its value + void addValue(String, float); //need key, name of parameter measured, and its value + String publish();//publish private: