2020
2121/* *********************** Example Starts Here *******************************/
2222
23+
24+ // Relay is connected to PyPortal's D3 connector
25+ #define RELAY_POWER_PIN 3
26+
27+ // Set up the 'relay feed'
28+ AdafruitIO_Feed *relay = io.feed(" relay" );
29+
2330void setup () {
2431
2532 // start the serial connection
@@ -33,6 +40,12 @@ void setup() {
3340 // connect to io.adafruit.com
3441 io.connect ();
3542
43+ // set up a message handler for the 'relay' feed.
44+ // the handleMessage function (defined below)
45+ // will be called whenever a message is
46+ // received from adafruit io
47+ relay->onMessage (handleMessage);
48+
3649 // wait for a connection
3750 while (io.status () < AIO_CONNECTED) {
3851 Serial.print (" ." );
@@ -43,6 +56,9 @@ void setup() {
4356 Serial.println ();
4457 Serial.println (io.statusText ());
4558
59+ // Get the last known value from the feed
60+ relay->get ();
61+
4662}
4763
4864void loop () {
@@ -53,5 +69,27 @@ void loop() {
5369 // io.adafruit.com, and processes any incoming data.
5470 io.run ();
5571
72+ }
73+
74+ // this function is called whenever an 'relay' feed message
75+ // is received from Adafruit IO. it was attached to
76+ // the 'relay' feed in the setup() function above.
77+ void handleMessage (AdafruitIO_Data *data) {
78+
79+ Serial.print (" feed received new data <- " );
80+ Serial.println (data->toChar ());
5681
57- }
82+ // Check to see if the morning scheduled trigger has executed
83+ if (strcmp (data->toChar (), " morning" ) == 0 ) {
84+ Serial.println (" Turning lights ON" );
85+ digitalWrite (RELAY_POWER_PIN, HIGH);
86+ }
87+ // Check to see if the evening scheduled trigger has executed
88+ else if (strcmp (data->toChar (), " night" ) == 0 ) {
89+ Serial.println (" Turning lights OFF" );
90+ digitalWrite (RELAY_POWER_PIN, LOW);
91+ }
92+ else {
93+ Serial.println (" Unexpected data received from Adafruit IO" );
94+ }
95+ }
0 commit comments