[SOLVED]Particle.variable shows no values

Dear all,
this week is my - particle week - I’m trying erverything :wink: And sometimes I need a helping hand :wink:

I tried to use that example: Logging an Graphing using Goolge but it failed…
Everytime if I open:

https://api.spark.io/v1/devices/YOUR-DEVICE-ID/result?access_token=YOUR-ACCESS-TOKEN 

I get as result for the variables only:

  "variables": {
    "result": "string"
  },

even if I use something simple: like

Particle.variable("time", Time.now() );

I only get:

  "variables": {
    "result": "string",
    "time": "int32"
  },

But I expected a timestamp to see…
What am I doing wrong?

Thanks in advance

You need to show the context (full code, complete function, …) rather than just one line of code.

Hi ScruffR,

it is the same thing from yesterday - only next steps…

    // This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"

//Schaltplan
/* 
Pulse counter to measure used kWh and calculate current W usage from a GM3D

GND ---\
       |
       O 10k
       |
D3 ----+------ 41

3v3 -----------42

*/



//für den HTTP Client
    unsigned int nextTime = 0;    // Next time to contact the server
    HttpClient http;

    // Headers currently need to be set at init, useful for API keys etc.
    http_header_t headers[] = {
        //  { "Content-Type", "application/json" },
        //  { "Accept" , "application/json" },
        { "Accept" , "*/*"},
        { NULL, NULL } // NOTE: Always terminate headers will NULL
    };
    
    http_request_t request;
    http_response_t response;

//Variablen für Grundeinstellungen
    byte TryNr = 114;                         //Grundeinstellung/Versuchs Nr 
    int ReportIntervall = 1;                  //Grundeinstellung/ Wie oft soll reported werden? In ganzen Minuten also 1, 2, 5, 10 etc !!
    byte minPulseWidth = 25;                  //Grundeinstellung/ Eltako pulse lenght 30ms
//Variablen für Berechnungen
    char resultstr[64];                       //Versuch
    unsigned long PulseTime=0;                //Berechnungen/ Misst die Pulselänge
    volatile unsigned int PulseCount=0;       //Berechnungen/ Zählt die Impule die länger als 25ms sind
    volatile unsigned int DifPulseCount=0;    //Berechnungen/ Anzahl der Impule zwischen zwei Meldungen
    volatile unsigned int OldPulseCount=0;    //Berechnungen/ Zur Berechnung neu-alt benötigt
    volatile unsigned long PulsePower=0;      //Berechnungen/ Zur Berechnung der Watt benötigt
    volatile unsigned int kwhCount=0;         //Berechnungen/ Zur Berechnung der kwh benötigt
    int Timestamp=0;                          //Berechnungen/ Zur Anzeige des Timestamps benötigt
    String deviceID=Particle.deviceID();;     //Berechnungen/ Zur Anzeige der ParticleID benötigt

//Funktion blink die ausgelöst wird, wenn ein Pulse erfolgt
void blink() {
  if ( (millis() - PulseTime) > minPulseWidth) {
    PulseCount++;
    kwhCount = PulseCount / 2000;
  }
}

//Funktion Setup, die einmalig am Anfang ausgeführt wird
void setup() {
  pinMode(D3, INPUT);                     //SETUP/ Hier wird festeglegt, welcher Pin wie reagiert
  attachInterrupt(D3, blink, RISING);     //SETUP/ Hier wird festgelegt, welche Funktion ausgfeführt wird, wenn am Pin etwas geschieht
  Particle.publish("power-meter-reporter", "gestarted - Version: "+String(TryNr)); //Ein Lebenszeichen wird beim Neustart ausgesandt

  Particle.variable("result", resultstr, STRING); //Versuch1
  Particle.variable("time", Time.now() );         //Versuch2
}

//Loop
void loop() {
  Timestamp = Time.now();                            //Holt sich den aktuell Timestamp
  DifPulseCount = PulseCount - OldPulseCount;        //Berechnet die Differnz zur letzten Meldung 
  PulsePower = DifPulseCount * 30 / ReportIntervall; // DifPulseCount / 2000 * 1000 / 1 * 60; // Könnte die Watt im Zeitraum 1 Minute (delay unten) berechnen
  PulseTime = millis();                              //Für die Berechnung benötigt 
  OldPulseCount = PulseCount;                        // Setzt den akutellen Wert
  
  //http Client
  request.hostname = "www.diefrieds.de";
  request.port = 80;
  //request.path = "/Testumgebung/Photon/";
  request.path = String::format("/Testumgebung/Photon/index.php?deviceID=%s&Timestamp=%d&PulseCount=%d&DifPulseCount=%d&kwhCount=%d&PulsePower=%d"
                             , (const char*)deviceID
                             , Timestamp 
                             , PulseCount
                             , DifPulseCount
                             , kwhCount
                             , PulsePower
                             );                             
                             
  http.get(request, response, headers);
  //Particle.publish("power-meter-reporter", "request - " + String(request.hostname) + String(request.path) + String(request.body) + String(response) );
  Particle.publish("power-meter-reporter", "kwh - " + String(kwhCount) + " - Difpulses - " + String(DifPulseCount));
  int data1 = analogRead(A0); // read some data
  int data2 = analogRead(A1); // some some other data
  sprintf(resultstr, "{\"data1\":%d,\"data2\":%d}", data1, data2);
  
  delay(ReportIntervall * 60 * 1000); //Publish every (10Sec = 10000, 60sec = 60000)
}

And the API response:

{
  "id": "310040000b47343138333038",
  "name": "live_photon_02_powermeter",
  "connected": true,
  "variables": {
    "result": "string",
    "time": "int32"
  },
  "functions": [],
  "cc3000_patch_version": "wl0: Nov  7 2014 16:03:45 version 5.90.230.12 FWID 01-640014e",
  "product_id": 6,
  "last_heard": "2015-11-30T13:10:53.730Z"
}

This won't work since you are exposing a temporary string which was created by a function (now()) and will cease to exist after a while and will definetly not be updated as time moves on.
However Particle.variable("time", Timestamp); should work.
And your resultstr should work just fine too.

To see your exposed variables more conveniently, you could try one of these

1 Like

Ok, as the resultstr ist only showing "“string” in the api-response I tried it with something else - the time.now() thing… and used something that couldn’t work ;-(

I changed it to “Timestamp”, but the API response is still the same:

{
“id”: “310040000b47343138333038”,
“name”: “live_photon_02_powermeter”,
“connected”: true,
“variables”: {
“result”: “string”,
“time”: “int32”
},
“functions”: [],
“cc3000_patch_version”: “wl0: Nov 7 2014 16:03:45 version 5.90.230.12 FWID 01-640014e”,
“product_id”: 6,
“last_heard”: “2015-11-30T13:36:07.977Z”
}

Ok using github.io I could see the values… but shouldn’t instead of “string” and “int32” the values be seen here?
The example shows the values… ???

Apparently you’re calling the device endpoint, which will tell you which variables are exposed, as well as what type they are. For the actual values, you’ll have to poll the variables endpoint. That’s what the sites mentioned by @ScruffR do, which is why they work :wink:

1 Like

Are you sure you’ve set the correct access token and device ID in your URL?
When I do this (in Chrome)

https://api.spark.io/v1/devices/<myDeviceID>/result?access_token=<myAccessToken>
https://api.spark.io/v1/devices/<myDeviceID>/time?access_token=<myAccessToken>

I get

{
  "cmd": "VarReturn",
  "name": "result",
  "result": "-1448891338-",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2015-11-30T13:48:59.191Z",
    "connected": true,
    "last_handshake_at": "2015-11-30T13:48:39.675Z",
    "deviceID": "<myDeviceID>",
    "product_id": 6
  }
}

{
  "cmd": "VarReturn",
  "name": "time",
  "result": 1448891695,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2015-11-30T13:54:55.743Z",
    "connected": true,
    "last_handshake_at": "2015-11-30T13:54:33.629Z",
    "deviceID": "<myDeviceID>",
    "product_id": 6
  }
}

It seems that both of you are right (I didn’t expect something else :wink: ) but it makes me a bit sad…
The URL I used (and always refreshed) was:

https://api.particle.io/v1/devices/310040000b47343138333038/?access_token=<withmycorrecttoken>

But that was missing something: the “result” so I always got only which variables were exposed…

Thanks to you both… !!!

2 Likes

Yup, just noticed that the URL you quoted above can’t have been the one you used :wink:

Good catch @Moors7 :+1:

2 Likes

2 Likes