Use spark.publish() AND http.get() in one app?

Hi Guys

I would like to use spark.publish to interact with, and listen to events from my CORE. However, I would also like to use something like http.get (“HttpClient/HttpClient.h”) to log data on my public server. I normally do a http.get with
URL = http://www.mydomain.com/LogCore.aspx?Value=123. This allows me to dump the values into a DB where I can later do charting etc.

The problem is that the two will not work together in one app.

How can I post values to my server while also using spark.variable/function/publish?

Many thanks

Hi @oks

Maybe you should show us your code–I can do that and it works fine for me! There are limits on how often you can publish (once per second on average with a burst of up to four allowed) but otherwise it should be fine.

@bko

Sorry for the delay in replying.

This is the code. It runs and posts successfully to my server, but when I do a web request to it, it flashes SOS in red and reboots.


#include “application.h”
#include “HttpClient/HttpClient.h”

unsigned int nextTime = 0; // Next time to contact the server
char message[60];
int loopcount = 0;
char pinStatus[64] = “NONE”;

HttpClient http;
http_request_t request;
http_response_t response;
http_header_t headers[] = {
{ “Content-Type”, “application/json” },
{ “User-agent”, “Particle HttpClient”},
{ “Accept” , “/”},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};

//------------------------------------------------------------------------------------

void setup() {

nextTime = millis() + 30000; // give some time before we start looping.
loopcount = 0;


sprintf(message, "");
request.body = message;
request.hostname = "www.sysweb.co.za";
request.port = 80;

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);


// Cloud functions
Spark.function("fnrouter", fnRouter);
// Cloud variables
Spark.variable("pinStatus", &pinStatus, STRING);

// update the "pinStatus" global variable with the current pin statusses.
updateStatus(); 

}

void loop() {
// This is where I will poll for button press

///The next part of the loop only runs every 30 seconds
if (nextTime > millis()) { 
    return; 
    }

loopcount++;    //this is to see how many times the loop runs before it crashes

String ppath = "";
ppath.concat("/Core.aspx?TestVer=12|loop:");
ppath.concat(String(loopcount));
request.path = ppath;

// Get request
http.get(request, response, headers);
nextTime = millis() + 30000;

}

//webhook function to call from jquery
int fnRouter(String command) {
command.trim();
command.toUpperCase();

if(command.equals("D0HIGH")) {digitalWrite(0, HIGH);}
if(command.equals("D1HIGH")) {digitalWrite(1, HIGH);}
if(command.equals("D2HIGH")) {digitalWrite(2, HIGH);}
if(command.equals("D3HIGH")) {digitalWrite(3, HIGH);}

if(command.equals("D0LOW")) {digitalWrite(0, LOW);}
if(command.equals("D1LOW")) {digitalWrite(1, LOW);}
if(command.equals("D2LOW")) {digitalWrite(2, LOW);}
if(command.equals("D3LOW")) {digitalWrite(3, LOW);}

updateStatus();
return 0;

}

//function to update the pinStatus variable used for web requests.
void updateStatus(){
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;

val0 = digitalRead(D0);  // read input value
val1 = digitalRead(D1);  // read input value
val2 = digitalRead(D2);  // read input value
val3 = digitalRead(D3);  // read input value

String strPinStatus = "";
if (val0 == HIGH) { strPinStatus.concat("1|");      } else {    strPinStatus.concat("0|");    }
if (val1 == HIGH) { strPinStatus.concat("1|");      } else {    strPinStatus.concat("0|");    }
if (val2 == HIGH) { strPinStatus.concat("1|");      } else {    strPinStatus.concat("0|");    }
if (val3 == HIGH) { strPinStatus.concat("1|");      } else {    strPinStatus.concat("0|");    }
strPinStatus.toCharArray(pinStatus, 64);

}

Hi @oks

Try taking the ampersand "&" out of this. You want the address of the array pinStatus like this:

Spark.variable("pinStatus", pinStatus, STRING);

Wow such a small thing! So far so good. Can you give more detail about the “&” role?

Hi @oks

In C, the “&” operator means “take the address of” so for an integer, the address is a pointer to the memory location where that integer value is stored. But for an array in C, the name of the array (like pinStatus) without any dereferencing using []'s is already the address of the array in memory. So you don’t need the “&” for an array.

I am not getting any red flashing, but it still reboots in less than 15mins. Also, calling the web functions are not very responsive and can still cause it to reboot.

It is a lot better, but i suspect having a web call at the same time as the “get” still causes issues.

After testing it through the night it would seem all problems still exist.
It posts fine and without any web hook calls it made it through the night without resetting, but as soon as I requested a variable this morning it gave me the red flashing SOS again and reset.