Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simple_pubblish/simple_pubblish.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ void loop() {
lastRequest = now;
}
delay(10);
}
}
75 changes: 75 additions & 0 deletions examples/simple_pubblish_dht/simple_pubblish_dht.ino
Original file line number Diff line number Diff line change
@@ -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 <Emoncms.h>
// Include framework code and libraries
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#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);
}
13 changes: 13 additions & 0 deletions src/Emoncms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
4 changes: 3 additions & 1 deletion src/Emoncms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down