How to import feeds from an emonPi to an Arduino Uno via ethernet connexion?

Good morning,

I have a running solar PV emonPi for one year and now I would like to configure an independent arduino Uno with ethernet shield to access emoncms via my home ethernet connection and get some data to do actions to manage my home consumption.
At first, I would like to write an arduino sketch to get something like with this following working url, but on my arduino serial monitor (http://192.168.1.xx/emoncms/feed/get.json apikey=xxxxxxx&id=1&field=value).
Any recommendation or arduino sketch would be helpful.

Thanks.

I can’t recall any mention of anyone wanting to transfer the data in that direction. Most people send the data to the emonPi.

I think the emonCMS API calls are your starting point - send the appropriate request, and then parse out the bits of data that you require. There has to be a web scraper sketch for a Uno with Ethernet somewhere that does something like what you want, which you can use as a starting point.

Thank you Robert,

I found the following sketch on a blog (in french) and it works well.

// Ces deux bibliothèques sont indispensables pour le shield
#include <SPI.h>
#include <Ethernet.h>

// L'adresse MAC du shield
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x4E, 0x8E };
// L'adresse IP que prendra le shield
IPAddress ip(192,168,1,57);
// L'objet qui nous servira a la communication
EthernetClient client;
// Le serveur à interroger : "emoncms"
IPAddress server(192,168,1,xx);
//La clé API pour lecture seule
  String apikey_read = "xxx...";  //api read key
// pour lire les caractères
char carlu = 0;
// moment de la dernière requête
long derniereRequete = 0;
// temps minimum entre deux requêtes
const long updateInterval = 10000;
// mémorise l'état de la connexion entre deux tours de loop
bool etaitConnecte = false;

void setup() {
  // On démarre la voie série pour déboguer
  Serial.begin(9600);

  char erreur = 0;
  // On démarre le shield Ethernet SANS adresse IP (donc donnée via DHCP)
  erreur = Ethernet.begin(mac);

  if (erreur == 0) {
    Serial.println("Parametrage avec ip fixe...");
    // si une erreur a eu lieu cela signifie que l'attribution DHCP
    // ne fonctionne pas. On initialise donc en forçant une IP
    Ethernet.begin(mac, ip);
  }
  Serial.println("Init...");
  // Donne une seconde au shield pour s'initialiser
  delay(1000);
  Serial.println("Pret !");
}

void loop()
{
  // on lit les caractères s'il y en a de disponibles
  if(client.available()) {
    carlu = client.read();
    Serial.print(carlu);
  }

  // SI on était connecté au tour précédent
  // ET que maintenant on est plus connecté
  // ALORS on ferme la connexion
  if (etaitConnecte && !client.connected()) {
    Serial.println();
    Serial.println("Deconnexion !");
    // On ferme le client
    client.stop();
  }

  // Si on est déconnecté
  // et que cela fait plus de xx secondes qu'on a pas fait de requête
  if(!client.connected() && ((millis() - derniereRequete) > updateInterval)) {
    requete(); 
  }

  // enregistre l'état de la connexion (ouvert ou fermé)
  etaitConnecte = client.connected();
}

void requete() {
  // On connecte notre Arduino sur "emoncms" et le port 80 (defaut pour l'http)
  char erreur = client.connect(server, 80);

  if(erreur == 1) {
      // Pas d'erreur ? on continue !
      Serial.println("Connexion OK, envoi en cours...");

      // On construit l'en-tête de la requête
      client.print("GET /emoncms/feed/value.json?id=3&apikey=");
      client.print(apikey_read);
      client.println(" HTTP/1.1");
      client.println("Host: server");
      client.println("User-Agent: Arduino-ethernet");
      client.println("Connection: close");
      client.println();

      // On enregistre le moment d'envoi de la dernière requête
      derniereRequete = millis();
  } else {
    // La connexion a échoué :(
    // On ferme notre client
    client.stop();
    // On avertit l'utilisateur
    Serial.println("Echec de la connexion");
    switch(erreur) {
      case(-1):
        Serial.println("Time out");
        break;
      case(-2):
        Serial.println("Serveur invalide");
        break;
      case(-3):
        Serial.println("Tronque");
        break;
      case(-4):
        Serial.println("Reponse invalide");
        break;
    }
  }
}

This is what I get on my serial monitor.

36

My goal now is to write a sketch to activate a Grove Relay when the returned value remains above a threshold for a set amount of time. I found examples on blogs, but I have not had any results yet.

Although as Robert mentioned, most folks transfer the data in the opposite direction,
there was at least one other user that wanted to do the same thing. Here’s the thread:

1 Like

Thank you Bill,

This is similar to what I want to do and I will try to adapt this code to my project.