├── .github └── FUNDING.yml ├── smarthome-control ├── firebase.json ├── database.rules.json └── functions │ ├── .eslintrc.json │ ├── package.json │ ├── devices.json │ └── index.js ├── .gitignore ├── CONTRIBUTING.md ├── NodeMCU Files ├── NodeMCU-Wemos-Switch │ └── NodeMCU-Wemos-Switch.ino ├── NodeMCU-Wemos-White-LEDs │ └── NodeMCU-Wemos-White-LEDs.ino ├── NodeMCU-Thermal-Controller-DHT │ └── NodeMCU-Thermal-Controller-DHT.ino └── NodeMCU-Wemos-RGB-LEDs │ └── NodeMCU-Wemos-RGB-LEDs.ino ├── README.md └── LICENSE /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [shivasiddharth] 4 | -------------------------------------------------------------------------------- /smarthome-control/firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "database": { 3 | "rules": "database.rules.json" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /smarthome-control/database.rules.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | ".read": "true", 4 | ".write": "true" 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/functions/node_modules 2 | **/functions/package-lock.json 3 | **/firebase-debug.log 4 | **/.firebaserc 5 | **/.firebase 6 | 7 | **/functions/key.json 8 | -------------------------------------------------------------------------------- /smarthome-control/functions/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "ecmaVersion": 7, 4 | "sourceType": "script" 5 | }, 6 | "extends": ["google"], 7 | "rules": { 8 | "no-unused-vars": "warn" 9 | }, 10 | "env": { 11 | "node": true 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /smarthome-control/functions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smarthome-control", 3 | "description": "Codes for controlling devices through the Google Assistant By Sid", 4 | "engines": { 5 | "node": "10" 6 | }, 7 | "scripts": { 8 | "lint": "eslint ." 9 | }, 10 | "dependencies": { 11 | "actions-on-google": "^2.12.0", 12 | "cors": "^2.8.5", 13 | "firebase-admin": "^8.0.0", 14 | "firebase-functions": "^3.8.0", 15 | "googleapis": "^43.0.0" 16 | }, 17 | "private": true, 18 | "devDependencies": { 19 | "eslint": "^6.3.0", 20 | "eslint-config-google": "^0.14.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement 9 | (CLA). 10 | 11 | * If you are an individual writing original source code and you're sure you 12 | own the intellectual property, then you'll need to sign an [individual CLA](https://developers.google.com/open-source/cla/individual). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA](https://developers.google.com/open-source/cla/corporate). 15 | 16 | Follow either of the two links above to access the appropriate CLA and 17 | instructions for how to sign and return it. Once we receive it, we'll be able to 18 | accept your pull requests. 19 | 20 | ## Contributing A Patch 21 | 22 | 1. Submit an issue describing your proposed change to the repo in question. 23 | 1. The repo owner will respond to your issue promptly. 24 | 1. If your proposed change is accepted, and you haven't already done so, sign a 25 | Contributor License Agreement (see details above). 26 | 1. Fork the desired repo, develop and test your code changes. 27 | 1. Ensure that your code adheres to the existing style in the sample to which 28 | you are contributing. Refer to the 29 | [Google Cloud Platform Samples Style Guide](https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the 30 | recommended coding standards for this organization. 31 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 32 | 1. Submit a pull request. -------------------------------------------------------------------------------- /NodeMCU Files/NodeMCU-Wemos-Switch/NodeMCU-Wemos-Switch.ino: -------------------------------------------------------------------------------- 1 | //************************Written by Sid for Sid's E Classroom***************************** 2 | //**********************https://www.youtube.com/c/SidsEClassroom*************************** 3 | //*************************https://github.com/shivasiddharth******************************* 4 | //*************************Do not modify or remove this part******************************* 5 | //************If you are modifying or re-using the code, credit the the author************* 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | // On a Trinket or Gemma we suggest changing this to 1: 15 | //Pin is set for Wemos. If using any other board, change it accordingly. 16 | #define SWITCH_PIN D4 17 | 18 | //EEPROM address definition 19 | #define Start_Address 0 20 | #define OnOff_Address Start_Address + sizeof(int) 21 | 22 | //Firebase Database URL and KEY 23 | #define FIREBASE_DATABASE_URL "ENTER YOUR FIREBASE DATABASE URL HERE" 24 | #define FIREBASE_KEY "ENTER YOUR FIREBASE KEY HERE" 25 | 26 | //Set the ID to the device id used in the index.json file 27 | static const String STRMDEVID = "6"; 28 | 29 | //Variables for OnOffvalue 30 | bool OnOffvalue; 31 | 32 | void setup() { 33 | 34 | pinMode(SWITCH_PIN, OUTPUT); 35 | Serial.begin(115200); 36 | EEPROM.begin(256); 37 | EEPROM.get(OnOff_Address, OnOffvalue); 38 | digitalWrite(SWITCH_PIN, OnOffvalue); 39 | 40 | //WiFi Manager 41 | WiFiManager wifiManager; 42 | wifiManager.autoConnect(); 43 | Serial.println("Connected.."); 44 | 45 | //Firebase Declaration 46 | Firebase.begin(FIREBASE_DATABASE_URL, FIREBASE_KEY); 47 | Firebase.stream(STRMDEVID); 48 | } 49 | 50 | 51 | void loop() { 52 | 53 | //Check Firebase connection 54 | if (Firebase.failed()) { 55 | Serial.println("Streaming Error"); 56 | Serial.println(Firebase.error()); 57 | } 58 | if (Firebase.available()) { 59 | FirebaseObject event = Firebase.readEvent(); 60 | String eventType = event.getString("type"); 61 | eventType.toLowerCase(); 62 | Serial.println(eventType); 63 | String path = event.getString("path"); 64 | Serial.println(path); 65 | if (eventType == "patch" || eventType == "put") { 66 | if (path == "/OnOff") { 67 | bool OnOffvalue = Firebase.getBool("/" + STRMDEVID + "/OnOff/on"); 68 | Serial.println(OnOffvalue); 69 | digitalWrite(SWITCH_PIN, OnOffvalue); 70 | EEPROM.put(OnOff_Address, OnOffvalue); 71 | EEPROM.commit(); 72 | EEPROM.end(); 73 | } 74 | } 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /NodeMCU Files/NodeMCU-Wemos-White-LEDs/NodeMCU-Wemos-White-LEDs.ino: -------------------------------------------------------------------------------- 1 | //************************Written by Sid for Sid's E Classroom***************************** 2 | //**********************https://www.youtube.com/c/SidsEClassroom*************************** 3 | //*************************https://github.com/shivasiddharth******************************* 4 | //*************************Do not modify or remove this part******************************* 5 | //************If you are modifying or re-using the code, credit the the author************* 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // On a Trinket or Gemma we suggest changing this to 1: 16 | //Pin is set for Wemos. If using any other board, change it accordingly. 17 | #define LED_PIN D6 18 | 19 | // How many NeoPixels are attached to the Arduino? 20 | #define LED_COUNT 12 21 | 22 | // Declare NeoPixel strip object: 23 | Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); 24 | // Argument 1 = Number of pixels in NeoPixel strip 25 | // Argument 2 = Arduino pin number (most are valid) 26 | // Argument 3 = Pixel type flags, add together as needed: 27 | // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) 28 | // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) 29 | // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) 30 | // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) 31 | // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) 32 | 33 | //EEPROM address definition 34 | #define Start_Address 0 35 | #define Bri_Address Start_Address + sizeof(int) 36 | 37 | //Firebase Database URL and KEY 38 | #define FIREBASE_DATABASE_URL "ENTER YOUR FIREBASE DATABASE URL HERE" 39 | #define FIREBASE_KEY "ENTER YOUR FIREBASE KEY HERE" 40 | 41 | //Set the ID to the device id used in the index.json file 42 | static const String STRMDEVID = "2"; 43 | 44 | //Variables for brightness and color 45 | int bri; 46 | 47 | void setup() { 48 | 49 | Serial.begin(115200); 50 | EEPROM.begin(256); 51 | EEPROM.get(Bri_Address, bri); 52 | strip.begin(); 53 | strip.fill(0xFFFFFF); 54 | strip.setBrightness(bri); 55 | strip.show(); 56 | Serial.println("Current value: "+String(bri)); 57 | 58 | //WiFi Manager 59 | WiFiManager wifiManager; 60 | wifiManager.autoConnect(); 61 | Serial.println("Connected.."); 62 | 63 | //Firebase Declaration 64 | Firebase.begin(FIREBASE_DATABASE_URL, FIREBASE_KEY); 65 | Firebase.stream(STRMDEVID); 66 | } 67 | 68 | 69 | void loop() { 70 | 71 | //Check Firebase connection 72 | if (Firebase.failed()) { 73 | Serial.println("Streaming Error"); 74 | Serial.println(Firebase.error()); 75 | } 76 | if (Firebase.available()) { 77 | FirebaseObject event = Firebase.readEvent(); 78 | String eventType = event.getString("type"); 79 | eventType.toLowerCase(); 80 | Serial.println(eventType); 81 | String path = event.getString("path"); 82 | Serial.println(path); 83 | if (eventType == "patch" || eventType == "put") { 84 | if (path == "/Brightness") { 85 | int bright = Firebase.getInt("/" + STRMDEVID + "/Brightness/brightness"); 86 | strip.setBrightness(bright); 87 | strip.show(); 88 | bri = bright; 89 | EEPROM.put(Bri_Address, bri); 90 | EEPROM.commit(); 91 | EEPROM.end(); 92 | Serial.println(bri); 93 | } 94 | else if (path == "/OnOff") { 95 | bool lightstatus = Firebase.getBool("/" + STRMDEVID + "/OnOff/on"); 96 | if (lightstatus == 0) { 97 | strip.fill(); 98 | strip.show(); 99 | } else if (lightstatus == 1) { 100 | EEPROM.get(Bri_Address, bri); 101 | strip.fill(0xFFFFFF); 102 | strip.setBrightness(bri); 103 | strip.show(); 104 | } 105 | Serial.println(lightstatus); 106 | } 107 | } 108 | } 109 | } 110 | 111 | -------------------------------------------------------------------------------- /NodeMCU Files/NodeMCU-Thermal-Controller-DHT/NodeMCU-Thermal-Controller-DHT.ino: -------------------------------------------------------------------------------- 1 | //************************Written by Sid for Sid's E Classroom***************************** 2 | //**********************https://www.youtube.com/c/SidsEClassroom*************************** 3 | //*************************https://github.com/shivasiddharth******************************* 4 | //*************************Do not modify or remove this part******************************* 5 | //************If you are modifying or re-using the code, credit the the author************* 6 | //**********************This sketch has been adapted from Aandroide************************ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // On a Trinket or Gemma we suggest changing this to 1: 16 | //Pin is set for Wemos d1 mini. If using any other board, change it accordingly. 17 | #define RELAY_PIN D6 18 | #define DHTPIN 2 //connected to pin D4 which already has a pull up resistance 19 | #define PWRSENSOR D5 //pin to be connected to the + of the sensor temperature 20 | 21 | // Uncomment the type of sensor in use: 22 | //#define DHTTYPE DHT11 // DHT 11 23 | #define DHTTYPE DHT22 // DHT 22 (AM2302) 24 | //#define DHTTYPE DHT21 // DHT 21 (AM2301) 25 | 26 | DHT dht(DHTPIN, DHTTYPE); 27 | 28 | //Firebase Database URL and KEY 29 | #define FIREBASE_DATABASE_URL "ENTER YOUR FIREBASE DATABASE URL HERE" 30 | #define FIREBASE_KEY "ENTER YOUR FIREBASE KEY HERE" 31 | 32 | //Set the ID to the device id used in the index.json file 33 | static const String STRMDEVID = "4"; 34 | 35 | //Variables 36 | float temperature; 37 | float humidity; 38 | unsigned long previousMillis = 0; 39 | const long interval = 10000; // interval to refresh temp from sensor (10 seconds) 40 | 41 | void setup() { 42 | 43 | Serial.begin(115200); 44 | dht.begin(); 45 | pinMode(RELAY_PIN, OUTPUT); 46 | digitalWrite(RELAY_PIN, LOW); 47 | pinMode(PWRSENSOR, OUTPUT); 48 | digitalWrite(PWRSENSOR, HIGH); 49 | 50 | //WiFi Manager 51 | WiFiManager wifiManager; 52 | wifiManager.autoConnect(); 53 | Serial.println("Connected.."); 54 | 55 | //Firebase Declaration 56 | Firebase.begin(FIREBASE_DATABASE_URL, FIREBASE_KEY); 57 | Firebase.stream(STRMDEVID); 58 | } 59 | 60 | void loop() { 61 | 62 | unsigned long currentMillis = millis(); 63 | if (currentMillis - previousMillis >= interval) { 64 | // save the last time you updated the temp values 65 | previousMillis = currentMillis; 66 | 67 | // Read temperature as Celsius (the default) 68 | float newT = dht.readTemperature(); 69 | if (isnan(newT)) { 70 | Serial.println("Failed to read from DHT sensor!"); 71 | } 72 | else { 73 | temperature = newT - 2.00; 74 | Serial.print("temperature detected: "); 75 | Serial.print(temperature); 76 | Serial.println(" C°"); 77 | Firebase.setFloat("/" + STRMDEVID + "/TemperatureSetting/thermostatTemperatureAmbient", temperature); 78 | } 79 | 80 | // Read Humidity 81 | float newH = dht.readHumidity(); 82 | // if humidity read failed, don't change h value 83 | if (isnan(newH)) { 84 | Serial.println("Failed to read from DHT sensor!"); 85 | } 86 | else { 87 | humidity = newH ; 88 | Serial.print("humidity: "); 89 | Serial.print(humidity); 90 | Serial.println(" %"); 91 | Firebase.setFloat("/" + STRMDEVID + "/TemperatureSetting/thermostatHumidityAmbient", humidity); 92 | } 93 | } 94 | 95 | //Check Firebase connection 96 | if (Firebase.failed()) { 97 | Serial.println("Streaming Error"); 98 | Serial.println(Firebase.error()); 99 | } 100 | if (Firebase.available()) { 101 | FirebaseObject event = Firebase.readEvent(); 102 | String eventType = event.getString("type"); 103 | eventType.toLowerCase(); 104 | Serial.println(eventType); 105 | String path = event.getString("path"); 106 | Serial.println(path); 107 | if (eventType == "patch" || eventType == "put" ) { 108 | if (path == "/thermostatTemperatureSetpoint/", "") { 109 | float temperature_set = Firebase.getFloat("/" + STRMDEVID + "/TemperatureSetting/thermostatTemperatureSetpoint/"); 110 | Serial.print("temp set to: "); 111 | Serial.print(temperature_set); 112 | Serial.println(" C°"); 113 | 114 | if (temperature < temperature_set) { 115 | digitalWrite(RELAY_PIN, HIGH); 116 | } 117 | if (temperature > temperature_set) { 118 | digitalWrite(RELAY_PIN, LOW); 119 | } 120 | } 121 | 122 | if (path == "/thermostatMode/", "") { 123 | String set_mode = (Firebase.getString("/" + STRMDEVID + "/TemperatureSetting/thermostatMode")); 124 | Serial.print("termostat mode: "); 125 | Serial.println(set_mode); 126 | } 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Create your own smart devices 2 | 3 | This project contains the code which demonstrates the integration of smart home devices with the Google Assistant 4 | using a Firebase backend. 5 | 6 | ## How to 7 | 8 | Refer to the video below for step by step guidelines. 9 | 10 | Detailed Youtube Video 13 | 14 | 15 | 1. Go to the Actions on Google Developer Console. 16 | 2. Click New Project, enter a name for the project, and click CREATE PROJECT. 17 | 3. Select the Smart Home App. 18 | 4. On the Overview screen in the Actions console, select Smart home. 19 | 5. Choose the Smart home experience card, and you will then be directed to your project console. 20 | 6. Download and install Node.js from https://nodejs.org/en/download/. 21 | 7. To install the firebase CLI, run the following npm command from the terminal: 22 | ``` 23 | sudo npm install -g firebase-tools 24 | ``` 25 | 8. To verify that the CLI has been installed correctly, run: 26 | ``` 27 | firebase --version 28 | ``` 29 | 9. Authorize the Firebase CLI with your Google account by running: 30 | ``` 31 | firebase login 32 | ``` 33 | 10. Install git and clone the project using: 34 | ``` 35 | sudo apt-get install git 36 | git clone https://github.com/shivasiddharth/google-actions-smarthome 37 | ``` 38 | 11. Change directory using: 39 | ``` 40 | cd /google-actions-smarthome/smarthome-control/functions/ 41 | ``` 42 | 12. Navigate to the Google Cloud Console API Manager for your project id. 43 | 13. Enable the HomeGraph API. 44 | 14. Navigate to the Google Cloud Console API & Services page. 45 | 15. Select Create Credentials and create a Service account key. 46 | 16. Create a new Service account. 47 | 17. Use the role Service Account > Service Account Token Creator. 48 | 18. Create the account and download a JSON file. Save this in the functions folder as smart-home-key.json 49 | 19. Connect to firebase using: 50 | ``` 51 | firebase use 52 | ``` 53 | 20. Deploy firebase using: 54 | ``` 55 | sudo npm install 56 | firebase init database 57 | firebase deploy 58 | ``` 59 | 21. In the Actions console under Overview > Build your Action, select Add Action(s). Enter the URL for your cloud function that provides fulfillment for the smart home intents and click Save. 60 | ``` 61 | https://us-central1-.cloudfunctions.net/smarthome 62 | ``` 63 | 22. On the Develop > Invocation tab, add a Display Name for your Action, and click Save. This name will appear in the Google Home app. 64 | 23. To enable Account linking, select the Develop > Account linking option in the left navigation. Use these account linking settings: 65 | ``` 66 | Client ID : ABC123 67 | Client secret : DEF456 68 | Authorization URL : https://us-central1-.cloudfunctions.net/fakeauth 69 | Token URL : https://us-central1-.cloudfunctions.net/faketoken 70 | ``` 71 | 24. Click Save to save your account linking configuration, then click Test to enable testing on your project. 72 | 25. To link to Google Assistant: 73 | 1. On your phone, open the Google Assistant settings. Note that you should be logged in as the same account as in the console. 74 | 2. Navigate to Google Assistant > Settings > Home Control (under Assistant). 75 | 3. Select the plus (+) icon in the bottom right corner. 76 | 4. You should see your test app with the [test] prefix and the display name you set. 77 | 5. Select that item. The Google Assistant will then authenticate with your service and send a SYNC request, asking your service to provide a list of devices for the user. 78 | 26. Now pre-programmed devices will appear in the Google Home app. 79 | 80 | ## Pre-programmed smart devices 81 | 82 | The codes in this project will create the following devices: 83 | 1. 1 RGB light with brightness, color and on/off control. 84 | 2. 1 Normal light with brightness and on/off control. 85 | 3. 1 Fan with speed and on/off control (speed control trait is currently disabled by google hence the corresponding segments of codes have been commented out). 86 | 4. 1 Thermostat with mode change and temperature control. 87 | 5. 1 Temperature sensor device with temperature readout only. 88 | 6. 1 Plug/Switch. 89 | 90 | ## Adding or modifying the devices 91 | 92 | The devices can be added or modified from the **devices.json** within the functions folder. Use https://jsonlint.com to validate your device list in the JSON file. 93 | 94 | ## Note 95 | 96 | 1. Use the Arduino Json library version 5.13.0 97 | 2. Use Firebase Arduino library from here: https://github.com/FirebaseExtended/firebase-arduino.git 98 | 3. Default **USER_ID** is set to **123** in the index.js file. Please change it to something unique to make use of Homegraph based reporting. 99 | 4. Use esp8266 core version 2.5.2 100 | 5. You need to setup a billing account for utilizing firebase services more details can be found [here](https://firebase.google.com/support/faq#expandable-9). 101 | 6. Though you setup a billing account, you have free usage limits. So, if your usage is within the limits, you will not be billed. More details can be found [here](https://firebase.google.com/support/faq#expandable-10). 102 | 103 | 104 | ## License 105 | Copyright 2020 Google Inc. 106 | 107 | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at 108 | 109 | http://www.apache.org/licenses/LICENSE-2.0 110 | 111 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 112 | -------------------------------------------------------------------------------- /smarthome-control/functions/devices.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "1", 4 | "type": "action.devices.types.LIGHT", 5 | "traits": [ 6 | "action.devices.traits.OnOff", 7 | "action.devices.traits.Brightness", 8 | "action.devices.traits.ColorSetting" 9 | ], 10 | "name": { 11 | "defaultNames": [ 12 | "My Test Light1" 13 | ], 14 | "name": "Light 1", 15 | "nicknames": [ 16 | "Patio Light" 17 | ] 18 | }, 19 | "deviceInfo": { 20 | "manufacturer": "Siddhy Co", 21 | "model": "Siddhys RGB Bulb", 22 | "hwVersion": "1.0", 23 | "swVersion": "1.0.1" 24 | }, 25 | "willReportState": true, 26 | "attributes": { 27 | "commandOnlyOnOff": false, 28 | "commandOnlyColorSetting": false, 29 | "commandOnlyBrightness": false, 30 | "colorModel": "rgb", 31 | "colorTemperatureRange": { 32 | "temperatureMinK": 2000, 33 | "temperatureMaxK": 6500 34 | } 35 | } 36 | }, 37 | { 38 | "id": "2", 39 | "type": "action.devices.types.LIGHT", 40 | "traits": [ 41 | "action.devices.traits.OnOff", 42 | "action.devices.traits.Brightness" 43 | ], 44 | "name": { 45 | "defaultNames": [ 46 | "My Test Light2" 47 | ], 48 | "name": "Light 2", 49 | "nicknames": [ 50 | "Corridor Light" 51 | ] 52 | }, 53 | "deviceInfo": { 54 | "manufacturer": "Siddhy Co", 55 | "model": "Siddhys LED Bulb", 56 | "hwVersion": "2.0", 57 | "swVersion": "2.0.1" 58 | }, 59 | "willReportState": true, 60 | "attributes": { 61 | "commandOnlyOnOff": false, 62 | "commandOnlyBrightness": false 63 | } 64 | }, 65 | { 66 | "id": "3", 67 | "type": "action.devices.types.FAN", 68 | "traits": [ 69 | "action.devices.traits.FanSpeed", 70 | "action.devices.traits.OnOff" 71 | ], 72 | "name": { 73 | "defaultNames": [ 74 | "My Fan" 75 | ], 76 | "name": "Fan1", 77 | "nicknames": [ 78 | "Ceiling Fan" 79 | ] 80 | }, 81 | "deviceInfo": { 82 | "manufacturer": "Siddhy Co", 83 | "model": "Siddhys High Effeciency Fan", 84 | "hwVersion": "3.0", 85 | "swVersion": "4.0.1" 86 | }, 87 | "willReportState": true, 88 | "attributes": { 89 | "commandOnlyOnOff": false, 90 | "commandOnlyFanSpeed": false, 91 | "availableFanSpeeds": { 92 | "speeds": [ 93 | {"speed_name": "Low", "speed_values": [{"speed_synonym": ["low", "low speed", "slow"], "lang": "en"}]}, 94 | {"speed_name": "Medium", "speed_values": [{"speed_synonym": ["medium", "medium speed", "med"], "lang": "en"}]} 95 | ], 96 | "ordered": true 97 | } 98 | } 99 | }, 100 | { 101 | "id": "4", 102 | "type": "action.devices.types.THERMOSTAT", 103 | "traits": [ 104 | "action.devices.traits.TemperatureSetting" 105 | ], 106 | "name": { 107 | "defaultNames": [ 108 | "Temp Control" 109 | ], 110 | "name": "Temperature Controller ", 111 | "nicknames": [ 112 | "Thermal Controller" 113 | ] 114 | }, 115 | "deviceInfo": { 116 | "manufacturer": "Siddhy Co", 117 | "model": "Siddhys Thermostat", 118 | "hwVersion": "4.0", 119 | "swVersion": "5.0.1" 120 | }, 121 | "willReportState": true, 122 | "attributes": { 123 | "availableThermostatModes": "off,on,heat,cool,heatcool", 124 | "queryOnlyTemperatureSetting": false, 125 | "thermostatTemperatureUnit": "C", 126 | "commandOnlyTemperatureSetting": false 127 | } 128 | }, 129 | { 130 | "id": "5", 131 | "type": "action.devices.types.THERMOSTAT", 132 | "traits": [ 133 | "action.devices.traits.TemperatureSetting" 134 | ], 135 | "name": { 136 | "defaultNames": [ 137 | "Temp Sensor" 138 | ], 139 | "name": "Room Temperature Sensor", 140 | "nicknames": [ 141 | "Ambient Temperature Sensor" 142 | ] 143 | }, 144 | "deviceInfo": { 145 | "manufacturer": "Siddhy Co", 146 | "model": "Siddhys Temperature Sensor", 147 | "hwVersion": "5.0", 148 | "swVersion": "6.0.1" 149 | }, 150 | "willReportState": true, 151 | "attributes": { 152 | "queryOnlyTemperatureSetting": true, 153 | "thermostatTemperatureUnit": "C" 154 | } 155 | }, 156 | { 157 | "id": "6", 158 | "type": "action.devices.types.SWITCH", 159 | "traits": [ 160 | "action.devices.traits.OnOff" 161 | ], 162 | "name": { 163 | "defaultNames": [ 164 | "Plug Socket" 165 | ], 166 | "name": "Smart Switch", 167 | "nicknames": [ 168 | "New Switch" 169 | ] 170 | }, 171 | "deviceInfo": { 172 | "manufacturer": "Siddhy Co", 173 | "model": "Siddhys On/Off Switch", 174 | "hwVersion": "6.0", 175 | "swVersion": "7.0.1" 176 | }, 177 | "willReportState": true, 178 | "attributes": { 179 | "commandOnlyOnOff": false 180 | } 181 | }, 182 | { 183 | "id": "7", 184 | "type": "action.devices.types.LIGHT", 185 | "traits": [ 186 | "action.devices.traits.OnOff", 187 | "action.devices.traits.Brightness", 188 | "action.devices.traits.ColorSetting", 189 | "action.devices.traits.Modes" 190 | ], 191 | "name": { 192 | "defaultNames": [ 193 | "My Test Light3" 194 | ], 195 | "name": "Light 3", 196 | "nicknames": [ 197 | "Mood light" 198 | ] 199 | }, 200 | "deviceInfo": { 201 | "manufacturer": "Siddhy Co", 202 | "model": "Siddhys RGB Bulb Mode", 203 | "hwVersion": "2.0", 204 | "swVersion": "2.0.0" 205 | }, 206 | "willReportState": true, 207 | "attributes": { 208 | "availableModes": [{ 209 | "name": "Mood", 210 | "name_values": [{"name_synonym": ["mood", "feeling"], "lang": "en" }], 211 | "settings": [ 212 | {"setting_name": "Blank", "setting_values": [{"setting_synonym": ["null", "nothing"], "lang": "en"}] 213 | }, 214 | {"setting_name": "Happy", "setting_values": [{"setting_synonym": ["happy", "cheerful"], "lang": "en"}] 215 | }, 216 | {"setting_name": "Sad", "setting_values": [{"setting_synonym": ["sad", "unhappy"], "lang": "en"}] 217 | }, 218 | {"setting_name": "Angry", "setting_values": [{"setting_synonym": ["angry", "annoyed"], "lang": "en"}] 219 | }, 220 | {"setting_name": "Party", "setting_values": [{"setting_synonym": ["party", "dance"], "lang": "en"}] 221 | }, 222 | {"setting_name": "Anxious", "setting_values": [{"setting_synonym": ["anxious mood", "anxiety"], "lang": "en"}] 223 | }, 224 | {"setting_name": "Busy", "setting_values": [{"setting_synonym": ["busy mood", "busy", "at work"], "lang": "en"}] 225 | } 226 | ], 227 | "ordered": true 228 | }], 229 | "commandOnlyOnOff": false, 230 | "commandOnlyBrightness": false, 231 | "commandOnlyModes": false, 232 | "colorModel": "rgb", 233 | "colorTemperatureRange": { 234 | "temperatureMinK": 2000, 235 | "temperatureMaxK": 6500 236 | } 237 | } 238 | } 239 | ] 240 | -------------------------------------------------------------------------------- /NodeMCU Files/NodeMCU-Wemos-RGB-LEDs/NodeMCU-Wemos-RGB-LEDs.ino: -------------------------------------------------------------------------------- 1 | //************************Written by Sid for Sid's E Classroom***************************** 2 | //**********************https://www.youtube.com/c/SidsEClassroom*************************** 3 | //*************************https://github.com/shivasiddharth******************************* 4 | //*************************Do not modify or remove this part******************************* 5 | //************If you are modifying or re-using the code, credit the the author************* 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // On a Trinket or Gemma we suggest changing this to 1: 16 | //Pin is set for Wemos. If using any other board, change it accordingly. 17 | #define LED_PIN D6 18 | 19 | // How many NeoPixels are attached to the Arduino? 20 | #define LED_COUNT 12 21 | 22 | // Declare NeoPixel strip object: 23 | Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); 24 | // Argument 1 = Number of pixels in NeoPixel strip 25 | // Argument 2 = Arduino pin number (most are valid) 26 | // Argument 3 = Pixel type flags, add together as needed: 27 | // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) 28 | // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) 29 | // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) 30 | // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) 31 | // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) 32 | 33 | //EEPROM address definition 34 | #define Start_Address 0 35 | #define Bri_Address Start_Address + sizeof(int) 36 | #define Clr_Address Bri_Address + sizeof(int) 37 | 38 | //Firebase Database URL and KEY 39 | #define FIREBASE_DATABASE_URL "ENTER YOUR FIREBASE DATABASE URL HERE" 40 | #define FIREBASE_KEY "ENTER YOUR FIREBASE KEY HERE" 41 | 42 | //Set the ID to the device id used in the index.json file 43 | static const String STRMDEVID = "1"; 44 | 45 | //Temperature in K 46 | int Temperature[111] = {1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500, 6600, 6700, 6800, 6900, 7000, 7100, 7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900, 8000, 8100, 8200, 8300, 8400, 8500, 8600, 8700, 8800, 8900, 9000, 9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900, 10000, 10100, 10200, 10300, 10400, 10500, 10600, 10700, 10800, 10900, 11000, 11100, 11200, 11300, 11400, 11500, 11600, 11700, 11800, 11900, 12000}; 47 | //Correspodning Hexcolor 48 | unsigned long Hexcolor[111] = {0xFF3800, 0xFF4700, 0xFF5300, 0xFF5D00, 0xFF6500, 0xFF6D00, 0xFF7300, 0xFF7900, 0xFF7E00, 0xFF8300, 0xFF8A12, 0xFF8E21, 0xFF932C, 0xFF9836, 0xFF9D3F, 0xFFA148, 0xFFA54F, 0xFFA957, 0xFFAD5E, 0xFFB165, 0xFFB46B, 0xFFB872, 0xFFBB78, 0xFFBE7E, 0xFFC184, 0xFFC489, 0xFFC78F, 0xFFC994, 0xFFCC99, 0xFFCE9F, 0xFFD1A3, 0xFFD3A8, 0xFFD5AD, 0xFFD7B1, 0xFFD9B6, 0xFFDBBA, 0xFFDDBE, 0xFFDFC2, 0xFFE1C6, 0xFFE3CA, 0xFFE4CE, 0xFFE6D2, 0xFFE8D5, 0xFFE9D9, 0xFFEBDC, 0xFFECE0, 0xFFEEE3, 0xFFEFE6, 0xFFF0E9, 0xFFF2EC, 0xFFF3EF, 0xFFF4F2, 0xFFF5F5, 0xFFF6F7, 0xFFF8FB, 0xFFF9FD, 0xFEF9FF, 0xFCF7FF, 0xF9F6FF, 0xF7F5FF, 0xF5F3FF, 0xF3F2FF, 0xF0F1FF, 0xEFF0FF, 0xEDEFFF, 0xEBEEFF, 0xE9EDFF, 0xE7ECFF, 0xE6EBFF, 0xE4EAFF, 0xE3E9FF, 0xE1E8FF, 0xE0E7FF, 0xDEE6FF, 0xDDE6FF, 0xDCE5FF, 0xDAE5FF, 0xD9E3FF, 0xD8E3FF, 0xD7E2FF, 0xD6E1FF, 0xD4E1FF, 0xD3E0FF, 0xD2DFFF, 0xD1DFFF, 0xD0DEFF, 0xCFDDFF, 0xCFDDFF, 0xCEDCFF, 0xCDDCFF, 0xCFDAFF, 0xCFDAFF, 0xCED9FF, 0xCDD9FF, 0xCCD8F, 0xCCD8FF, 0xCBD7F, 0xCAD7F, 0xCAD6FF, 0xC9D6FF, 0xC8D5FF, 0xC7D4FF, 0xC6D4FF, 0xC6D4FF, 0xC5D3FF, 0xC5D3FF, 0xC5D2FF, 0xC4D2FF, 0xC3D2FF, 0xC3D1FF}; 49 | 50 | //Variables for brightness and color 51 | int bri; 52 | int clr; 53 | 54 | void setup() { 55 | 56 | Serial.begin(115200); 57 | EEPROM.begin(256); 58 | EEPROM.get(Bri_Address, bri); 59 | EEPROM.get(Clr_Address, clr); 60 | //Neopixel Initialization 61 | strip.begin(); 62 | strip.fill(clr); 63 | strip.setBrightness(bri); 64 | strip.show(); 65 | Serial.println("Current values: "+String(bri)+","+String(clr)); 66 | 67 | //WiFi Manager 68 | WiFiManager wifiManager; 69 | wifiManager.autoConnect(); 70 | Serial.println("Connected.."); 71 | 72 | //Firebase Declaration 73 | Firebase.begin(FIREBASE_DATABASE_URL, FIREBASE_KEY); 74 | Firebase.stream(STRMDEVID); 75 | } 76 | 77 | 78 | void loop() { 79 | 80 | //Check Firebase connection 81 | if (Firebase.failed()) { 82 | Serial.println("Streaming Error"); 83 | Serial.println(Firebase.error()); 84 | } 85 | 86 | if (Firebase.available()) { 87 | FirebaseObject event = Firebase.readEvent(); 88 | String eventType = event.getString("type"); 89 | eventType.toLowerCase(); 90 | Serial.println(eventType); 91 | String path = event.getString("path"); 92 | Serial.println(path); 93 | 94 | if (eventType == "patch" || eventType == "put") { 95 | if (path == "/Brightness") { 96 | int bright = Firebase.getInt("/" + STRMDEVID + "/Brightness/brightness"); 97 | strip.setBrightness(bright); 98 | strip.show(); 99 | bri = bright; 100 | EEPROM.put(Bri_Address, bri); 101 | EEPROM.commit(); 102 | EEPROM.end(); 103 | Serial.println(bri); 104 | } 105 | else if (path == "/OnOff") { 106 | bool lightstatus = Firebase.getBool("/" + STRMDEVID + "/OnOff/on"); 107 | if (lightstatus == 0) { 108 | strip.fill(); 109 | strip.show(); 110 | } else if (lightstatus == 1) { 111 | EEPROM.get(Clr_Address, clr); 112 | EEPROM.get(Bri_Address, bri); 113 | strip.fill(clr); 114 | strip.setBrightness(bri); 115 | strip.show(); 116 | } 117 | Serial.println(lightstatus); 118 | } 119 | else if (path == "/ColorSetting") { 120 | String colorname = Firebase.getString("/" + STRMDEVID + "/1/ColorSetting/color/name"); 121 | Serial.println(colorname); 122 | String colors = event.getJsonVariant("data"); 123 | if (colors.indexOf("temperature") != -1) { 124 | int colortemp = Firebase.getInt("/" + STRMDEVID + "/1/ColorSetting/color/temperature"); 125 | for (int i = 1; i <= 111; i++) { 126 | if (colortemp == Temperature[i]) { 127 | int colorhex = Hexcolor[i]; 128 | clr = colorhex; 129 | strip.fill(clr); 130 | strip.show(); 131 | EEPROM.put(Clr_Address, clr); 132 | EEPROM.commit(); 133 | EEPROM.end(); 134 | break; 135 | } 136 | } 137 | Serial.println(clr); 138 | } else if (colors.indexOf("spectrumRGB") != -1) { 139 | int colordec = Firebase.getInt("/" + STRMDEVID + "/ColorSetting/color/spectrumRGB"); 140 | clr = colordec; 141 | strip.fill(clr); 142 | strip.show(); 143 | EEPROM.put(Clr_Address, clr); 144 | EEPROM.commit(); 145 | EEPROM.end(); 146 | Serial.println(clr); 147 | } 148 | } 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "[]" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright [yyyy] [name of copyright owner] 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. -------------------------------------------------------------------------------- /smarthome-control/functions/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 Google Inc. All Rights Reserved. 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 | 'use strict'; 18 | 19 | const functions = require('firebase-functions'); 20 | const {smarthome} = require('actions-on-google'); 21 | const {google} = require('googleapis'); 22 | const util = require('util'); 23 | const admin = require('firebase-admin'); 24 | // Initialize Firebase 25 | admin.initializeApp(); 26 | const firebaseRef = admin.database().ref('/'); 27 | 28 | // Hardcoded user ID 29 | const USER_ID = '123'; 30 | 31 | exports.login = functions.https.onRequest((request, response) => { 32 | if (request.method === 'GET') { 33 | functions.logger.log('Requesting login page'); 34 | response.send(` 35 | 36 | 37 | 38 |
39 | 41 | 44 |
45 | 46 | 47 | `); 48 | } else if (request.method === 'POST') { 49 | // Here, you should validate the user account. 50 | // In this sample, we do not do that. 51 | const responseurl = decodeURIComponent(request.body.responseurl); 52 | functions.logger.log(`Redirect to ${responseurl}`); 53 | return response.redirect(responseurl); 54 | } else { 55 | // Unsupported method 56 | response.send(405, 'Method Not Allowed'); 57 | } 58 | }); 59 | 60 | 61 | exports.fakeauth = functions.https.onRequest((request, response) => { 62 | const responseurl = util.format('%s?code=%s&state=%s', 63 | decodeURIComponent(request.query.redirect_uri), 'xxxxxx', 64 | request.query.state); 65 | functions.logger.log(`Set redirect as ${responseurl}`); 66 | return response.redirect( 67 | `/login?responseurl=${encodeURIComponent(responseurl)}`); 68 | }); 69 | 70 | exports.faketoken = functions.https.onRequest((request, response) => { 71 | const grantType = request.query.grant_type ? 72 | request.query.grant_type : request.body.grant_type; 73 | const secondsInDay = 86400; // 60 * 60 * 24 74 | const HTTP_STATUS_OK = 200; 75 | functions.logger.log(`Grant type ${grantType}`); 76 | 77 | let obj; 78 | if (grantType === 'authorization_code') { 79 | obj = { 80 | token_type: 'bearer', 81 | access_token: '123access', 82 | refresh_token: '123refresh', 83 | expires_in: secondsInDay, 84 | }; 85 | } else if (grantType === 'refresh_token') { 86 | obj = { 87 | token_type: 'bearer', 88 | access_token: '123access', 89 | expires_in: secondsInDay, 90 | }; 91 | } 92 | response.status(HTTP_STATUS_OK) 93 | .json(obj); 94 | }); 95 | 96 | let jwt 97 | try { 98 | jwt = require('./smart-home-key.json') 99 | } catch (e) { 100 | functions.logger.warn('Service account key is not found') 101 | functions.logger.warn('Report state and Request sync will be unavailable') 102 | } 103 | 104 | const app = smarthome({ 105 | jwt: jwt, 106 | debug: true, 107 | }) 108 | 109 | let devicelist 110 | devicelist = require('./devices.json') 111 | const deviceitems = JSON.parse(JSON.stringify(devicelist)); 112 | 113 | var devicecounter; 114 | 115 | app.onSync((body) => { 116 | functions.logger.log('onSync'); 117 | for (devicecounter = 0; devicecounter < deviceitems.length; devicecounter++) { 118 | if (deviceitems[devicecounter].traits.includes('action.devices.traits.TemperatureSetting')) { 119 | if (deviceitems[devicecounter].attributes.queryOnlyTemperatureSetting == true) { 120 | firebaseRef.child(deviceitems[devicecounter].id).child('TemperatureSetting').set({thermostatMode: "off", thermostatTemperatureAmbient: 20, thermostatHumidityAmbient: 90}); 121 | } else if (deviceitems[devicecounter].attributes.queryOnlyTemperatureSetting == false) { 122 | firebaseRef.child(deviceitems[devicecounter].id).child('TemperatureSetting').set({thermostatMode: "off", thermostatTemperatureSetpoint: 25.5, thermostatTemperatureAmbient: 20, thermostatHumidityAmbient: 90, thermostatTemperatureSetpointLow: 15, thermostatTemperatureSetpointHigh: 30}); 123 | } 124 | } 125 | if (deviceitems[devicecounter].traits.includes('action.devices.traits.OnOff')) { 126 | firebaseRef.child(deviceitems[devicecounter].id).child('OnOff').set({on: false}); 127 | } 128 | if (deviceitems[devicecounter].traits.includes('action.devices.traits.Brightness')) { 129 | firebaseRef.child(deviceitems[devicecounter].id).child('Brightness').set({brightness: 10}); 130 | } 131 | if (deviceitems[devicecounter].traits.includes('action.devices.traits.ColorSetting')) { 132 | firebaseRef.child(deviceitems[devicecounter].id).child('ColorSetting').set({color: {name: "deep sky blue", spectrumRGB: 49151}}); 133 | } 134 | if (deviceitems[devicecounter].traits.includes('action.devices.traits.Modes')) { 135 | var modename = deviceitems[devicecounter].attributes.availableModes[0].name 136 | var modevalue = deviceitems[devicecounter].attributes.availableModes[0].settings[0].setting_name 137 | firebaseRef.child(deviceitems[devicecounter].id).child('Modes').set({currentModeSettings: {modename: modevalue}}); 138 | } 139 | if (deviceitems[devicecounter].traits.includes('action.devices.traits.FanSpeed')) { 140 | firebaseRef.child(deviceitems[devicecounter].id).child('FanSpeed').set({currentFanSpeedSetting: 20.0}); 141 | } 142 | } 143 | return { 144 | requestId: body.requestId, 145 | payload: { 146 | agentUserId: USER_ID, 147 | devices: deviceitems 148 | }, 149 | }; 150 | }); 151 | 152 | 153 | const queryFirebase = async (deviceId) => { 154 | const snapshot = await firebaseRef.child(deviceId).once('value'); 155 | const snapshotVal = snapshot.val(); 156 | 157 | var asyncvalue = {}; 158 | 159 | if (Object.prototype.hasOwnProperty.call(snapshotVal, 'OnOff')) { 160 | asyncvalue = Object.assign(asyncvalue, {on: snapshotVal.OnOff.on}); 161 | } 162 | if (Object.prototype.hasOwnProperty.call(snapshotVal, 'Brightness')) { 163 | asyncvalue = Object.assign(asyncvalue, {brightness: snapshotVal.Brightness.brightness}); 164 | } 165 | if (Object.prototype.hasOwnProperty.call(snapshotVal, 'ColorSetting')) { 166 | asyncvalue = Object.assign(asyncvalue, {color: snapshotVal.ColorSetting.color}); 167 | } 168 | if (Object.prototype.hasOwnProperty.call(snapshotVal, 'FanSpeed')) { 169 | if (Object.prototype.hasOwnProperty.call(snapshotVal.FanSpeed, 'currentFanSpeedSetting')) { 170 | asyncvalue = Object.assign(asyncvalue, {currentFanSpeedSetting: snapshotVal.FanSpeed.currentFanSpeedSetting}); 171 | } 172 | } 173 | if (Object.prototype.hasOwnProperty.call(snapshotVal, 'Modes')) { 174 | if (Object.prototype.hasOwnProperty.call(snapshotVal.Modes, 'currentModeSettings')) { 175 | asyncvalue = Object.assign(asyncvalue, {currentModeSettings: snapshotVal.Modes.currentModeSettings}); 176 | } 177 | } 178 | if (Object.prototype.hasOwnProperty.call(snapshotVal, 'TemperatureSetting')) { 179 | if (Object.prototype.hasOwnProperty.call(snapshotVal.TemperatureSetting, 'thermostatMode')) { 180 | asyncvalue = Object.assign(asyncvalue, {thermostatMode: snapshotVal.TemperatureSetting.thermostatMode}); 181 | } 182 | if (Object.prototype.hasOwnProperty.call(snapshotVal.TemperatureSetting, 'thermostatTemperatureSetpoint')) { 183 | asyncvalue = Object.assign(asyncvalue, {thermostatTemperatureSetpoint: snapshotVal.TemperatureSetting.thermostatTemperatureSetpoint}); 184 | } 185 | if (Object.prototype.hasOwnProperty.call(snapshotVal.TemperatureSetting, 'thermostatTemperatureAmbient')) { 186 | asyncvalue = Object.assign(asyncvalue, {thermostatTemperatureAmbient: snapshotVal.TemperatureSetting.thermostatTemperatureAmbient}); 187 | } 188 | if (Object.prototype.hasOwnProperty.call(snapshotVal.TemperatureSetting, 'thermostatHumidityAmbient')) { 189 | asyncvalue = Object.assign(asyncvalue, {thermostatHumidityAmbient: snapshotVal.TemperatureSetting.thermostatHumidityAmbient}); 190 | } 191 | if (Object.prototype.hasOwnProperty.call(snapshotVal.TemperatureSetting, 'thermostatTemperatureSetpointLow')) { 192 | asyncvalue = Object.assign(asyncvalue, {thermostatTemperatureSetpointLow: snapshotVal.TemperatureSetting.thermostatTemperatureSetpointLow}); 193 | } 194 | if (Object.prototype.hasOwnProperty.call(snapshotVal.TemperatureSetting, 'thermostatTemperatureSetpointHigh')) { 195 | asyncvalue = Object.assign(asyncvalue, {thermostatTemperatureSetpointHigh: snapshotVal.TemperatureSetting.thermostatTemperatureSetpointHigh}); 196 | } 197 | } 198 | return asyncvalue; 199 | }; 200 | 201 | const queryDevice = async (deviceId) => { 202 | const data = await queryFirebase(deviceId); 203 | 204 | var datavalue = {}; 205 | 206 | if (Object.prototype.hasOwnProperty.call(data, 'on')) { 207 | datavalue = Object.assign(datavalue, {on: data.on}); 208 | } 209 | if (Object.prototype.hasOwnProperty.call(data, 'brightness')) { 210 | datavalue = Object.assign(datavalue, {brightness: data.brightness}); 211 | } 212 | if (Object.prototype.hasOwnProperty.call(data, 'color')) { 213 | datavalue = Object.assign(datavalue, {color: data.color}); 214 | } 215 | if (Object.prototype.hasOwnProperty.call(data, 'currentFanSpeedSetting')) { 216 | datavalue = Object.assign(datavalue, {currentFanSpeedSetting: data.currentFanSpeedSetting}); 217 | } 218 | if (Object.prototype.hasOwnProperty.call(data, 'currentModeSettings')) { 219 | datavalue = Object.assign(datavalue, {currentModeSettings: data.currentModeSettings}); 220 | } 221 | if (Object.prototype.hasOwnProperty.call(data, 'thermostatMode')) { 222 | datavalue = Object.assign(datavalue, {thermostatMode: data.thermostatMode}); 223 | } 224 | if (Object.prototype.hasOwnProperty.call(data, 'thermostatTemperatureSetpoint')) { 225 | datavalue = Object.assign(datavalue, {thermostatTemperatureSetpoint: data.thermostatTemperatureSetpoint}); 226 | } 227 | if (Object.prototype.hasOwnProperty.call(data, 'thermostatTemperatureAmbient')) { 228 | datavalue = Object.assign(datavalue, {thermostatTemperatureAmbient: data.thermostatTemperatureAmbient}); 229 | } 230 | if (Object.prototype.hasOwnProperty.call(data, 'thermostatHumidityAmbient')) { 231 | datavalue = Object.assign(datavalue, {thermostatHumidityAmbient: data.thermostatHumidityAmbient}); 232 | } 233 | if (Object.prototype.hasOwnProperty.call(data, 'thermostatTemperatureSetpointLow')) { 234 | datavalue = Object.assign(datavalue, {thermostatTemperatureSetpointLow: data.thermostatTemperatureSetpointLow}); 235 | } 236 | if (Object.prototype.hasOwnProperty.call(data, 'thermostatTemperatureSetpointHigh')) { 237 | datavalue = Object.assign(datavalue, {thermostatTemperatureSetpointHigh: data.thermostatTemperatureSetpointHigh}); 238 | } 239 | return datavalue; 240 | }; 241 | 242 | app.onQuery(async (body) => { 243 | const {requestId} = body; 244 | const payload = { 245 | devices: {}, 246 | }; 247 | 248 | const queryPromises = []; 249 | const intent = body.inputs[0]; 250 | for (const device of intent.payload.devices) { 251 | const deviceId = device.id; 252 | queryPromises.push( 253 | queryDevice(deviceId) 254 | .then((data) => { 255 | // Add response to device payload 256 | payload.devices[deviceId] = data; 257 | }) ); 258 | } 259 | // Wait for all promises to resolve 260 | await Promise.all(queryPromises); 261 | return { 262 | requestId: requestId, 263 | payload: payload, 264 | }; 265 | }); 266 | 267 | const updateDevice = async (execution, deviceId) => { 268 | const {params, command} = execution; 269 | let state, let ref; 270 | switch (command) { 271 | case 'action.devices.commands.OnOff': 272 | state = {on: params.on}; 273 | ref = firebaseRef.child(deviceId).child('OnOff'); 274 | break; 275 | case 'action.devices.commands.BrightnessAbsolute': 276 | state = {brightness: params.brightness}; 277 | ref = firebaseRef.child(deviceId).child('Brightness'); 278 | break; 279 | case 'action.devices.commands.ColorAbsolute': 280 | state = {color: params.color}; 281 | ref = firebaseRef.child(deviceId).child('ColorSetting'); 282 | break; 283 | case 'action.devices.commands.SetFanSpeed': 284 | state = {currentFanSpeedSetting: params.fanSpeed}; 285 | ref = firebaseRef.child(deviceId).child('FanSpeed'); 286 | break; 287 | case 'action.devices.commands.SetModes': 288 | state = {currentModeSettings: params.updateModeSettings}; 289 | ref = firebaseRef.child(deviceId).child('Modes'); 290 | break; 291 | case 'action.devices.commands.ThermostatTemperatureSetpoint': 292 | state = {thermostatTemperatureSetpoint: params.thermostatTemperatureSetpoint}; 293 | ref = firebaseRef.child(deviceId).child('TemperatureSetting'); 294 | break; 295 | case 'action.devices.commands.ThermostatSetMode': 296 | state = {thermostatMode: params.thermostatMode}; 297 | ref = firebaseRef.child(deviceId).child('TemperatureSetting'); 298 | break; 299 | case 'action.devices.commands.ThermostatTemperatureSetRange': 300 | state = {thermostatTemperatureSetpointLow: params.thermostatTemperatureSetpointLow,thermostatTemperatureSetpointHigh: params.thermostatTemperatureSetpointHigh}; 301 | ref = firebaseRef.child(deviceId).child('TemperatureSetting'); 302 | } 303 | return ref.update(state) 304 | .then(() => state); 305 | }; 306 | 307 | app.onExecute(async (body) => { 308 | const {requestId} = body; 309 | // Execution results are grouped by status 310 | const result = { 311 | ids: [], 312 | status: 'SUCCESS', 313 | states: { 314 | online: true, 315 | }, 316 | }; 317 | 318 | const executePromises = []; 319 | const intent = body.inputs[0]; 320 | for (const command of intent.payload.commands) { 321 | for (const device of command.devices) { 322 | for (const execution of command.execution) { 323 | executePromises.push( 324 | updateDevice(execution, device.id) 325 | .then((data) => { 326 | result.ids.push(device.id); 327 | Object.assign(result.states, data); 328 | }) 329 | .catch(() => functions.logger.error('EXECUTE', device.id))); 330 | } 331 | } 332 | } 333 | 334 | await Promise.all(executePromises); 335 | return { 336 | requestId: requestId, 337 | payload: { 338 | commands: [result], 339 | }, 340 | }; 341 | }); 342 | 343 | app.onDisconnect((body, headers) => { 344 | functions.logger.log('User account unlinked from Google Assistant'); 345 | // Return empty response 346 | return {}; 347 | }); 348 | 349 | exports.smarthome = functions.https.onRequest(app); 350 | 351 | exports.requestsync = functions.https.onRequest(async (request, response) => { 352 | response.set('Access-Control-Allow-Origin', '*'); 353 | functions.logger.info('Request SYNC for user ${USER_ID}'); 354 | try { 355 | const res = await app.requestSync(USER_ID); 356 | functions.logger.log('Request sync completed'); 357 | response.json(res.data); 358 | } catch (err) { 359 | functions.logger.error(err); 360 | response.status(500).send(`Error requesting sync: ${err}`); 361 | } 362 | }); 363 | 364 | /** 365 | * Send a REPORT STATE call to the homegraph when data for any device id 366 | * has been changed. 367 | */ 368 | exports.reportstate = functions.database.ref('{deviceId}').onWrite(async (change, context) => { 369 | functions.logger.info('Firebase write event triggered this cloud function'); 370 | if (!app.jwt) { 371 | functions.logger.warn('Service account key is not configured'); 372 | functions.logger.warn('Report state is unavailable'); 373 | return; 374 | } 375 | const snapshot = change.after.val(); 376 | 377 | var syncvalue = {}; 378 | 379 | if (Object.prototype.hasOwnProperty.call(snapshot, 'OnOff')) { 380 | syncvalue = Object.assign(syncvalue, {on: snapshot.OnOff.on}); 381 | } 382 | if (Object.prototype.hasOwnProperty.call(snapshot, 'Brightness')) { 383 | syncvalue = Object.assign(syncvalue, {brightness: snapshot.Brightness.brightness}); 384 | } 385 | if (Object.prototype.hasOwnProperty.call(snapshot, 'ColorSetting')) { 386 | syncvalue = Object.assign(syncvalue, {color: snapshot.ColorSetting.color}); 387 | } 388 | if (Object.prototype.hasOwnProperty.call(snapshot, 'FanSpeed')) { 389 | if (Object.prototype.hasOwnProperty.call(snapshot.FanSpeed, 'currentFanSpeedSetting')) { 390 | syncvalue = Object.assign(syncvalue, {currentFanSpeedSetting: snapshot.FanSpeed.currentFanSpeedSetting}); 391 | } 392 | } 393 | if (Object.prototype.hasOwnProperty.call(snapshot, 'Modes')) { 394 | if (Object.prototype.hasOwnProperty.call(snapshot.Modes, 'currentModeSettings')) { 395 | syncvalue = Object.assign(syncvalue, {currentModeSettings: snapshot.Modes.currentModeSettings}); 396 | } 397 | } 398 | if (Object.prototype.hasOwnProperty.call(snapshot, 'TemperatureSetting')) { 399 | if (Object.prototype.hasOwnProperty.call(snapshot.TemperatureSetting, 'thermostatMode')) { 400 | syncvalue = Object.assign(syncvalue, {thermostatMode: snapshot.TemperatureSetting.thermostatMode}); 401 | } 402 | if ("thermostatTemperatureSetpoint" in snapshot) { 403 | syncvalue = Object.assign(syncvalue, {thermostatTemperatureSetpoint: snapshot.TemperatureSetting.thermostatTemperatureSetpoint}); 404 | } 405 | if ("thermostatTemperatureAmbient" in snapshot) { 406 | syncvalue = Object.assign(syncvalue, {thermostatTemperatureAmbient: snapshot.TemperatureSetting.thermostatTemperatureAmbient}); 407 | } 408 | if ('thermostatHumidityAmbient' in snapshot) { 409 | syncvalue = Object.assign(syncvalue, {thermostatHumidityAmbient: snapshot.TemperatureSetting.thermostatHumidityAmbient}); 410 | } 411 | if ('thermostatTemperatureSetpointLow' in snapshot) { 412 | syncvalue = Object.assign(syncvalue, {thermostatTemperatureSetpointLow: snapshot.TemperatureSetting.thermostatTemperatureSetpointLow}); 413 | } 414 | if ('thermostatTemperatureSetpointHigh' in snapshot) { 415 | syncvalue = Object.assign(syncvalue, {thermostatTemperatureSetpointHigh: snapshot.TemperatureSetting.thermostatTemperatureSetpointHigh}); 416 | } 417 | } 418 | 419 | const postData = { 420 | requestId: 'ff36a3ccsiddhy', /* Any unique ID */ 421 | agentUserId: USER_ID, /* Hardcoded user ID */ 422 | payload: { 423 | devices: { 424 | states: { 425 | /* Report the current state of our light */ 426 | [context.params.deviceId]: syncvalue, 427 | }, 428 | }, 429 | }, 430 | }; 431 | 432 | const data = await app.reportState(postData); 433 | functions.logger.log('Report state came back'); 434 | functions.logger.info(data); 435 | }); 436 | --------------------------------------------------------------------------------