Author Topic: Help publishing to cloud  (Read 2096 times)

brohan

  • Jr. Member
  • **
  • Posts: 59
Help publishing to cloud
« on: June 24, 2016, 03:08:33 pm »
I am having issues, when performing the photocell tutorial, publishing the ADC value to the cloud. From a previous post it seems I should use Particle.publish, which takes:

Particle.publish(const char *eventName, const char *data);

How would I convert the integer to a constant char? 
Below is my program that give me the error "no matching function for call to 'CloudClass::publish(const char [6], String)'"

// variable to store our reading
int light;
int ledPin = 1;

void setup()
{               

  // enable the on-board LED as an output
  pinMode(ledPin, OUTPUT);
 
  // initialize the analog pin as an input
  pinMode(A0, INPUT);

}


// the loop() routine runs over and over again
void loop()
{

  // take a reading
  light = analogRead(A0);
 
  // print to cloud
  Particle.publish("light", String(analogRead(A0),DEC));
 
 
  // turn on/off the LED based on light reading
  if(light < 800) { digitalWrite(ledPin, HIGH); }
  if(light >= 800) {digitalWrite(ledPin, LOW); }
 
  // a short delay
  delay(250);

}

I had a line:  Particle.publish("light"); which did publish the word light to my particle log, so I know that the connection is made etc.

Thank you for the help.

brohan

  • Jr. Member
  • **
  • Posts: 59
Re: Help publishing to cloud
« Reply #1 on: June 24, 2016, 05:21:23 pm »
This worked for me, however, is it the best way?

#
// variable to store our reading
int light;
int ledPin = 1;
char ADC_status[64];

void setup()
{               

  // enable the on-board LED as an output
  pinMode(ledPin, OUTPUT);
 
  // initialize the analog pin as an input
  pinMode(A0, INPUT);

}


// the loop() routine runs over and over again
void loop()
{

  // take a reading
  light = analogRead(A0);
 
  // print to cloud
  sprintf(ADC_status, "%04d", light); //convert int to string for Particle.publish
  Particle.publish("light", ADC_status);
 
 
  // turn on/off the LED based on light reading
  if(light < 800) { digitalWrite(ledPin, HIGH); }
  if(light >= 800) {digitalWrite(ledPin, LOW); }
 
  // a short delay
  delay(250);

}
#
« Last Edit: June 24, 2016, 08:37:18 pm by brohan »

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Help publishing to cloud
« Reply #2 on: June 24, 2016, 08:11:30 pm »
I don't know about the 'best' way, but if it works for you that is a start!  ;D

Using sprintf isn't such a bad idea, as it is a pretty standard way of formatting strings, and gives you a lot of control over that formatting.

The ADC_status buffer variable can be a lot smaller if you want - it only needs to be the length of your longest string + 1 (for a null terminator character). Since the ADC value won't be larger than a 4 digit number, the array could be 5 instead of 64, but I would use 8 (but only as it is a multiple of 8, no other reason).

For floating point numbers, I do something like
Code: [Select]
dtostrf(celsius, 5, 2, tmpStr); //input variable, total number of digits to output, number of digits after decimal, output variable
Particle.publish("tempC",tmpStr,30,PRIVATE); //event name, data to publish,valid for duration,private event

For integer numbers, you can use
Code: [Select]
itoa(intVar,charVar,10); //where the 10 represents which number system, 10 being decimal numbers

Size-wise, that will make negligible difference in the case of the Oak - about 10 bytes less of memory and flash usage. For the flexibility of sprintf, I'd stick with that.

btw, you can hit the '#' button on the forum post editor to insert or format text as code so it does the monospaced front and puts a scroll window in for longer code.

brohan

  • Jr. Member
  • **
  • Posts: 59
Re: Help publishing to cloud
« Reply #3 on: June 24, 2016, 08:39:43 pm »
Thank you for the reply and help. I'm starting to realize the power of such a small device.