Open IOT Challenge: whatâs up with Atta for Christmas?
Today (these last days in fact) I added 2 new sensors and logging features for the gateways.
New sensors
I added 2 new âsensor abilitiesâ (Groovy Traits) to Atta that produce less random data (I want more realistic data):
- temperature
- humidity
Iâve used this kind of formula:
y = Amplitude * cos B(x - C) + D
(where y is the value of the sensor, x is the time and the Amplitude is calculate with the min and max values.)
For example, this is the temperature ability:
Then the TemperatureSensor is very easy to implements:
You can see that we model fluctuations in temperature data throughout the time:
And, of course, these sensors are very easy to use:
Logging (and monitoring) features
I added a new object: Supervisor, that allows several things:
- display some data to the console about time:
-> [test](g002):emitting start:06:55:04.468 end:06:55:50.472 delay:46004 ms
- log data to file
- get data from gateways with REST API or SSE streaming
Remark: (the gateway has to implement supervisable
trait).
Logging
For example, if you want to display informations:
You have to get an instance of Supervisor
and âassignâ the gateways to the supervisor. Then if tou only want display informations, you can do something like that:
And then youâll get displayed informations like that:
-> [test](g001):emitting start:07:38:30.856 end:07:38:34.870 delay:4014 ms
-> [test](g001):publication start:07:38:34.868 end:07:38:34.871 delay:3 ms
-> [test](g002):emitting start:07:38:30.853 end:07:38:34.872 delay:4019 ms
-> [test](g002):publication start:07:38:34.869 end:07:38:34.873 delay:4 ms
Remark: gateway1.updateLog("publication", true, false)
return a Map
, ie: [scenarioName:test, gatewayId:g002, gatewayType:MQTT, task:publication, start:07:38:34.869, end:07:38:34.873, delay:4]
If you want to log data to a file (see loggerFileName("temperatures.humidity.log")
), you have just to do that : gateway1.updateLog("publication", true, true)
or even simpler gateway1.updateLog("publication")
. Then you will find a log file (temperatures.humidity.log
) that will look like this:
âMonitoringâ
I added two helpers if you want to make a web application to follow the gateways and their data. If you want to use it, you just have to do this:
Remark: this part has been develop with Vert.x.
# REST API
Then you can query gateways like that:
Open a browser with http://localhost:9090/api/gateways and youâll get a JSON Array of data:
Remark: If you want to query a specific gateway, you can use the api with the id of the gateway: http://localhost:9090/api/gateways/g002
Of course you can use it with JavaScript like that (with jQuery):
# SSE Streaming
If you prefer streaming than polling, Supervisor instance streams data to thanks SSE. You can open http://localhost:9090/sse/all to test it.
Youâll obtain a flux like that:
event: message
data: [{"id":"g001","kind":"MQTT","location":"somewhere","lastSensorsData":{"001":{"temperature":{"unit":"Celsius","value":7.9672647056605905},"locationName":"RoomA","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"002":{"temperature":{"unit":"Celsius","value":3.15452894071316},"locationName":"RoomB","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"H003":{"humidity":{"unit":"%","value":15.498214331266041},"locationName":"Garden","when":"2015-12-28T06:55:53+0000","kind":"H%\u00b0"}}},{"id":"g002","kind":"MQTT","location":"somewhere","lastSensorsData":{"T004":{"temperature":{"unit":"Celsius","value":2.710313725785902},"locationName":"RoomB","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"T003":{"temperature":{"unit":"Celsius","value":7.9672647056605905},"locationName":"RoomA","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"H002":{"humidity":{"unit":"%","value":15.498214331266041},"locationName":"Garden","when":"2015-12-28T06:55:53+0000","kind":"H%\u00b0"}}}]
event: message
data: [{"id":"g001","kind":"MQTT","location":"somewhere","lastSensorsData":{"001":{"temperature":{"unit":"Celsius","value":7.9672647056605905},"locationName":"RoomA","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"002":{"temperature":{"unit":"Celsius","value":3.15452894071316},"locationName":"RoomB","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"H003":{"humidity":{"unit":"%","value":15.498214331266041},"locationName":"Garden","when":"2015-12-28T06:55:53+0000","kind":"H%\u00b0"}}},{"id":"g002","kind":"MQTT","location":"somewhere","lastSensorsData":{"T004":{"temperature":{"unit":"Celsius","value":2.710313725785902},"locationName":"RoomB","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"T003":{"temperature":{"unit":"Celsius","value":7.9672647056605905},"locationName":"RoomA","when":"2015-12-28T06:55:53+0000","kind":"TC\u00b0"},"H002":{"humidity":{"unit":"%","value":15.498214331266041},"locationName":"Garden","when":"2015-12-28T06:55:53+0000","kind":"H%\u00b0"}}}]
And itâs very easy to use it with JavaScript:
You can find a complete sample here: https://github.com/ant-colony/atta/blob/master/sandbox/mqtt_samples/groovy/mqtt_temp_hum.groovy.
âEt voilĂ !â, thatâs all for today. Next time, I will introduce you a new sensor to simulate a herd of animals. So stay tuned. :)
Tweet