|
| 1 | +// |
| 2 | +// Copyright 2015 Google Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +// FirebaseRoom_ESP8266 is a sample that demo using multiple sensors |
| 18 | +// and actuactor with the FirebaseArduino library. |
| 19 | + |
| 20 | +#include <ESP8266WiFi.h> |
| 21 | +#include <FirebaseArduino.h> |
| 22 | + |
| 23 | +void setup() { |
| 24 | + Serial.begin(9600); |
| 25 | + |
| 26 | + // power grove connector |
| 27 | + pinMode(15, OUTPUT); |
| 28 | + digitalWrite(15, HIGH); |
| 29 | + |
| 30 | + // pin 5 is connected to a vibrator motor. |
| 31 | + pinMode(5, OUTPUT); |
| 32 | + // pin A0 is connected to a light sensor. |
| 33 | + pinMode(A0, INPUT); |
| 34 | + // pin 12 is connected to a red LED. |
| 35 | + pinMode(12, OUTPUT); |
| 36 | + // pin 14 is connected to a push button. |
| 37 | + pinMode(14, INPUT); |
| 38 | + // pin 13 is connected to a fan. |
| 39 | + pinMode(13, OUTPUT); |
| 40 | + |
| 41 | + // connect to wifi. |
| 42 | + WiFi.begin("SSID", "PASSWORD"); |
| 43 | + Serial.print("connecting"); |
| 44 | + while (WiFi.status() != WL_CONNECTED) { |
| 45 | + Serial.print("."); |
| 46 | + delay(500); |
| 47 | + } |
| 48 | + Serial.println(); |
| 49 | + Serial.print("connected: "); |
| 50 | + Serial.println(WiFi.localIP()); |
| 51 | + |
| 52 | + Firebase.begin("example.firebaseio.com", "secret_or_token"); |
| 53 | +} |
| 54 | + |
| 55 | +int button = 0; |
| 56 | +float light = 0.0; |
| 57 | + |
| 58 | +void loop() { |
| 59 | + digitalWrite(12, (int)Firebase.get("redlight")); |
| 60 | + digitalWrite(13, (int)Firebase.get("cooldown")); |
| 61 | + digitalWrite(5, (int)Firebase.get("brrr")); |
| 62 | + int newButton = digitalRead(14); |
| 63 | + if (newButton != button) { |
| 64 | + button = newButton; |
| 65 | + Firebase.set("pushbutton", button); |
| 66 | + } |
| 67 | + float newLight = analogRead(A0); |
| 68 | + if (abs(newLight - light) > 100) { |
| 69 | + light = newLight; |
| 70 | + Firebase.set("sunlight", light); |
| 71 | + } |
| 72 | +} |
0 commit comments