//Initialize the influxDB settings (no port option because i use the default one)
constinflux=newInflux.InfluxDB({
host:'192.168.1.5',
database:'values_from_sensor',
schema:[{
measurement:'temp',
fields:{
value:Influx.FieldType.FLOAT
},
tags:[
'sensor'
]
}]
})
//RabbitMQ Settings initialization
constrabbitmqSettings={
protocol:'amqp',
hostname:'192.168.1.5',
port:5672,
username:'iotproject',
password:'iotproject',
vhost:'/',
authMechanism:['PLAIN','AMQPLAIN','EXTERNAL']
}
//The main function (connect), implement all the functionality for the purpose of reading broker (tempValues) queue, process the data, write data into influxDB, alert the user and send the recieved data to external an external server...
asyncfunctionconnect(){
constqueue='tempValues';//RabbitMQ queue name...
constsensorName='TMP36GZ';//The name of the sensor
try{
//apply the RabbitMQ settings to create a link between the consumer and the queue...
constconn=awaitamp.connect(rabbitmqSettings);
console.log("Connection Created...");
constchannel=awaitconn.createChannel();
console.log("Channel Created...");
constres=awaitchannel.assertQueue(queue);
console.log("Queue Created...");
console.log(`Waiting for messages from ${queue}`);
//Read the queue value
channel.consume(queue,message=>{
lettempValue=JSON.parse(message.content.toString());//Convert from JSON to javascript object
console.log(`Received the temp value ${tempValue.temperature} from queue`);
//Process only the temperature data
if(tempValue.sensorName==sensorName){
//Delete the message from the queue
channel.ack(message);
console.log('Deleted message from queue...');
//Write the data to influxDB
influx.writePoints([{
measurement:'temp',
tags:{sensor:tempValue.sensorName},
fields:{value:tempValue.temperature},
}]).then(()=>{
console.log("The write into influxDB was successful\n")
})
//Trigger the "handleData" event and send the data to the external server as JSON...