Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. var wifi = require("Wifi");
  2. var http = require('http');
  3. var dht = require('DHT11');
  4. var dhtVysledek = null;
  5.  
  6. E.on('init', function() {
  7. var on = 1;
  8. var blinkInterval = 50;
  9.  
  10. function blink() {
  11. digitalWrite(2, !(on=!on));
  12. setTimeout(blink, blinkInterval);
  13. }
  14. setTimeout(blink, blinkInterval);
  15.  
  16. setInterval(function() {dht.read(function(vysledek) {dhtVysledek = vysledek} ); }, 10000);
  17.  
  18. function doAction(action, params, finishCallback) {
  19. var res = {};
  20.  
  21. if (action == "uptime") {
  22. res.status = 200;
  23. res.seconds = Math.round((new Date()).getTime() / 100) / 10;
  24.  
  25. finishCallback(res);
  26. }else if (action == "setBlinkInterval") {
  27. res.status = 200;
  28. blinkInterval = params.interval;
  29.  
  30. finishCallback(res);
  31. }else if (action == "zapniRele") {
  32. res.status = 200;
  33. if (params.cislo == 1) {
  34. digitalWrite(NodeMCU.D1, 1);
  35. // }else if (params.cislo == 2) {
  36. // digitalWrite(NodeMCU.D8, 1);
  37. // }else if (params.cislo == 3) {
  38. // digitalWrite(NodeMCU.D9, 1);
  39. // }else if (params.cislo == 4) {
  40. // digitalWrite(NodeMCU.D7, 1);
  41. }
  42.  
  43. finishCallback(res);
  44. }else if (action == "vypniRele") {
  45. res.status = 200;
  46. if (params.cislo == 1) {
  47. digitalWrite(NodeMCU.D1, 0);
  48. // }else if (params.cislo == 2) {
  49. // digitalWrite(NodeMCU.D8, 0);
  50. // }else if (params.cislo == 3) {
  51. // digitalWrite(NodeMCU.D9, 0);
  52. // }else if (params.cislo == 4) {
  53. // digitalWrite(NodeMCU.D7, 0);
  54. }
  55.  
  56. finishCallback(res);
  57. }else if (action == "jakaJeTeplota") {
  58. res.status = 200;
  59. res.teplota = dhtVysledek.temp;
  60.  
  61. finishCallback(res);
  62. });
  63.  
  64.  
  65.  
  66. function startHttpServer() {
  67. http.createServer(function(request, response) {
  68. console.log("New http request");
  69. request.on('data',function(data) {
  70. console.log("Request data:" + data);
  71. jsonRequest = JSON.parse(data);
  72. response.writeHead(200, {'Content-Type': 'application/json'});
  73.  
  74. doAction(jsonRequest.method, jsonRequest.params[0], function(res) {
  75. response.write(JSON.stringify({
  76. "id" : jsonRequest.id,
  77. "result" : res
  78. }));
  79.  
  80. response.end();
  81. });
  82.  
  83. });
  84. }).listen(80);
  85. }
  86.  
  87. function mqttConnect() {
  88. var server = "192.168.1.20"; // the ip of your MQTT broker
  89. var options = { // all optional - the defaults are below
  90. client_id : "random", // the client ID sent to MQTT - it's a good idea to define your own static one based on `getSerial()`
  91. keep_alive: 60, // keep alive time in seconds
  92. port: 1883, // port number
  93. clean_session: true,
  94. username: "username", // default is undefined
  95. password: "password", // default is undefined
  96. protocol_name: "MQTT", // or MQIsdp, etc..
  97. protocol_level: 4, // protocol level
  98. };
  99. var mqtt = require("MQTT").create(server, options /*optional*/);
  100.  
  101. mqtt.on('connected', function() {
  102. pinMode(NodeMCU.D5, "input_pullup");
  103. setWatch(function(e) {
  104. console.log("Button pressed");
  105. mqtt.publish("srackomet", "zapnuto cerpadlo");
  106. }, NodeMCU.D5, { repeat: true }
  107. );
  108. });
  109.  
  110. mqtt.connect();
  111. }
  112.  
  113. wifi.connect("ZyXEL", {password:"tajneheslo"}, function(err){
  114. console.log("connected? err=", err, "info=", wifi.getIP());
  115. blinkInterval = 500;
  116. startHttpServer();
  117. mqttConnect();
  118. });
  119. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement