The gateway to be registered on ttn network (gateway registration on TTN).
The hummbox devices to be registered on TTN network (device registration on TTN)
Catch the MQTT message flow from a nodejs client:
Make sure you installed git on your computer. Otherwise install it prior the following. Install git
Then install nodejs
Open the nodejs command prompt and browse to the target directory. then type:
'npm init' to initialize the nodejs project
'npm install twit – -save' to install the tweeter library dependancies
'npm install ttn– -save' to install the ttn library dependancies
If you have difficulties to execute the npm install you most likely did not add Git to your PATH environment variable. If you installed Git via the link above with default options, you should be able to use git from the command line. If it does not work, try running the command from within the Git Bash.
After setting up the dependencies. Now, we will go to dev.twitter.com to generate:
1. consumer_key
2. consumer_secret
3. access_token
4. access_token_secret
These keys and tokens are secret keys and access tokens which give access to use theTwitter API .
-
Go to dev.twitter.com
-
Apply for a tweeter dev acount
- Create your Twitter application
-
Go to the keys and access token, find your keys there
-
create a config.js file with these keys
config.js
module.exports = {
consumer_key: 'QvmKfFtQQpL2NNFbIlXktQzhG ',
consumer_secret: 'hrvxHQqZsWatw8TR6ae3Ds2N0Y1qkeHoCNbGoAP7wOYNdCOvZ5 ',
access_token: '1052128832688463872-LQ53ckXNX9uolKCLYLynrZwdxgeqSM',
access_token_secret: 'fSRMOOtS6fQm0Gptdo0Tp3apT8EgoGavWnvexO0YTJ42g '
}
Finally create the file mqtt2tweeter.js with the following code:
var ttn = require("ttn")
var Twit = require('twit');
var config = require('./config')
var appID = "centralemarseille";
var accessKey = "ttn-account-v2.9822HBdSsOE6fG35h5oZ5hdxgLRb59zMYbyGp744pso";
ttn.data(appID, accessKey)
.then(function (client) {
client.on("uplink", function (devID, payload) {
console.log("Received uplink from ", devID)
console.log(payload)
var co2 = payload.payload_raw[5] + (payload.payload_raw[6]<<8);
console.log(co2);
if (co2 > 1000) {
var T = new Twit(config);
var tweet = {status: 'Please open the window Co2 concentration is too high: '+co2+' ppm !!' } // this is the tweet message
T.post('statuses/update', tweet, tweeted); // this is how we actually post a tweet ,again takes three params 'statuses/update' , tweet message and a call back function
}
})
})
.catch(function (error) {
console.error("Error", error)
process.exit(1)
})
function tweeted(err, data, response) {
//console.log(err);
if(err){
console.log("Something went wrong!");
}
else{
console.log("Voila It worked!");
}
} // this is the call back function which does something if the post was successful or unsuccessful.
Dans la command prompt node.js executer la commande:
node mqtt2tweeter.js
Twitter will prevent spam by publishing only changing message. Meaning that if you got two identical successive messages (same C02 measurement) Twiter will not publish the second one.
Commentaires
0 commentaire
Vous devez vous connecter pour laisser un commentaire.