Posting data from Electron to Emoncms using particle.publish()

Hi I am working on monitoring system, monitoring water temperature and level in reservoir for small scale hydro. I have been using long range transmitters to transmit data to photons which then post straight to my emoncms server. But now when the electron is coming it would make more sense to skip the two wireless transmitters and the photon and have the electron post the data to emoncms. To save data and battery (by running for as short time as possible befor going to sleep) I believe (correct me if I am wrong) the best method to port messages is to use particle.publish() publishing a csv string with the values being monitored, I assume this will be no problem to do. But what I am not sure of is what would be the best method to relay the csv messages to my emoncms server? I am installing 10-20 systems per year so that is approx the scale of what I am doing… (here I start talking about things I am not familiar with and might be talking bullshit :slight_smile: ) I need an application to subscribe to my particle messages and then relay it to emoncms, I know that one solution is to use node-red and have that subscribe to each of my particle devices and link it to emoncms account, but to do this I have to have node-red running on a PC 24hours a day all year around, which does not make too much sense (in my mind atleast)… is there any easier way to accomplish what I am trying to do (and preferably is “free”)

How do you currently send the data from your Photon to your server?
Possibly this mightstill work with the Electron (providing your server is publicly accessable).

thanks for the reply, yes my server is publicly accessible, to give you the best answare I will copy/paste in my code (I got it originally from someone else in here and have made some changes to make it fit for me, before copy/pasting I deleted some of the code (not needed to explain your question, hope I did not delete to much)

this is how I do it today using photons, which are constantly connected to the internet, it might be ok to skip some of the delays (atleast the 10000 msek one in the setup routine) what I am afraid of (but don’t have knowledge to judge) is the the electron will be using too much data (and time) doing it this way instead if simply using particle.publish. at the moment I have a motino waking up for approxm 1 second every 10 minutes, measuring and transmitting the data wirelessly to another motion which is connected to the serialport of the photon, while at sleep the motino is drawing less than 10µA and the maximum power consumption when transmitting is in the 200mA range if I remember right. I know that these low power consumption numbers will be impossible for the electron, but still it could deliver acceptable results, I think that delivering data once every hour would be acceptable, the batteries I am using now are 4xAA in series and have been lasting since december 2014 (15 months) and are still going, I expect them to last atleast 24 months, I would say that 12 months lasting time on AA batteries (2-4pcs) would be acceptable for the electron, the absolute minimum I think would be 6 months.

anyway… here is the code… :smile:

//get csv string from arduino and send to emoncms, en axample string would be A4123,456,7.89; where A is simply to get rid of everything else coming from the Arduino serialport except strings starting with “A” then the first number (which is 4) represents the node id in emoncms, the rest of the string are the data numbers 123 456 and 7.89, semicolon ; simply represents the end of the csv file so when reading the serialport you know when the last character has arrived

TCPClient client;
//EnergyMonitor emon1;

//char incoming;
#define SERIAL_BAUD   115200

char nodeID;
int ledPin = D7; 
int i=0;

char payload[62] = "1" ;
const int SAMPLEINTERVAL = 10000; //time between samples
char * partOfString; // this is used by strtok_r() as an index
float Powerhouse[10];  // TempBearing, TempCoil, TempOutPwrHouse, Nozzle1, Nozzle2, Flow, Pressure, Current, Voltage, Frequency; //Powerhouse
float Intake[10];  // Interval,waterlevel,watertemp,airtemp,batteryVoltage,messageNR_inntak,messageNR_repeater,ExtraCycles,RSSI_repeater_intake,RSSI_GTW_repeater


void sendData(char node, char dataToSend[62]){
    client.connect("myPCaddress",80);//your PC address or emoncms.org
    delay(500);
    if (client.connected()) {
        digitalWrite(ledPin, HIGH);  //set LED high if client responded (should send ok)
        client.flush(); //clear any rubbish
        client.print("GET, /emoncms/input/post.json?node=");
        client.print(node);
        client.print("&csv=");
        client.print(dataToSend);
        client.println("&apikey=myWriteAPIkey");   // insert write key instead of myWriteAPIkey
        delay(500);
        
        if (client.available()){
            if (client.read()=='o') digitalWrite(ledPin, HIGH);  //set LED high if client responded (should send ok)
        }
        client.print("Host: ");
        client.println("hus.elab.is");
        client.println("Connection: close"); 
        client.println();
        client.flush();
        delay(400);
        client.stop();
    } 
    else {
        client.flush();
        client.stop();
        digitalWrite(ledPin, LOW);
    }
}


void setup() {
    Serial1.begin(115200);
    delay(10);
    pinMode(ledPin, OUTPUT);
    delay(10000);
}

void loop() {
    digitalWrite(ledPin, LOW);
    if (Serial1.available()) { 
        char incoming = Serial1.read();
        if (incoming=='A') {
            i=0;  
            nodeID = Serial1.read();
            while (payload[i-1]!=';'){
                payload[i]=Serial1.read();
                i++;
            } 
            payload[i]='\0'; 
            sendData(nodeID,payload);//send data to emoncms
        }
    }
}

You should also investigate web hooks. The idea is you do a Particle.publish to publish some data from a Photon or Electron. It’s really simple to do so, and is data-efficient on the Electron. The web hook makes it possible to make a request to another web server or web service when one of your devices does a publish. You could probably post directly to your emoncms server, I think. And web hooks are built into the particle cloud software and are free.
https://docs.particle.io/guide/tools-and-features/webhooks/

1 Like

Webhooks are definetly worth a look.

It might be worth a try to just use that code with your Electron.

Just some alterations might be good to make for the sake of reducing data and power consumption.

e.g. to reduce TCP overhead, construct the full output via snprintf() and do client.print() only once per transmission - maybe also rethink if the need for the “handshake”.

For power consumption you need to use some kind of sleep mode, which in turn requires a way to synchronize your sensors with the waking times.

1 Like

I used a webhook to publish to emoncms. Json for webhook is available a https://gist.github.com/tiagonmas/1ba4c7fb451aa9d06b8c

1 Like

directly posting the json for the webhook to emoncms

{
	"eventName": "emoncms",
	"url": "https://emoncms.org/input/post.json",
	"requestType": "GET",
	"query": {
		"apikey": "<YOUR API KEY>",
		"json": "{{SPARK_EVENT_NAME}}:{{SPARK_EVENT_VALUE}}"
	},
	"noDefaults": true,
	"mydevices": true
}
3 Likes

@tiagonmas So this is all you need to send your data to a custom EMONCMS account? Pretty simple if so :smiley:

1 Like

Do you know if it is possible to use webhhok to post to emoncms and post as a specific node? I have data coming from two devices and I somehow have to distinguish one from another, best way is if they can use separate node id’s

Since {{SPARK_EVENT_NAME}} is always emoncms, what do you write in {{SPARK_EVENT_VALUE}} that is recognized by emoncms?