0

Help understanding syntax of commands in io sketch
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help understanding syntax of commands in io sketch

by JackTheSE on Mon Feb 18, 2019 8:24 am

I have a "publish-subscribe" sketch running successfully on a feather connected to my io account. I push an On/Off button on my dashboard, and an led connected to the feather turns on or off. All that is good. I need help understanding the syntax of some of the code in the sketch, which I borrowed heavily from some of the IO examples:
(1) I'm trying to improve the following comments describing the sketch line of code at the end:

// On my IO.adafruit.com account, I have a dashboard called "Led On/Off".
// I have a feed called "OnOffFeed". On the dashboard I have a toggle block connected to
// OnOffFeed. Not sure of the syntax of the following line, nor what OnOffThing is.
AdafruitIO_Feed *OnOffThing = io.feed("OnOffFeed");

(2) At various points in the program I see a line of code like "OnOffThing->save("OFF");". I know this particular line of code in Setup turns the toggle block to OFF, but I don't know what the -> means, or why it is used.

(3) Down in the handleMessage section I get the IO message into a usable form by "String OOV = data->value();" but I really don't understand the syntax of why that works.

Help in understanding from an expert please?

JackTheSE
 
Posts: 14
Joined: Wed Jan 02, 2019 8:34 pm

Re: Help understanding syntax of commands in io sketch

by brubell on Tue Feb 19, 2019 11:03 am

(1) I'm trying to improve the following comments describing the sketch line of code at the end:
AdafruitIO_Feed *OnOffThing = io.feed("OnOffFeed");


I like to keep comments simple, this sets up the onoffthing feed object, with the `onofffeed` from IO.

Code: Select all | TOGGLE FULL SIZE
// Set up the 'onofffeed' feed
AdafruitIO_Feed *OnOffThing = io.feed("OnOffFeed");


(2) At various points in the program I see a line of code like "OnOffThing->save("OFF");". I know this particular line of code in Setup turns the toggle block to OFF, but I don't know what the -> means, or why it is used.

The arrow operator (http://www.fredosaurus.com/notes-cpp/structs/arrow.html) " dereferences a pointer to select a field." In our example, it sets the value of OnOffThing to `OFF` and then sends the value to Adafruit IO.

(3) Down in the handleMessage section I get the IO message into a usable form by "String OOV = data->value();" but I really don't understand the syntax of why that works.


Whenever a value is received from Adafruit IO on the OnOffThing feed, the handleMessage function will get called with the data from the feed.

For example, if the feed received a value of 'ON', ON is a character (https://en.cppreference.com/w/cpp/language/types). You're setting the character recieved to a string (string OOV).

You can also simplify this by instead using

Code: Select all | TOGGLE FULL SIZE
  String OOV = data->toString();


This'll convert the data character value to a string, then save it in the OOV variable.

brubell
 
Posts: 282
Joined: Fri Jul 17, 2015 10:33 pm

Re: Help understanding syntax of commands in io sketch

by JackTheSE on Wed Feb 20, 2019 7:24 am

brubell... Perfect, thanks. I'm new to OO languages obviously, but between my Arduino activities with C++, and Visual Basic for Applications, which I use heavily in Excel, I'm being dragged slowly to at least a feeble understanding of the topic. I've added both of the references you cited to my Arduino bookmarks folder for future reference. Is there a tutorial on C++ you would recommend? Thanks again...Jack

JackTheSE
 
Posts: 14
Joined: Wed Jan 02, 2019 8:34 pm

Re: Help understanding syntax of commands in io sketch

by brubell on Thu Feb 21, 2019 11:51 am

JackTheSE wrote:brubell... Perfect, thanks. I'm new to OO languages obviously, but between my Arduino activities with C++, and Visual Basic for Applications, which I use heavily in Excel, I'm being dragged slowly to at least a feeble understanding of the topic. I've added both of the references you cited to my Arduino bookmarks folder for future reference. Is there a tutorial on C++ you would recommend? Thanks again...Jack


There's an interactive tutorial over at https://www.learn-cpp.org.

brubell
 
Posts: 282
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.