Lasith Ishan Premaratne
Created March 27, 2017

A Complete Home Automation solution

The aim of this project is to implement an affordable, easy to install system to enable remote access to existing electrical appliances

78
A Complete Home Automation solution

Story

Read more

Schematics

Wiring Schematic

File missing, please reupload.

Wiring Schematic

Code

Linkit 7688 Duo Code

Python
import requests
import socket
import threading
import logging
from pyfirmata import Arduino, util
from time import sleep
 
board = Arduino('/dev/ttyS0')

# change this to the values from MCS web console
DEVICE_INFO = {
    'device_id' : 'INSERT YOUR ID',
    'device_key' : 'INSERT YOUR KEY'
}

# change 'INFO' to 'WARNING' to filter info messages
logging.basicConfig(level='INFO')

heartBeatTask = None

def establishCommandChannel():
    # Query command server's IP & port
    connectionAPI = 'https://api.mediatek.com/mcs/v2/devices/%(device_id)s/connections.csv'
    r = requests.get(connectionAPI % DEVICE_INFO,
                 headers = {'deviceKey' : DEVICE_INFO['device_key'],
                            'Content-Type' : 'text/csv'})
    logging.info("Command Channel IP,port=" + r.text)
    (ip, port) = r.text.split(',')

    # Connect to command server
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((ip, int(port)))
    s.settimeout(None)

    # Heartbeat for command server to keep the channel alive
    def sendHeartBeat(commandChannel):
        keepAliveMessage = '%(device_id)s,%(device_key)s,0' % DEVICE_INFO
        commandChannel.sendall(keepAliveMessage)
        logging.info("beat:%s" % keepAliveMessage)

    def heartBeat(commandChannel):
        sendHeartBeat(commandChannel)
        # Re-start the timer periodically
        global heartBeatTask
        heartBeatTask = threading.Timer(40, heartBeat, [commandChannel]).start()

    heartBeat(s)
    return s

def waitAndExecuteCommand(commandChannel):
    while True:
        command = commandChannel.recv(1024)
        logging.info("recv:" + command)
        # command can be a response of heart beat or an update of the LED_control,
        # so we split by ',' and drop device id and device key and check length
        fields = command.split(',')[2:]
        #setLED(1)

        if len(fields) > 1:
            timeStamp, dataChannelId, commandString = fields
            if dataChannelId == 'LED_Control':
                #check the value - it's either 0 or 1
                commandValue = int(commandString)
                logging.info("led :%d" % commandValue)
                setLED(commandValue)

def setLED(state):
    # Note the LED is "reversed" to the pin's GPIO status.
    # So we reverse it here.
    if state:
        board.digital[12].write(1)
    else:
        board.digital[12].write(0)


if __name__ == '__main__':
    channel = establishCommandChannel()
    waitAndExecuteCommand(channel)

Credits

Lasith Ishan Premaratne

Lasith Ishan Premaratne

1 project • 45 followers
An Electronics Engineering student with a passion towards Embedded hardware and software development to create energy efficient systems.

Comments