Tasos
Created August 23, 2019 © GPL3+

Daily Living Activities Radar

Detection of the environment where a person is located and the state of activity he/she is currently engaged.

IntermediateFull instructions provided10 hours27
Daily Living Activities Radar

Things used in this project

Story

Read more

Code

Main_Python_Gateway_Code.py

Python
Manages Google sheet, MQTT messages, and data handling towards Google sheets.
from __future__ import print_function
import uuid

import paho.mqtt.client as mqtt
from message import topics, Factory, Telemetry
from datetime import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
             'https://www.googleapis.com/auth/drive']

#Provide your own credentials path location
credentials = ServiceAccountCredentials.from_json_keyfile_name('C:/........../client_secret.json',
                                                                   scope)
prev_hum=0.0
prev_light=0.0
prev_sound=0.0
prev_temp=0.0

#Provide your own ID
SPREADSHEET_ID = '1wKjj57ddZDFL..........'
#RANGE_NAME = 'Class Data!A2:D200'
gc = gspread.authorize(credentials)
client = gspread.authorize(credentials)
sh = gc.open('Python_Test')
worksheet = sh.get_worksheet(0)
buf=worksheet
buf.clear()

if credentials.access_token_expired:
        checkA=1
        countA=0
        while (checkA):
            try:
                gc.login()  # refreshes the token  CRASHES HERE SOMETIMES
                sh = gc.open_by_key(SPREADSHEET_ID)
                worksheet = sh.get_worksheet(0)
                checkA=0
            except:
                print("Login refresh error")
            finally:
                countA=countA+1
                if (countA>10):
                    print("Failed REFRESHING LOGIN %d times.\n", countA)
                    checkA=0

worksheet.update_acell('A1', 'Light Intensity')
worksheet.update_acell('B1', 'Humidity')
worksheet.update_acell('C1', 'Sound Level')
worksheet.update_acell('D1', 'Temperature')
worksheet.update_acell('E1', 'Time')
worksheet.update_acell('F1', 'Day')

# cell_list = buf.range('A2:D50')
# for cell in cell_list:
#     cell.value = ''
# worksheet.update_cells(cell_list)

cnl=2
#Provide the mqtt paswword
mqtt_user_name = 'oauth2-user'
mqtt_password = '..........'  
# copy and paste here external client id from your account'  # copy and paste here external client id from your account
user_id = 'xxxx'  # copy and paste here your user id
device_id = 'TO136-xxxxxxxxx'  # copy and paste here your device id

ca_cert_path = 'cacert.crt'


def on_connect(client, userdata, flags, rc):
    print('Connected with result code {code}'.format(code=rc))
    for topic_str in topics:
        topic = topics[topic_str].format(userId=user_id, deviceId=device_id)
        client.subscribe(topic)
        print('Subscriptions sent')

def on_disconnect(client, userdata, rc):
    #logging.info("disconnecting reason  "  +str(rc))
    client.connected_flag=False
    client.disconnect_flag=True
    client.connect('ns01-wss.brainium.com', 443)


def on_message(client, userdata, msg):
    global buf, cnl
    global prev_hum, prev_light, prev_sound, prev_temp

    factory = Factory()
    msgObj = factory.getInstance(msg)
    msgObj.process()

    now = datetime.now()  # current date and time
    year = now.strftime("%Y")
    #print("year:", year)
    month = now.strftime("%m")
    #print("month:", month)
    day = now.strftime("%d")
    #print("day:", day)
    time = now.strftime("%H:%M:%S")
    #print("time:", time)
    date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
    #print("date and time:", date_time)

    #print("Light: ", Telemetry.vl)

    buf.update_cell(1, 15, Telemetry.vl)
    val = buf.cell(1, 15).value
    val=float(val)
    print("Light 2:", val)
    diff_light = abs(val - prev_light)
    print("Percent Light 2:", diff_light)
    if diff_light>0.99:
        prev_light = val
        buf.update_cell(cnl, 1, buf.cell(1, 15).value)
        buf.update_cell(cnl, 2, Telemetry.v2)
        buf.update_cell(cnl, 3, Telemetry.v3)
        buf.update_cell(cnl, 4, Telemetry.v4)
        buf.update_cell(cnl, 5, time)
        buf.update_cell(cnl, 6, day)
        cnl = cnl + 1
        print('Ligh Dif: ', diff_light)

    buf.update_cell(2, 15, Telemetry.v2)
    val = buf.cell(2, 15).value
    val = float(val)
    diff_hum = abs(val - prev_hum)
    #print("Percent Light 2:", percent_light)
    if diff_hum > 0.5:
        prev_hum = val
        buf.update_cell(cnl, 1, buf.cell(1, 15).value)
        buf.update_cell(cnl, 2, buf.cell(2, 15).value)
        buf.update_cell(cnl, 3, Telemetry.v3)
        buf.update_cell(cnl, 4, Telemetry.v4)
        buf.update_cell(cnl, 5, time)
        buf.update_cell(cnl, 6, day)
        cnl = cnl + 1
        print('Diff Hum: ', diff_hum)

    buf.update_cell(3, 15, Telemetry.v3)
    val = buf.cell(3, 15).value
    val = float(val.replace(',', '.'))
    diff_sound = abs(val - prev_sound)
    # print("Percent Light 2:", percent_light)
    if diff_sound > 2:
        prev_sound = val
        buf.update_cell(cnl, 1, buf.cell(1, 15).value)
        buf.update_cell(cnl, 2, buf.cell(2, 15).value)
        buf.update_cell(cnl, 3, buf.cell(3, 15).value)
        buf.update_cell(cnl, 4, Telemetry.v4)
        buf.update_cell(cnl, 5, time)
        buf.update_cell(cnl, 6, day)
        cnl = cnl + 1
        print("Sound Row: ", cnl)


    if cnl == 2:
        val = buf.cell(1, 15).value
        buf.update_cell(cnl, 1, val)
        val = buf.cell(2, 15).value
        buf.update_cell(cnl, 2, val)
        val = buf.cell(3, 15).value
        buf.update_cell(cnl, 3, val)
        buf.update_cell(cnl, 4, Telemetry.v4)
        buf.update_cell(cnl, 5, time)
        buf.update_cell(cnl, 6, day)
        # now = datetime.now()
        # timestamp = datetime.timestamp(now)
        # dt_object = datetime.fromtimestamp(timestamp)
        # buf.update_cell(cnl, 5, dt_object)
        cnl = cnl + 1









def main():


    client = mqtt.Client(client_id=str(uuid.uuid4()), transport='websockets')
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_disconnect=on_disconnect

    client.tls_set(ca_certs=ca_cert_path)
    client.username_pw_set(mqtt_user_name, mqtt_password)

    client.connect('ns01-wss.brainium.com', 443, keepalive=20)

    for topic_str in topics:
        topic = topics[topic_str].format(userId=user_id, deviceId=device_id)
        client.subscribe(topic)



    #client.loop_forever()
    run = True
    while run:
        client.loop(timeout=15.0)
        print("Row: ", cnl, "  Time: ", datetime.now())



if __name__ == "__main__":
    main()

Message.py

Python
PAHO client MQTT message processing
import json


class msg_dummy:
    def __init__(self, topic=None, payload=None):
        self.topic = topic
        self.payload = payload


class Message:
    def __init__(self, msg):
        self.topic = msg.topic
        self.payload = msg.payload.decode("utf-8")
        if not (self.payload in (None, '')):
            self.json = json.loads(self.payload)
            self.parse()

    def getType(self):
        return type(self).__name__

    def __str__(self):
        return type(self).__name__

    def dump(self):
        return str(self.json)

    def parse(self):
        pass


class ProbabilityRank(Message):
    def parse(self):
        self.code = self.json["code"]
        self.name = self.json["name"]
        self.rank = self.json["rank"]


class Motion(Message):
    def parse(self):
        self.id = self.json["id"]
        self.deviceId = self.json["deviceId"]
        self.projectId = self.json["projectId"]
        self.modelId = self.json["modelId"]
        self.startedAt = self.json["startedAt"]
        self.finishedAt = self.json["finishedAt"]
        self.receivedAt = self.json["receivedAt"]
        self.code = self.json["code"]
        self.name = self.json["name"]
        self.selfProbability = self.json["selfProbability"]
        self.acceleration = self.json["acceleration"]
        self.speed = self.json["speed"]
        self.probabilityRank = []
        for probabilityRank in self.json["probabilityRank"]:
            self.probabilityRank.append(ProbabilityRank(msg_dummy(self.topic, str(probabilityRank))))

    def process(self):
        print("Motion: ", self.name, " - Model ID: ", self.modelId)
        print("Start: ", self.startedAt, " - Finished: ", self.finishedAt, " - Received: ", self.receivedAt)


class Alert(Message):
    def parse(self):
        self.id = self.json["id"]
        self.type = self.json["type"]
        self.triggeredTimestamp = self.json["triggeredTimestamp"]
        self.ruleId = self.json["ruleId"]
        self.projectId = self.json["projectId"]
        self.projectName = self.json["projectName"]
        self.deviceId = self.json["deviceId"]
        self.deviceName = self.json["deviceName"]
        if self.type == "SMART_RULE":
            self.datasource = self.json["datasource"]
            self.condition = self.json["condition"]
            self.value = self.json["value"]  # array
            self.datasourceUnits = self.json["datasourceUnits"]
            self.triggeredValue = self.json["triggeredValue"]
        elif self.type == "AI_RULE":
            self.motionTypeCode = self.json["motionTypeCode"]
            self.motionTypeName = self.json["motionTypeName"]
        else:  # AI PREDICTIVE MAINTENANCE
            self.patternId = self.json["patternId"]
            self.patternName = self.json["patternName"]
            self.anomalyType = self.json["anomalyType"]

    def process(self):
        if self.type == "SMART_RULE":
            print("Source: ", self.datasource, " - Timestamp: ", self.triggeredTimestamp)
            print("Value: ", self.triggeredValue, " ", self.datasourceUnits, " (", self.condition, " ", self.value, ")")
        elif self.type == "AI_RULE":
            print("Motion: ", self.motionTypeName, " - Timestamp: ", self.triggeredTimestamp)
            print("Code: ", self.motionTypeCode)
        else:
            print("Source: ", self.patternName, " - Timestamp: ", self.triggeredTimestamp)
            print("Id: ", self.patternId, " - Anomaly Type: ", self.anomalyType)


class PDM(Message):
    def parse(self):
        self.id = self.json["id"]
        self.name = self.json["name"]
        self.type = self.json["type"]
        self.triggeredFirstTime = self.json["triggeredFirstTime"]
        self.triggeredLastTime = self.json["triggeredLastTime"]
        self.triggeredCounter = self.json["triggeredCounter"]

    def process(self):
        print("Pattern: ", self.name, " - Type: ", self.type, " - Trigger Counter: ", self.triggeredCounter)
        print("Triggered First Time: ", self.triggeredFirstTime, " - Triggered Last Time: ", self.triggeredLastTime)


class Telemetry(Message):
    global vl
    __vectors__ = ("ACCELERATION_NORM", "WORLD_ACCELERATION_NORM", "ROTATION", "MAGNETIC_FIELD_NORM")
    __scalars__ = ("GYROSCOPE_NORM", "HUMIDITY_TEMPERATURE", "PRESSURE", "HUMIDITY", "PROXIMITY",
                   "VISIBLE_SPECTRUM_LIGHTNESS", "IR_SPECTRUM_LIGHTNESS", "SOUND_LEVEL")

    def parse(self):
        if type(self.json) is list:
            self.readings = self.json
        else:
            self.reading = [self.json]

    def isVector(self):
        return self.topic in self.__vectors__

    def process(self):

        for reading in self.readings:
            #print(self.topic, " - Timestamp: ", reading["timestamp"])
             if self.topic=="/v1/users/2503/in/devices/TO136-02021000010010D5/datasources/VISIBLE_SPECTRUM_LIGHTNESS":
                 Telemetry.vl = reading["scalar"]
            #     print("Light: ",reading["scalar"])
             if self.topic == "/v1/users/2503/in/devices/TO136-02021000010010D5/datasources/HUMIDITY":
                Telemetry.v2 = reading["scalar"]
             if self.topic == "/v1/users/2503/in/devices/TO136-02021000010010D5/datasources/SOUND_LEVEL":
                    Telemetry.v3 = reading["scalar"]
             if self.topic == "/v1/users/2503/in/devices/TO136-02021000010010D5/datasources/HUMIDITY_TEMPERATURE":
                    Telemetry.v4 = reading["scalar"]
                #1wKjj57ddZDFLb4cu9lasXLlYcIVESptaHCJaAXEMQJs
            # if self.isVector():
            #     print(reading["vector"])
            # else:
            #     print(reading["scalar"])


topics = {
    "ACCELERATION_NORM": "/v1/users/{userId}/in/devices/{deviceId}/datasources/ACCELERATION_NORM",
    "WORLD_ACCELERATION_NORM": "/v1/users/{userId}/in/devices/{deviceId}/datasources/WORLD_ACCELERATION_NORM",
    "GYROSCOPE_NORM": "/v1/users/{userId}/in/devices/{deviceId}/datasources/GYROSCOPE_NORM",
    "ROTATION": "/v1/users/{userId}/in/devices/{deviceId}/datasources/ROTATION",
    "HUMIDITY_TEMPERATURE": "/v1/users/{userId}/in/devices/{deviceId}/datasources/HUMIDITY_TEMPERATURE",
    "PRESSURE": "/v1/users/{userId}/in/devices/{deviceId}/datasources/PRESSURE",
    "HUMIDITY": "/v1/users/{userId}/in/devices/{deviceId}/datasources/HUMIDITY",
    "PROXIMITY": "/v1/users/{userId}/in/devices/{deviceId}/datasources/PROXIMITY",
    "VISIBLE_SPECTRUM_LIGHTNESS": "/v1/users/{userId}/in/devices/{deviceId}/datasources/VISIBLE_SPECTRUM_LIGHTNESS",
    "IR_SPECTRUM_LIGHTNESS": "/v1/users/{userId}/in/devices/{deviceId}/datasources/IR_SPECTRUM_LIGHTNESS",
    "MAGNETIC_FIELD_NORM": "/v1/users/{userId}/in/devices/{deviceId}/datasources/MAGNETIC_FIELD_NORM",
    "SOUND_LEVEL": "/v1/users/{userId}/in/devices/{deviceId}/datasources/SOUND_LEVEL",
    "PDM_PATTERN": "/v1/users/{userId}/in/devices/{deviceId}/datasources/PDM_PATTERN",
    "PDM_EVENT": "/v1/users/{userId}/in/devices/{deviceId}/datasources/PDM_EVENT",
    "alerts": '/v1/users/{userId}/in/alerts',
    "MOTION": "/v1/users/{userId}/in/devices/{deviceId}/datasources/MOTION"
}


class Factory:
    __classMapping__ = {
        "alerts": Alert,
        "MOTION": Motion,
        "ACCELERATION_NORM": Telemetry,
        "WORLD_ACCELERATION_NORM": Telemetry,
        "GYROSCOPE_NORM": Telemetry,
        "ROTATION": Telemetry,
        "HUMIDITY_TEMPERATURE": Telemetry,
        "PRESSURE": Telemetry,
        "HUMIDITY": Telemetry,
        "PROXIMITY": Telemetry,
        "VISIBLE_SPECTRUM_LIGHTNESS": Telemetry,
        "IR_SPECTRUM_LIGHTNESS": Telemetry,
        "MAGNETIC_FIELD_NORM": Telemetry,
        "SOUND_LEVEL": Telemetry,
        "PDM_PATTERN": PDM,
        "PDM_EVENT": PDM
    }

    def getInstance(self, msg):
        obj = None
        for key in self.__classMapping__:
            if key in msg.topic:
                cls = self.__classMapping__.get(key)
                obj = cls(msg)
                break
        return obj

Credits

Tasos

Tasos

10 projects • 2 followers

Comments