Stap 7: Knooppunt server Code
// Include all modules required in your server var Firebase = require('firebase'); var mraa = require("mraa"); var fs = require('fs'); // Create a new Firebase Reference object with your Firebase application url var firebaseRef = new Firebase('.firebaseio.com'); //Initialize values var currentSettings = null; var pushedSettings = null; /********** Trigger message sending interrupt every 20 seconds *************/ var notifier_pin = new mraa.Gpio(5); notifier_pin.dir(mraa.DIR_OUT); // IPC to read data from Arduino Sketch ()example content: 123|45|200|....|0) //Subscribe to interrupt notifications from Arduino var subscriber_pin = new mraa.Gpio(1); subscriber_pin.dir(mraa.DIR_IN); subscriber_pin.isr(mraa.EDGE_RISING, subscriberEvent); //Attach change event listener on Firebase currentSettings value // Everytime the currentSettings value changes the callback will be executed. // Ref: https://www.firebase.com/docs/web/api/query/on.ht... firebaseRef.child('currentSettings').on('value', function(dataSnapShot){ // Get the new settings var data = dataSnapShot.val(); // Set the updated settings in pushed settings pushedSettings = data; //If the lightState is true then we need to switch on the otherwise vice versa if(data.lightState) data.lightState = '1'; else data.lightState = '0'; //Now we need to pass currentSettings value to Arduino to take appropriate action //Since arduino cannot understand Node object we will create a concatenated string with values //and pass it to Arduino using MQTT var arduinoSettingString = 'abcd'+'|'+data.plant+'|'+data.lightState+'|'+data.lightOn+'|'+data .lightOff+'|'+data.moisture+'|'+data.temperature; fs.writeFileSync("/home/root/ipc_codes/js_notification_out.txt", "NodeJS: " + arduinoSettingStr ing + "\n"); // Notify all the subscribers of the MQTT broker notifyWorld(); }); //Fire event to notify all subscribers function notifyWorld() { notifier_pin.write(1); setTimeout(function(){ notifier_pin.write(0); },100); } // Subscribe event of Node server // This event is called when data is sent from Arduino to Node over MQTT function subscriberEvent() { var arduinoSettingString = fs.readFileSync('/home/root/ipc_codes/arduino_notification_out.txt') .toString(); currentSettings = arduinoSettingString.split('|'); // In case tweet setting is true then send a tweet through firebase // This setting is true whenever person touches the Tweet touch sensor if(currentSettings[currentSettings.length -1] === '1') { var tweet = 'Temperature: '+ currentSettings[0] +' DEG | Moisture: '+ currentSettings[1] +' PPM | Light:'+ currentSettings[2]+ ' LUX'; firebaseRef.update({Tweet : tweet}); } //Add a new entry in logs firebaseRef.child('logs').push({ temperature: currentSettings[2], moisture: currentSettings[1], light: currentSettings[0] }); }