after some great advice on the forum i have followed a tutorial and edited it.
i want to blink 2 LEDS then publish when they are on or off however i am getting errors in my code but do not know what they mean. here is the code:
//unsigned long lastTime = 0UL;
char publishString[40];//the varible which holds the string for the publish
int led1 = D4; // Instead of writing D0 over and over again, we'll write led1
int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
char LED1info;
char LED2info;
void setup() {
pinMode(led1, OUTPUT); // declare that led1 pin D4 is an output
pinMode(led2, OUTPUT); // declare that led2 pin D7 is an output
}
void loop() {
// unsigned long now = millis();
//Every 15 seconds publish uptime
//if (now-lastTime>15000UL) {
//lastTime = now;
// now is in milliseconds
// nowSec = now/1000UL;
// unsigned sec = nowSec%60;
// unsigned min = (nowSec%3600)/60;
// unsigned hours = (nowSec%86400)/3600;
//sprintf(publishString,"LED1 is:LED2 is",LED1info,LED2info);
// Spark.publish("Uptime",publishString);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
LED1info = "on";
LED2info = "on";
sprintf(publishString,LED1info,LED2info);
Spark.publish("Uptime",publishString);
// We'll leave it on for 1 second...
delay(10000);
// Then we'll turn it off...
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
// Wait 1 second...
delay(100000);
}
here are the errors
project1.cpp: In function 'void loop()':
project1.cpp:29:14: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
//sprintf(publishString,"LED1 is:LED2 is",LED1info,LED2info);
^
project1.cpp:30:14: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
// Spark.publish("Uptime",publishString);
^
project1.cpp:31:42: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
from ../wiring/inc/spark_wiring_print.h:31,
from ../wiring/inc/spark_wiring_string.h:34,
from ../wiring/inc/spark_wiring_stream.h:30,
from ../wiring/inc/spark_wiring.h:39,
from ./inc/application.h:36,
from project1.cpp:2:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:217:5: error: initializing argument 2 of 'int sprintf(char*, const char*, ...)' [-fpermissive]
int _EXFUN(sprintf, (char *__restrict, const char *__restrict, ...)
^
project1.cpp:32:4: warning: 'Spark' is deprecated (declared at ../wiring/inc/spark_wiring_cloud.h:271): Spark is now Particle. [-Wdeprecated-declarations]
digitalWrite(led1, HIGH);
^
make[1]: *** [../build/target/user/platform-6project1.o] Error 1
make: *** [user] Error 2
Error: Could not compile. Please review your code.
It was kind of difficult to fix only your compilation errors because it still wouldn’t have worked quite right. I cleaned up and reorganized the code a bit; see if this makes sense to you.
//unsigned long lastTime = 0UL;
char publishString[40];//the varible which holds the string for the publish
int led1 = D4; // Instead of writing D0 over and over again, we'll write led1
int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
void setup() {
pinMode(led1, OUTPUT); // declare that led1 pin D4 is an output
pinMode(led2, OUTPUT); // declare that led2 pin D7 is an output
}
void loop() {
// unsigned long now = millis();
//Every 15 seconds publish uptime
//if (now-lastTime>15000UL) {
//lastTime = now;
// now is in milliseconds
// nowSec = now/1000UL;
// unsigned sec = nowSec%60;
// unsigned min = (nowSec%3600)/60;
// unsigned hours = (nowSec%86400)/3600;
//sprintf(publishString,"LED1 is:LED2 is",LED1info,LED2info);
// Spark.publish("Uptime",publishString);
updateLeds(true, true);
// We'll leave it on for 10 seconds...
delay(10000);
// Then we'll turn it off...
updateLeds(false, false);
// Wait 10 seconds...
delay(10000);
}
void updateLeds(bool on1, bool on2) {
// Turn the LEDs on and off
digitalWrite(led1, on1);
digitalWrite(led2, on2);
// Publish and event with the states of the LEDs
snprintf(publishString, sizeof(publishString), "LED1 is:%s, LED2 is: %s",
on1 ? "on" : "off", on2 ? "on" : "off");
Particle.publish("onoff", publishString, 60, PRIVATE);
}
thanks for that i got it working with ints instead of chars the way i did it before, i have change to your code but know am unable to see the pjublised data even thought i have changed uptime to onoff in the html code???
can i ask a few questions about how you wrote your code:
. what is snprint f
.what is the PRIVATE bit in particvle publish string
snprintf is like sprintf but it takes a new parameter in the second position, the size of the buffer passed in the first position. Say you had declared publishedString like this:
char publishedString[10];
The string you’re writing is much longer than 10 characters. If you use snprintf the resulting string will be truncated. If you use sprintf, it will write past the end of the buffer, corrupting random stuff in memory. The worst part about that is that it may not crash immediately, it may crash at some random point in the future, which makes debugging very difficult!
thanks, so i have got it working at the moment in with PRIVATE however i can still see the token and device id in the html code on the webpage should this be hidden and or is this what you mean by private?
No, when you publish event without the PRIVATE flag it means that anyone in the world subscribe to that event and see the values you publish. Private means that only devices, servers, web pages, etc. that log into your account can see the events.
I recently did a tutorial on using Particle API JS without storing the key in the Javascript code. You can look at the page source there to get some ideas of how it works: