|
| 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