20
20
21
21
/* *********************** Example Starts Here *******************************/
22
22
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
+
23
30
void setup () {
24
31
25
32
// start the serial connection
@@ -33,6 +40,12 @@ void setup() {
33
40
// connect to io.adafruit.com
34
41
io.connect ();
35
42
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
+
36
49
// wait for a connection
37
50
while (io.status () < AIO_CONNECTED) {
38
51
Serial.print (" ." );
@@ -43,6 +56,9 @@ void setup() {
43
56
Serial.println ();
44
57
Serial.println (io.statusText ());
45
58
59
+ // Get the last known value from the feed
60
+ relay->get ();
61
+
46
62
}
47
63
48
64
void loop () {
@@ -53,5 +69,27 @@ void loop() {
53
69
// io.adafruit.com, and processes any incoming data.
54
70
io.run ();
55
71
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 ());
56
81
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