How Can I Publish Int / Float

I’m converting my code with sprint() to publlish it to the cloud as a string. Here’s the before and after. Does it look right? I can’t tell if it’s working.

BEFORE…

int touchNumber;

void setup() {
    pinMode(A0, INPUT);
    Serial.begin(9600);
    Particle.variable("touch_Number", touchNumber);

}

void loop() {
    touchNumber = analogRead(A0);
    Serial.println(touchNumber);
    if (touchNumber > 2000)
    {
        Particle.publish("touchMe",touchStr, 60, PRIVATE);
        delay(1000);
    }
    delay(1000);
}

AFTER…

int touchNumber;
char touchStr[30];

void setup() {
    pinMode(A0, INPUT);
    Serial.begin(9600);
    Particle.variable("touch_Number", touchNumber);
    sprintf(touchStr,"%d", touchNumber);

}

void loop() {
    sprintf(touchStr,"%d", touchNumber);
    touchNumber = analogRead(A0);
    Serial.println(touchNumber);
    if (touchNumber > 2000)
    {
        Particle.publish("touchMe",touchStr, 60, PRIVATE);
        delay(1000);
    }
    delay(1000);
}

Why could you not tell if it’s working?
But I do see some issue in the order of execution :wink:
How would you retroactively push the value of analogRead() into the string you filled before?

One note on sprintf() tho’. When you can be absolutely certain that your buffer will be long enough for the data you want to insert (as clearly is the case in this example) then you can stick with sprintf() but for cases were there might be just a tiny bit of doubt I’d recommend using snprintf().

changed the order of the sprintf().
would there be anything else different beside changing sprintf() to snprintf()?

Also touchNumber would only be 3 digit voltage reading so it should be ok right?

Here’s the output from the subscription through the CLI. I don’t know if it’s publishing correctly because “data”: … Was text before and came out as “Touch Me”… but now it’s coming out at as “4000”. So in this case I’m not sure if the quotation marks are standard or if the represent a String in the output side.

void loop() {
    touchNumber = analogRead(A0);
    sprintf(touchStr,"%d", touchNumber);
  
    Serial.println(touchNumber);
    if (touchNumber > 2000)
    {
        Particle.publish("touchMe",touchStr, 60, PRIVATE);
        delay(1000);
    }
    delay(1000);
}

@LADD, things look better but you don’t need the delay(1000) right after the Particle.publish() since there is already a delay after the if (touchNumber > 2000) statement.

Also, if loop() has more things to execute, you may want to wrap the entire touchNumber code in a non-blocking timer like this. The if() and accompanying code will only run every second, assuming that’s what you want.

unsigned long touchtimer;    // This is a global variable

setup()
{

	touchTimer = millis();		// add this at the end of setup()
}

void loop()
{
	if ((millis() - touchTimer) >= 1000)
	{
		touchTimer = millis();

		touchNumber = analogRead(A0);
		sprintf(touchStr,"%d", touchNumber);
		
		Serial.println(touchNumber);
		if (touchNumber > 2000)
		{
			Particle.publish("touchMe",touchStr, 60, PRIVATE);
		}
	} // end if non-blocking timer check
	
	// ... rest of code to be run in loop()
	
}
1 Like

This is the JSON formatted event stream.
If you go to Particle Console | Build your connected product you'll get a nicer look at the event data.

And yes, this output is pretty much what one would expect from Particle.publish()

This is just a placeholder so I can understand the format of int and publish. I’ve got it to work and pushed through to my google sheet via IFTTT.

Before, I was just using a button connect to 3.3v. Now I need to swap that out for my INA216 current sensor and switch the int (%d) for a float (%f) and I’ll be set right?

I’m using this page as a simple guide which has really been helping.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.