Close

Sending data to emonCMS server

A project log for ESP Swiss Knife

This battery powered device is a all-in-one toolbox : WiFi AP, 3D printer remote, clock, serial console, sensors hub, and many more...

arcadia-labsArcadia Labs 05/25/2016 at 19:570 Comments

The device sends its voltage and temperature to my emoncms server.

Emoncms (https://openenergymonitor.org/emon/) is an open-source web-based software for monitoring home power, temperature and sensors. This is a central part of my DIY home automation system.

Emoncms exposes an API, so reading / posting data to the server is very easy.

Here is the piece of code I'm using :

#define emoncms_host "1.2.3.4"
const int emoncms_httpPort = 80;
String emoncms_apikey = "YOUR_API_KEY";
int node = 1;

void postEmoncms() {
  // run only if the ESP is connected to a network
  if ( WiFi.status() == WL_CONNECTED ) {
    WiFiClient client;
    if (!client.connect(emoncms_host, emoncms_httpPort)) {
      Serial.println("Post to emoncms failed");
      return;
    }
    // We now create a URI for the request
    String url = "GET /emoncms/input/post.json?apikey=" + emoncms_apikey + "&node=" + node + "&json={2:" + temperature + ",1:" + vcc + "";
    client.println(url);
    client.print("Host: ");
    client.println(emoncms_host);
    client.println("Connection: close");
    client.println();
    client.stop(); //Stopping client
    Serial.println("Post to emoncms : success");
  }
  else {
    Serial.println("Post to emoncms : not connected to internet");
  }
}
Of course, this function shouldn't run constantly in the main loop, but should run on timed intervals. I'm using RBD_Timer lib (https://github.com/alextaujenis/RBD_Timer)

This function runs in the background, so there won't be any screenshot this time !

Discussions