AttA plays well with Node-RED
Today I explain how to test your AttA simulation scripts with Node-RED.
Node-RED introduction
Node-RED is a project from IBM http://nodered.org/. This is a tool that allows to visually define connections, triggers, ⊠between things.
I wanted to check if AttA was compliant with Node-RED. But, first of all, we have to install Node-RED (you need NodeJS and npm):
sudo npm install -g node-red
Then, you have to install CoAP support thanks to the project node-red-contrib-coap:
cd ~/.node-red
npm install node-red-contrib-coap
Before running Node-RED, we are going to create an AttA script simulation.
AttA simulation
I want to simulate 2 MQTT Gateway and 1 CoAP Gateway:
package iot_demo
import org.typeunsafe.atta.gateways.Gateway
import org.typeunsafe.atta.gateways.coap.SimpleCoapGateway
import org.typeunsafe.atta.gateways.mqtt.MQTTGateway
import org.typeunsafe.atta.gateways.mqtt.tools.MQTTBroker
import org.typeunsafe.atta.sensors.HumiditySensor
import org.typeunsafe.atta.sensors.LightSensor
import org.typeunsafe.atta.sensors.SoundSensor
import org.typeunsafe.atta.sensors.TemperatureSensor
import static org.typeunsafe.atta.core.Timer.every
MQTTBroker broker = new MQTTBroker(protocol:"tcp", host:"localhost", port:1883)
SimpleCoapGateway coapGateway0 = new SimpleCoapGateway(
id:"coapgw0",
coapPort: 5686,
locationName: "Work",
path:"work"
)
coapGateway0.sensors([
new SoundSensor(id:"soundRoom9",locationName: "OFFICE03"),
new LightSensor(id:"lightRoom9A", locationName: "ROOM9")
]).start {
every(5).seconds().run {
coapGateway0.notifyAllSensors()
}
}
Gateway gateway1 = new MQTTGateway(
id:"g001",
mqttId: "g001",
locationName: "somewhere",
broker: broker
).sensors([
new TemperatureSensor(
id:"001",
minTemperature: -5.0, maxTemperature: 10.0,
delay: 1000, locationName:"ChildrenRoom"
),
new HumiditySensor(id:"H003", locationName:"Garden")
])
Gateway gateway2 = new MQTTGateway(
id:"g002",
mqttId: "g002",
locationName: "somewhere",
broker: broker
).sensors([
new TemperatureSensor(
id:"T003",
minTemperature: -5.0, maxTemperature: 10.0,
delay: 1000, locationName:"ParentsRoom"
),
new HumiditySensor(id:"H002", locationName:"BathRoom")
])
gateway1.connect(success: { token ->
gateway1.start {
every(3).seconds().run {
gateway1.notifyAllSensors()
gateway1
.topic("home/g1/sensors")
.jsonContent(gateway1.lastSensorsData())
.publish(success: {publishToken -> })
}
}
})
gateway2.connect(success: { token ->
gateway2.start {
every(4).seconds().run {
gateway2.notifyAllSensors()
gateway2
.topic("home/g2/sensors")
.jsonContent(gateway2.lastSensorsData())
.publish(success: {publishToken -> })
}
}
})
You can find the source here: https://github.com/ant-colony/atta/blob/master/sandbox/iot_demo/iot.groovy
MQTT Broker
You need a MQTT Broker:
import mosca from 'mosca';
let mqttBroker = new mosca.Server({
port: 1883
});
mqttBroker.on('clientConnected', (client) => {
console.log('client connected', client.id);
});
mqttBroker.on('subscribed', (topic, client) => {
console.log('subscribed : ', topic, client.id);
});
mqttBroker.on('clientDisconnected', (client) => {
console.log('clientDisconnected : ', client.id)
});
mqttBroker.on('ready', () => {
console.log('This is SKYNET listening on 1883');
})
You can find the source here: https://github.com/ant-colony/atta/blob/master/sandbox/iot_demo/mqtt-broker.js
- start the broker (the complete sample https://github.com/ant-colony/atta/blob/master/sandbox/iot_demo/), run
npm install
, then./broker.sh
- start the simulation script:
./iot.sh
(you need Atta Project, and you have to build the project jar)
Define connections with Node-RED
First, we have to run Node-RED:
node-red
And now, open your browser http://127.0.0.1:1880/
Define our MQTT connection
- 1: Dragân drop a mqtt input from the left column to the workspace
- 2: Double-Click on the mqtt node to setup the broker and to subscribe to a a topic
Remember, the gateway publishes data on this topic: home/g1/sensors
gateway1
.topic("home/g1/sensors")
.jsonContent(gateway1.lastSensorsData())
.publish(success: {publishToken -> })
- 3: Dragân drop an output (debug) node from the left column to the workspace
- 4: Link the input node and the output node
- 5: Double-Click on the output node to setup the properties (notice the property
to
is set withdebug tab
)
- 6: Now, click on the Deploy button (at the top right corner), and then select the debug tab (in the right column)
- 7: You can do the same thing for the other MQTT gateway (donât forget to deploy to see data of the new gateway):
# Add a log file
I want to store data in a log file:
- 1: Dragân drop a storage file node from the left column to the workspace
- 2: Double-Click on the file node to setup the properties
- 3: Link the file node to the 2 mqtt nodes, and deploy
Now you have a my.iot.log
file with all the data
Define our CoAP connection
- 1: To query a CoAP resource, dragân drop a function âcoap requestâ node from the left column to the workspace
- 2: Double-Click on the coap request node to setup the properties
Remember, the path resource of the CoAP is work
and the CoAP port is 5686
SimpleCoapGateway coapGateway0 = new SimpleCoapGateway(
id:"coapgw0",
coapPort: 5686,
locationName: "Work",
path:"work"
)
- 3: You have to define a trigger to run the request: dragân drop a input âinjectâ node to the workspace
- 4: Double-Click on the trigger node to setup the properties
- 5: Link the trigger node to the CoAP request node
- 6: Create an output (debug) node, link it to the CoAP request node and push the deploy button. Now you can see the CoAP data too:
Thatâs all. Node-RED is a very interesting project, an now Iâm sure that it plays well with Atta (a good test tool for me).
Stay tuned to the next episode. I think, I will play with Aedes and AttA.
Have a nice day!
Tweet