fork download
  1. var mqtt = require('mqtt'), url = require('url');
  2. var user = "USER DEVICES GEEKNESIA";
  3. var pass = "PASS DEVICES GEEKNESIA";
  4. var devID= "DEV ID ";
  5. var apiKey = "API KEY";
  6. var topic = "TOPIC";
  7. var mqtt_url = url.parse('mqtt://'+user+':'+pass+'@geeknesia.com:1883');
  8. var auth = (mqtt_url.auth || ':').split(':');
  9. var five = require("johnny-five");
  10. var Edison = require("edison-io");
  11. var board = new five.Board({
  12. io: new Edison()
  13. });
  14. var gcm = require('node-gcm');
  15.  
  16. function sendNotif(message1,message2){
  17. var message = new gcm.Message({
  18. data: {
  19. title: message1,
  20. message: message2
  21. }
  22. });
  23.  
  24. //Token dari device Android
  25. var regTokens = ['TOKEN DARI LOG ANDROID'];
  26.  
  27. //Replace your developer API key with GCM enabled here
  28. var sender = new gcm.Sender('API KEY GCM');
  29.  
  30. sender.send(message, regTokens, function (err, response) {
  31. if(err) {
  32. console.error(err);
  33. } else {
  34. console.log(response);
  35. }
  36. });
  37. }
  38.  
  39. var ledBiru = new five.Led(13);
  40. var ledKuning = new five.Led(12);
  41. var ledPutih = new five.Led(11);
  42. var temperature = new five.Temperature({
  43. controller: "BMP180",
  44. freq: 5000
  45. });
  46. var motion = new five.Motion({
  47. controller: "HC-SR501",
  48. pin: 7
  49. });
  50. var gas = new five.Sensor("A0");
  51.  
  52. var suhu;
  53. var statusBiru = "Off";
  54. var statusKuning = "Off";
  55. var statusPutih = "Off";
  56. var motionStatus = "On";
  57. var gasStatus = "On";
  58.  
  59. var pushBool = false;
  60. var pushBoolGas = false;
  61.  
  62. var client = mqtt.createClient(mqtt_url.port, mqtt_url.hostname, {
  63. username: auth[0],
  64. password: auth[1]
  65. });
  66.  
  67.  
  68. client.on('connect', function () {
  69. client.publish('iot/live', devID, function() {
  70. console.log("Terhubung ke Geeknesia");
  71. });
  72.  
  73. client.publish('iot/will',devID,function(){
  74. console.log("Aktif");
  75. });
  76.  
  77. client.subscribe(topic, function() {
  78. console.log("Subscribe");
  79. });
  80. });
  81.  
  82. client.on('message', function(topic, message, packet) {
  83. console.log(message.toString());
  84. if(message.toString()=="onBiru"){
  85. //console.log("On Lamp");
  86. statusBiru = "On";
  87. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  88. console.log("updated");
  89. });
  90. ledBiru.on();
  91. }
  92. else if(message.toString()=="offBiru"){
  93. //console.log("Off Lamp");
  94. statusBiru = "Off";
  95. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  96. console.log("updated");
  97. });
  98. ledBiru.off();
  99. }
  100. else if(message.toString()=="onKuning"){
  101. //console.log("On Lamp");
  102. statusKuning = "On";
  103. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  104. console.log("updated");
  105. });
  106. ledKuning.on();
  107. }
  108. else if(message.toString()=="offKuning"){
  109. //console.log("Off Lamp");
  110. statusKuning = "Off";
  111. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  112. console.log("updated");
  113. });
  114. ledKuning.off();
  115. }
  116. else if(message.toString()=="onPutih"){
  117. //console.log("On Lamp");
  118. statusPutih ="On";
  119. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  120. console.log("updated");
  121. });
  122. ledPutih.on();
  123. }
  124. else if(message.toString()=="offPutih"){
  125. //console.log("Off Lamp");
  126. statusPutih ="Off";
  127. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  128. console.log("updated");
  129. });
  130. ledPutih.off();
  131. }
  132. else if(message.toString()=="offAlarmMotion"){
  133. //console.log("Off Lamp");
  134. motionStatus ="Off";
  135. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  136. console.log("updated");
  137. });
  138. }
  139. else if(message.toString()=="onAlarmMotion"){
  140. //console.log("Off Lamp");
  141. motionStatus ="On";
  142. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  143. console.log("updated");
  144. });
  145. }
  146. else if(message.toString()=="offAlarmGas"){
  147. //console.log("Off Lamp");
  148. gasStatus ="Off";
  149. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  150. console.log("updated");
  151. });
  152. }
  153. else if(message.toString()=="onAlarmGas"){
  154. //console.log("Off Lamp");
  155. gasStatus ="On";
  156. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  157. console.log("updated");
  158. });
  159. }
  160. });
  161.  
  162.  
  163. board.on("ready", function() {
  164.  
  165. // "calibrated" occurs once, at the beginning of a session,
  166. motion.on("calibrated", function() {
  167. console.log("calibrated");
  168. });
  169.  
  170. // "motionstart" events are fired when the "calibrated"
  171. // proximal area is disrupted, generally by some form of movement
  172. motion.on("motionstart", function() {
  173. console.log("motionstart");
  174. if (motionStatus=="On"){
  175. //send GCM
  176. sendNotif('Bahaya!!!','Gerakan Terdeteksi!!!');
  177. }
  178. });
  179.  
  180. // "motionend" events are fired following a "motionstart" event
  181. // when no movement has occurred in X ms
  182. motion.on("motionend", function() {
  183. console.log("motionend");
  184. });
  185.  
  186.  
  187. //gas.
  188. gas.scale(0, 100).on("change", function() {
  189. if(gasStatus=="On"){
  190. if (this.value > 25 && !pushBoolGas) {
  191. //console.log("Gas Detected");
  192. pushBoolGas = true;
  193. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Bahaya"}}', function() {
  194. console.log(" celsius : ", suhu);
  195. });
  196. //Send GCM
  197. sendNotif('Bahaya!!!','Gas LPG Terdeteksi!!!');
  198. }else{
  199. pushBoolGas = false;
  200. }
  201. }
  202. });
  203.  
  204.  
  205. temperature.on("change", function() {
  206. suhu = parseFloat(Math.round(this.celsius * 100)/100).toFixed(2);
  207. client.publish('iot/data', '{"code":"'+user+':'+pass+'","attributes":{"Suhu":"'+suhu+' Celcius","LampuBiru":"'+statusBiru+'","LampuKuning":"'+statusKuning+'","LampuPutih":"'+statusPutih+'","MotionStatus":"'+motionStatus+'","GasStatus":"'+gasStatus+'","GasInfo":"Aman"}}', function() {
  208. console.log("updated");
  209. });
  210. });
  211. });
Runtime error #stdin #stdout #stderr 0.58s 323072KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
js: uncaught JavaScript runtime exception: ReferenceError: "require" is not defined.