SmartHome - your home data in real time!
Tags: redishackathon, showdev, iot, javascript
Published: 2022-08-28 21:28:00


### Overview of My Submission
hi, today we are going to see a project that collect your home temperature and humidity in real time!

![base app overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3bmfae81zmhnnukv8lp.gif)
before talking in the detail about the app is important to understand the architecture.

![architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bp83qf82ocu7pcm64uh.png)

as we can see there are many different parts:
* Backend: write using node js and TS, handle the redis integration using redis OM, MongoDb integration, Aws Iot real time MQTT connection
and Tensorflow js model utilization. Use redis for reading the real time sensor messagges over the MQTT channel "machines" and try to find a sensor associated with it,
if found insert a datalog of the received data in both MongoDb and redis for rapid real time data into the frontend graph.
the alarm logic is associated to the datalogs, if when a datalog is created trigger an alarm rule an active alarm is created and will be closed only when an alarm below the trashold arrive, all the alarms are stored and not deleted.
Tensorflow is used to read a very simple regression model and use it for the next hour predictions.
the most cool features of the backend are: 
 - Aws Iot integration
 - Tensorflow Js integration to parse an existing ML model and use it into JS
 - Redis integration to cache data and use redis JSON and redis search to get real time data
 - Redis model generator that take an existing Mongoose model and parse it into a redis one with no code logic!
 - the alarms rules that are stored into the database!, an example is:

 

```
{
    "_id" : ObjectId("6300e639830e983ff546f7af"),
    "sensorId" : ObjectId("62fce2bc830e983ff546f775"),
    "rule" : {
        ">" : [
            {
                "var" : "temp"
            },
            25.0
        ]
    },
    "name" : "Bedroom High Temperature",
    "type" : "rule"
}
```
as you can see the rule is a sort of logic!, in fact this is possible by the JSON logic module. 

* Frontend: retrieve the data from the backend API to make a graph based on a custom range time, in the graph you can choose the sensor who data you want to see,
there is also the average temperature and humidity cards in the bottom of the dashboard that show you the Average in the **selected range**. 
the alrams are real time and retrived with only 1 minute cache to don't lose the new alarms, as explained before the alrams are created only when a value trigger an alarm trashold.
there is also a home map to see the sensors disposition and have an idea of the space, the "SUN" card show you the **actual** values and not the average.
the last card show you the prediction fo the next hour.
All the components support the **multi theme** feature to personalize the user experience.


![theme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viibmjdwtr2oo91csez2.gif)

frontend also have mobile support but only **Horizontal** not vertical!

* Sensors: send real time temperature and humidity data every 5 minutes, integrated with Aws-Iot. this sensor are made using Mongoose Os, a really cool it framework that help to increase speed using simil JS code. for the code see the Firmware repo


![sensor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6fyawk3lio1h4w5hdyg.jpg)

* Redis: used to store the cache and the partial datalogs data to speed up the responses time and keep retrive graphic data simple.

* MongoDb: used to store all other data like sensor and alarms

### Previews
Live Preview: [Site](https://iot-sensor-frontend-git-master-marco-bertelli.vercel.app/pages/iot-dashboard)
after enter into the site select a range and enjoy it!

now that we know the base of the app is time to talk about the redis integration, it help a lot for the graph real time data, because as we say before the data are partial stored into redis to take i for graphs, before redis with only mongo the response time was 300 ms, after 120 ms what a great thing!. i also use redis cache to store the most important and slow enpoint (like the tensorflow one) and it was very helpfull, here we go too see a list of endpoints and used cache:
* datalogs/: 
    - /predict: predict the next hour data, using redis cache to keep it for 15 minutes
    - /ambient/now: actual data
    - /:sensorId/:interval: get datalogs of a sensor use redis JSON and redis Search!
    - /ambient/humidity/:interval: Average humidity
    - /ambient/temperature/:interval: Average temp
* alarms/:
    - /active: get active alrams, use redis cache to store it for 1 minutes
    - /finished: get finished alarms: use redis cache to keep it for 3 minutes
* sensors/: get the sensor list, use redis cache to keep it for 30 minutes.

so a full list of frontend feature you can try in the live demo!
- real time graph data from my house sensors on 1 or more sensor in an interval
- 2D map to take an idea of sensor position
- active alarms up to now!
- average temp and humidity in the selected range
- card with actuals info
- next hours prevision using ML!

## futures feature
the only thing that i don't have time to make was a complete ML model, in fact this simple one use a regression but a complete one is a timeseries prevision model!



### Submission Category: MEAN/MERN Mavericks

### video overview of the project [Video](https://www.youtube.com/watch?v=0IFBh0KPuxA)


### Language Used
Node js / TS for the backend
Angular for the frontend
Mongoose Os for the firmware code


### Link to Code
Live Preview: [Site](https://iot-sensor-frontend-git-master-marco-bertelli.vercel.app/pages/iot-dashboard)
after enter into the site select a range and enjoy it!
BackeEnd repo: {% embed https://github.com/marco-bertelli/iotSensorBackend %}
FrontEnd repo: {% embed https://github.com/marco-bertelli/iotSensorFrontend %}
Firmware repo: {% embed https://github.com/marco-bertelli/iotSensorFirmware %}
Tensorflow Model: if you want more info of tensorflow visit the model here: [colab](https://colab.research.google.com/drive/14tjipy3yHAeLTcxzom1SU8sERq2GiEgl?usp=sharing)

## closure

if you like the project or have any questions leave a like and a comment! 
all the shares will be appreciated!

- - - 


* _Check out [Redis OM](https://redis.io/docs/stack/get-started/clients/#high-level-client-libraries), client libraries for working with Redis as a multi-model database._
* _Use [RedisInsight](https://redis.info/redisinsight) to visualize your data in Redis._
* _Sign up for a [free Redis database](https://redis.info/try-free-dev-to)._