Reading Json file data via Particle variable

#include "WiFi.h"
#include "application.h"

char buf[1024];

void setup() 
{
    WiFi.on();
    Serial.begin(57600);
    Serial1.begin(57600);

    Particle.variable("WiFi_Responce", buf);
}


void loop() 
{
    int Status;
    WiFiAccessPoint ap;
    static unsigned long TenSecSync = millis();

    if (millis() - TenSecSync > TEN_SEC)
    {
        memset(buf, 0, sizeof(buf));
        JSONBufferWriter writer(buf, sizeof(buf) - 1);

        writer.beginObject();
        writer.name("1").value("A");
        writer.name("2").value("B");
        writer.endObject();

        Serial.println(buf);
        Particle.publish("Wifi_Data", buf);
        TenSecSync = millis();
    }
    Particle.process();
}

When I view Particle.publish(“Wifi_Data”, buf) I get:
{"1":"A","2":"B"}

When I print buf I get:
{"1":"A","2":"B"}

When I read the data off the cloud (on my device) I get:
"{"1":"A","2":"B"}"
I don’t know why I am getting the two(2) extra quotes?

First some notes about your code
#include "WiFi.h" is not required and #include "application.h" should rather be #include <Particle.h>.

Since you are not stating any other SYSTEM_MODE() your code will be running SYSTEM_MODE(AUTOMATIC) which renders WiFi.on() in setup() superfluous - as is Particle.process() in loop().

I also don’t see the use of WiFiAccessPoint ap in your loop() as you are nowhere using it.

However, can you explain what you mean with

How do you read the date off the cloud and on which device?

Also what is the difference between this and

Where would you view that if not “off the cloud”?

1 Like

Here is some slightly different code.

//#include "WiFi.h"
#include <Particle.h>

char buf[1024];
//WIFI wifi;

void setup() 
{
    Serial.begin(57600);
    Serial1.begin(57600);
    SYSTEM_MODE(AUTOMATIC);
    Particle.variable("WiFi_Responce", buf);
    Particle.function("WIFI", Wififunctions);
}


void loop() 
{
    static unsigned long TenSecSync = millis();

    if (millis() - TenSecSync > TEN_SEC)
    {
        memset(buf, 0, sizeof(buf));
        JSONBufferWriter writer(buf, sizeof(buf) - 1);

        writer.beginObject();
            writer.name("1").value("A");
            writer.name("2").value("B");
        writer.endObject();

        Serial.println(buf);
        Particle.publish("Wifi_Data", buf);
    TenSecSync = millis();
    }
}

Data seen via Particle Console {“1”:“A”,“2”:“B”}


fig 1.

Data seen via print {“1”:“A”,“2”:“B”}
I am using HMI Editor app on my ipad when I request data stored in Particle.variable(“WiFi_Responce”, buf), the response is:


fig 2.

Data is "{“1”:“A”,“2”:“B”}"
S/B {“1”:“A”,“2”:“B”}
Hope this clears things up some.

1 Like

This is most likely a combination of two factory

  1. The publish payload is not sent as a JSON object but as string value wrapped in the publish package (which is a JSON object)
  2. Your HMI Editor fails to correctly escape embedded double quotes inside of strings

When you use particle subscribe you will see how a string with embedded double quotes should be displayed to make clear which double quotes are constituting the boundaries of the string and which are embedded in the string
It should look something like this

{"result":"{\"1\":\"A\", \"2\":\"B\"}", "cmd": "VarReturn", ...}
1 Like

It’s looks like is something wrong with your HMI Editor app as @ScruffR mentioned.
I was so curious what is going wrong as I never hit this issue with JSON Buffer Writer so I made a simple html file which gets Particle.variable named json_test and flash exactly part of your code to one of my photon and I got correct answer as @ScruffR shows how this should look like:

here is my HTML for test:

<html>
<head>
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/particle-api-js@8/dist/particle.min.js"></script>
  <meta charset="utf-8"/>
</head>
<style>  
.btn-fs {
  display: inline-block;
  top: 20px;
  left: 10px;
  padding: 5px;
  color: black;
  border: 3px solid green;
  border-radius: 7px;
  z-index: 1;
  cursor: pointer;
  font-size: 30px;
}           
</style> 
<body>
<div class="btn-fs" id="btnFS">
    Reflesh
</div>

<script>
var particle = new Particle();
var accessToken = "not display for security"; 
var deviceID = "not display for security";

var fs = document.getElementById('btnFS'); 
fs.addEventListener('click', get_data.bind(null, 'json_test'), false);

 function get_data(VarName){
     particle.getVariable({ deviceId: deviceID , name: VarName, auth: accessToken }).then(
     function(data) {
         console.log('Device variable retrieved successfully:', JSON.parse(data.body.result));      
         }, 
    function(err) {
        console.log('An error occurred while getting attrs:', err);
       });
 }

get_data('json_test');

</script>
</body>
</html>

and here are the results:

Great feedback guys as always. Thanks.