Bayu Abi Pamungkas
Created February 24, 2018

Monitoring and Control Temperature with AlexaPi

In this project we will ask the AlexaPi how much the temperature in a room and ask her for turn on or off a fan

117
Monitoring and Control Temperature with AlexaPi

Things used in this project

Story

Read more

Schematics

Schematics

Code

Arduino Code

Arduino
#include <dht.h>

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_HOST "alexaarduino-4ee6a.firebaseio.com"
#define FIREBASE_AUTH "KXrRUE6yq5BEXIC6S25Bx8UAwRVOm0uGMyBaWQST"
#define WIFI_SSID "bayuabi"
#define WIFI_PASSWORD "12345678"

#define dhtPin D8
dht DHT;
int lastPowerState = 0;

#define relayPin D5

unsigned long prev=0;

void setup() {
  Serial.begin(9600);
  pinMode(relayPin,OUTPUT);
  digitalWrite(relayPin,LOW);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop() {
  DHT.read11(dhtPin);
  int temperature = DHT.temperature;

  unsigned long current = millis();
  if(current - prev > 60000){
    Firebase.setInt("temperature",temperature);
    prev = current;
  }
  
  int power = Firebase.getInt("power");
  Serial.println(power);

  if(power != lastPowerState){
    if(power ==1){
      for(int i=0; i<3;i++){
      digitalWrite(relayPin,HIGH);
      delay(100);
      digitalWrite(relayPin,LOW);
      delay(100);
      }
    }
    if(power == 0){
      for(int i=0; i<2;i++){
      digitalWrite(relayPin,HIGH);
      delay(100);
      digitalWrite(relayPin,LOW);
      delay(100);
      }
    }
    lastPowerState = power;
  }
  else{
    digitalWrite(relayPin,LOW);
  }
}

Python Code

Python
from firebase import firebase
from flask import Flask
from flask_ask import Ask,statement,question,session
import requests

app = Flask(__name__)
ask = Ask(app,"/")

url = 'https://alexaarduino-4ee6a.firebaseio.com/'
firebase = firebase.FirebaseApplication(url,None)

@ask.launch
def start():
    return question('hi there, do you want to know the temperature in this room?')

@ask.intent("YesIntent")
def yes():
    temperature = firebase.get('/temperature',None)
    if temperature >= 25:
        answer ='the temperature is {}...do you want to turn on the fan?'.format(temperature)
    else:
        answer ='the temperature is {}...do you want to turn off the fan?'.format(temperature)


    return question(answer)

@ask.intent("NoIntent")
def no():
    return statement('You are so handsome')

@ask.intent("TurnOn")
def on():
    post = firebase.patch('',{'power':1})
    return statement('of course')

@ask.intent("TurnOff")
def off():
    post = firebase.patch('',{'power':0})
    return statement('of course')

if __name__ == '__main__':
    app.run(debug=True)

IntentSchema

ActionScript
{
  "intents": [
    {
      "intent": "YesIntent"
    },
    {
      "intent": "NoIntent"
    },
    {
      "intent": "TurnOn"
    },
    {
      "intent": "TurnOff"
    }
  ]
}

Sample Utterances

ActionScript
YesIntent yes
YesIntent sure
NoIntent no
NoIntent go away
TurnOn yes please turn on the fan
TurnOff yes please turn off the fan

Arduino Code

Python Code

Credits

Bayu Abi Pamungkas

Bayu Abi Pamungkas

1 project • 0 followers

Comments