Skip to content

Commit 4a24a17

Browse files
authored
Merge pull request #70 from adafruit/io-garage
Adding IO:Garage example
2 parents d79b5a1 + c3eb044 commit 4a24a17

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "YOUR_AIO_USERNAME"
6+
#define IO_KEY "YOUR_AIO_KEY"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
14+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
15+
// - Feather WICED -> https://www.adafruit.com/products/3056
16+
17+
#define WIFI_SSID "YOUR_WIFI_SSID"
18+
#define WIFI_PASS "YOUR_WIFI_PASSWORD"
19+
20+
// comment out the following two lines if you are using fona or ethernet
21+
#include "AdafruitIO_WiFi.h"
22+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
23+
24+
25+
/******************************* FONA **************************************/
26+
27+
// the AdafruitIO_FONA client will work with the following boards:
28+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
29+
30+
// uncomment the following two lines for 32u4 FONA,
31+
// and comment out the AdafruitIO_WiFi client in the WIFI section
32+
// #include "AdafruitIO_FONA.h"
33+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
34+
35+
36+
/**************************** ETHERNET ************************************/
37+
38+
// the AdafruitIO_Ethernet client will work with the following boards:
39+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
40+
41+
// uncomment the following two lines for ethernet,
42+
// and comment out the AdafruitIO_WiFi client in the WIFI section
43+
// #include "AdafruitIO_Ethernet.h"
44+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Adafruit IO Garage
2+
// Tutorial Link: https://learn.adafruit.com/adafruit-io-home-garage
3+
//
4+
// Adafruit invests time and resources providing this open source code.
5+
// Please support Adafruit and open source hardware by purchasing
6+
// products from Adafruit!
7+
//
8+
// Written by Brent Rubell for Adafruit Industries
9+
// Copyright (c) 2018 Adafruit Industries
10+
// Licensed under the MIT license.
11+
//
12+
// All text above must be included in any redistribution.
13+
14+
/************************** Configuration ***********************************/
15+
16+
// edit the config.h tab and enter your Adafruit IO credentials
17+
// and any additional configuration needed for WiFi, cellular,
18+
// or ethernet clients.
19+
#include "config.h"
20+
21+
// include the servo library
22+
#include <Servo.h>
23+
24+
// include the ultrasonic library
25+
#include <Ultrasonic.h>
26+
27+
/************************ Example Starts Here *******************************/
28+
29+
#define SERVOPIN 15
30+
#define DOORPIN 0
31+
32+
// servo (garage door opener) angle presets
33+
#define DOORCLOSE_POS 30
34+
#define DOOROPEN_POS 130
35+
36+
// sketch starts assuming the the door is closed
37+
int doorState = LOW;
38+
39+
// ultrasonic sensor distance
40+
int ultrasonicRead;
41+
42+
// set up the ultrasonic object (trigger, echo)
43+
Ultrasonic ultrasonic(12, 13);
44+
// create servo object
45+
Servo doorServo;
46+
47+
// set up the Adafruit IO Feeds
48+
AdafruitIO_Feed *openGarage_feed = io.feed("garage-opener");
49+
AdafruitIO_Feed *closeGarage_feed = io.feed("garage-closer");
50+
AdafruitIO_Feed *carDistance_feed = io.feed("carDistance");
51+
AdafruitIO_Feed *doorStatus_feed = io.feed("doorStatus");
52+
53+
void setup() {
54+
55+
// start the serial connection
56+
Serial.begin(115200);
57+
58+
// wait for serial monitor to open
59+
while(! Serial);
60+
61+
// connect to io.adafruit.com
62+
Serial.print("Connecting to Adafruit IO");
63+
io.connect();
64+
65+
// set up message handlers
66+
openGarage_feed->onMessage(handleGarageOpener);
67+
closeGarage_feed->onMessage(handleGarageCloser);
68+
69+
70+
// wait for a connection
71+
while(io.status() < AIO_CONNECTED) {
72+
Serial.print(".");
73+
delay(500);
74+
}
75+
76+
// we are connected
77+
Serial.println();
78+
Serial.println(io.statusText());
79+
80+
// attach the servo object to pin 16
81+
doorServo.attach(SERVOPIN);
82+
// start with the door closed
83+
doorServo.write(DOORCLOSE_POS);
84+
}
85+
86+
void loop() {
87+
// io.run(); is required for all sketches.
88+
// it should always be present at the top of your loop
89+
// function. it keeps the client connected to
90+
// io.adafruit.com, and processes any incoming data.
91+
io.run();
92+
93+
// read the door sensor
94+
readDoorSensor();
95+
96+
// read the ultrasonic sensor
97+
ultrasonicRead = ultrasonic.distanceRead(INC);
98+
delay(1000);
99+
Serial.print("Distance from car (inches):"); Serial.println(ultrasonicRead);
100+
carDistance_feed->save(ultrasonicRead);
101+
}
102+
103+
// reads the status of the garage door and sends to adafruit io
104+
void readDoorSensor() {
105+
doorState = digitalRead(DOORPIN);
106+
if (doorState == LOW) {
107+
Serial.println("* Door Closed");
108+
doorStatus_feed->save(0);
109+
} else {
110+
Serial.println("* Door Open");
111+
doorStatus_feed->save(1);
112+
}
113+
delay(2000);
114+
}
115+
116+
// 'open garage' button handler
117+
void handleGarageOpener(AdafruitIO_Data *data) {
118+
Serial.println("Door Opened!");
119+
doorServo.write(DOOROPEN_POS);
120+
}
121+
122+
// 'close garage' button handler
123+
void handleGarageCloser(AdafruitIO_Data *data) {
124+
Serial.println("Door Closed!");
125+
doorServo.write(DOORCLOSE_POS);
126+
}
127+
128+

0 commit comments

Comments
 (0)