Send Shelly data to emoncms

Hello everyone,

I would like to share my Shelly script, which can be used to send data from an energy meter directly (without MQTT detour) to emoncms.
If you don’t know the Shelly energy meters, they are small inexpensive devices (available from 15 €) based on an esp8266/esp32 and a dedicated energy measurement chip (AD7953).

Note: Only Pro and Plus devices support scripting.

 // Shelly GEN2 script: HTTP GET - Send energy meter watts to emoncms

// Settings
let user_apikey = "xxxxx"; // Copy from emoncms.org > my account > Read & Write API Key
let name = "shelly";  // set a custom name for your shelly

let url = "https://emoncms.org/input/post?node=";

// Define timespan: sec * 1000 milliseconds
let interval = 10 * 1000;

// Set timer which send the HTTP GET
Timer.set(
  interval,
  true,
  function () {let status = Shelly.getComponentStatus("switch", 0);
  let url_send = url + name + "&json={Power:" + JSON.stringify(status.apower) + ",Wh:" + JSON.stringify(status.aenergy.total) + ",V:" + JSON.stringify(status.voltage) + "}&apikey=" + user_apikey;
  Shelly.call("HTTP.GET", {"url": url_send});
}
);
2 Likes

Nice! Thanks for sharing.

The only thing I’d suggest is using HTTP POST so that the API key is not left exposed in the url.

Hi Tim, thanks for the feedback, you’re right, I’ll try the variant with post.

The switch to using HTTP POST has unfortunately not yet worked, but I have discovered that https connections also work, so the problem should no longer be that urgent.

I have updated the code so that Wh and voltage are now also transmitted. I also noticed that the values were not being updated at all (the same value was being sent every 10 seconds), so I have now moved the reading of the values to the timer, which solves the problem.

Thank you for this! I have 20 of the things mqttt ing into emoncms via node red…busy and fragile. I will look forward to having a go over the weekend. Cheers!

Having just been coding some emoncms POSTing, how does this work without HTTPS being set up?
I couldn’t get data to go to emoncms without it being secured in some way.

In ESP32 (for an M5StickCPlus2) I had to do define the rootcertificate of emoncms in code then use specific https libraries to get the data to go:

Does this help?

https://emoncms.org/Modules/site/api.php

Basically you just need to add your APIKEY to the post.

Or are you saying that the ESP32 won’t let you send anything except to an https address?

Just as additional confirmation, this definitely works as you describe. Doing it from an ESP32 programmed with Arduino C++ and no https. Apikey at the end and that’s it

Thanks @djh and @Andre_K
Maybe I’ve just used a sledgehammer to crack a nut! (this is where I got the info: ESP32 HTTPS Requests (Arduino IDE) | Random Nerd Tutorials)

It’s quite likely I got a security related error and thought I’d implement HTTPS as the solution, whereas fixing my HTTP code may have been simpler…

So I just put my first Shelly in (my own house to test it)

I bought a 3M Pro, so the code didn’t work straight away.

Top tip for anyone trying this is to go to the local IP address (not via the cloud):
image

look at the Diagnostics tab, I had to change “switch”, 0 to “em”, 0 and use the available data points which seem to differ between models

1 Like

Nice, thanks for sharing, very useful!

1 Like