UbiMaker
Published

Make me coffee when I get to the office

Detect when I connect to my office's WiFi network and then make coffee for me

EasyFull instructions provided1,899
Make me coffee when I get to the office

Things used in this project

Story

Read more

Code

dictionary.csv

Plain text
Agustin,e8:99:c4:81:cd:8c

wifi_sniff.py

Python
#!/usr/bin/python

from multiprocessing import Process, Manager
from scapy.all import *
import ubidots
import csv
import time
from datetime import datetime, date, timedelta

# Declare variables

users = {}
office = "MassChallenge"

# Functions

def update_user(user, ds):
    try:
        var = get_var_by_name(user, ds)
        var.save_value({"value": 1,"context":{"log":"First time today"}})
        var.save_value({"value":0})
        return

    except:
        return

def arp_count(pkt):

    if ARP in pkt:
        if pkt[ARP].hwsrc in users:
            device = users[pkt[ARP].hwsrc]
            print "User seen:" + device
            p = Process(target=update_user, args=(device,ds,))
            p.start()
            return
        else:
            #print "Scapy running - HW address not found"
            return

def get_var_by_name(var_name, ds):

    # Search for a variable in a datasource. If found, returns the variable. If$
    try:
        for var in ds.get_variables():

            if var.name == var_name:
                return var

        var = ds.create_variable({"name": var_name, "unit": "."})
        return var
    except:
        print "Couldn't get variables from DS"
        return

if __name__ == '__main__':

    # Create Ubidots connection

    api = ubidots.ApiClient("a5b9c577ec80838819f7a25d0397e1fcac142ffc")

    # Search for a data source with name matching this. If it doesn't exist, cr$

    ds = None

    try:
        for cur_ds in api.get_datasources():
            if cur_ds.name == office:
                ds = cur_ds
                break

            if ds is None:
                ds = api.create_datasource({"name": office})
        print "Connected to Ubidots, will send data to Ubidots data source call$

    except:
        print "Ds not found nor created"

    # Load dictionary from CSV file

    with open('/home/root/dictionary.csv','rb') as f:
        reader = csv.reader(f)
        for row in reader:
            users[row[1]] = row[0]

    print "List of users loaded."
    print users

    sniff(prn = arp_count, filter="arp", store = 0)

pour_coffee.py

Python
#!/usr/bin/python

import time, mraa
from ubidots import ApiClient

# Setup Servo

x = mraa.Pwm(3)
x.period_ms(20)

#Connect to Ubidots

for i in range(0,5):
    try:
        print "Requesting Ubidots token"
        api = ApiClient('a5b9c577ec80838819f7a25d0397e1fcac142ffc')
        # Replace with your Ubidots API Key here
    except:
        print "No internet connection, retrying..."
        time.sleep(5)

try:
    control = api.get_variable('554e5f5c7625423aa9241811')
    print "Connected to Ubidots"

except:
    print "No internet connection or no variables found"
    sys.exit(0)

while(True):

    y = mraa.Aio(0)
    temp.save_value({"value": y.read()})

    # Read control variable

    lastValue = control.get_values(1)

    # Set pins to the values received

    if lastValue[0]['value']:
        print "Switch is ON"
        x.enable(True)
        value = 0.1
        while (value >= 0.05):
            x.write(value)
            time.sleep(0.5)
            value = value - 0.005

        value = 0.1
        x.write(value)
        time.sleep(1)
        x.enable(False)
        control.save_value({"value":0})

    else:

        print "Switch off, doing nothing..."

Credits

UbiMaker

UbiMaker

8 projects • 186 followers
Maker @ ubidots.com

Comments