JSON Web Client
Reads JSON from external web servers and from another Q5 running the JSON Web
Server example — POSTing a payload, checking err/code, and parsing the
response.
// Example JSON web client showing reading from external web servers and from another Q5 running the JSON WEB SERVER example
setEnv("http.aesKey","myAesPassphrase"); // Set end to end encryption key (Only used if encryption is enabled), if not set the device password is used
function getDateFromRemoteQ5(url)
{
let request = {
msg: "hello from client",
epoch: datetime().epoch
};
let res = http.req(url,
string(request), // POST request payload
1000, // timeout
false // no encryption
);
if(res.err === 0 && res.code === 200) // Valid response, process it!
{
let payload = JSON.parse(res.payload); // Parse text payload into a JSON object
print("Remote Q5 Battery Voltage",payload.batteryVoltage);
print(string(payload)); // Print the entire payload to the log
} else {
print("Error reading data from remote Q5");
print(string(res));
}
print("");
}
while(run()){
// Request data from a remote Q5 running the json web server demo, change the URL to the appropriate URL of your device for testing
getDateFromRemoteQ5("http://172.16.220.39/app/abc");
let res = http.req("http://172.16.220.39/app/xyz",
"", // No Payload
1000, // 1s timeout
false // no aes encryption
);
print(string(res));
waitMS(5000);
}