Advertisement
Guest User

Untitled

a guest
May 14th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     DS18B20 OneWire temperature sensor test based on mongoose-os/fw/mjs_api/api_onewire.js
  3.  
  4.     Hardware hookup:
  5.  
  6.     .===. top
  7.     |   | front view
  8.     _____
  9.     1 2 3
  10.     | | |_VCC (3-5V)
  11.     | |___Data (need 4.7K to VCC) --> D3 on ESP8266
  12.     |_____GND
  13.  
  14.     mos put init.js && mos call Sys.Reboot && mos console
  15.     curl -m 5 -d '{}' 192.168.0.104/rpc/temperature
  16.     curl -m 5 -d '{"init": true }' 192.168.0.104/rpc/temperature
  17.     curl -m 5 -d '{"device": "28FF66C864150196" }' 192.168.0.104/rpc/temperature
  18. */
  19.  
  20. load('api_config.js');
  21. load('api_gpio.js');
  22. //load('api_sys.js');
  23. //load('api_timer.js');
  24. load('api_rpc.js');
  25. load('api_onewire.js');
  26.  
  27. // Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO
  28. let led = ffi('int get_led_gpio_pin()')();
  29. let relay1 = 12;    // D6
  30. let relay2 = 13;    // D7
  31.  
  32. GPIO.set_mode(led, GPIO.MODE_OUTPUT);
  33. GPIO.set_mode(relay1, GPIO.MODE_OUTPUT);
  34. GPIO.set_mode(relay2, GPIO.MODE_OUTPUT);
  35.  
  36. let tmsPin = 10; // GPIO2 = D4  DS18B20 temperature sensor pin
  37.  
  38. let num2Hex = function(n) {
  39.     let s = '';
  40.     let a = '0123456789ABCDEF';
  41.     let i = (n >> 4) & 0x0F;
  42.     s += a[i];
  43.     i = n & 0x0F;
  44.     s += a[i];
  45. //  s[0] = a + ((a < 10) ? 48 : 55);
  46. //  s[1] = a + ((a < 10) ? 48 : 55);
  47.     return s;
  48. };
  49.  
  50. let dsAddr2Str = function(rs) {
  51.     let s = '';
  52.     for(let i=0; i < rs.length; i++) {
  53.         s += num2Hex(rs.charCodeAt(i));
  54.     }
  55.     return s;
  56. };
  57.  
  58. let dsRom = ['01234567'];
  59. let dsDeviceCnt = 0;
  60.  
  61. // Initialize 1-Wire bus
  62. let owBus = OneWire.init(tmsPin);
  63.  
  64. let searchForDevice = function() {
  65.     let n = 0;
  66.     //Setup the search to find the device type 'DS18B20' (0x28) if it is present
  67.     let req_family_code = 0x28;
  68.     //Normal search mode
  69.     let search_mode = 0;
  70.  
  71.     OneWire.targetSetup(owBus, req_family_code);
  72.  
  73.     while (OneWire.next(owBus, dsRom[n], search_mode)) {
  74.         //If no devices of the desired family are currently on the bus,
  75.         //then another type will be found. We should check it.
  76.         if (dsRom[n][0].charCodeAt(0) !== req_family_code) {
  77.             print('Found unknown device' + JSON.stringify());
  78.             break;
  79.         }
  80.         //DS18B20 found
  81.         print( 'Found ' + dsAddr2Str(dsRom[n]));
  82.         dsRom[++n] = '01234567';
  83.     }
  84.     dsDeviceCnt = n;
  85. };
  86.  
  87. let getTemp = function(devAddress) {
  88.     let data = [];
  89.     let raw;
  90.     let cfg;
  91.  
  92.     OneWire.reset(owBus);
  93.     OneWire.select(owBus, devAddress);
  94.     OneWire.write(owBus, 0x44);
  95.  
  96.     OneWire.delay(750);
  97.  
  98.     OneWire.reset(owBus);
  99.     OneWire.select(owBus, devAddress);
  100.     OneWire.write(owBus, 0xBE);
  101.  
  102.     for (let j = 0; j < 9; j++) {
  103.         data[j] = OneWire.read(owBus);
  104.     }
  105.  
  106.     raw = (data[1] << 8) | data[0];
  107.     cfg = (data[4] & 0x60);
  108.  
  109.     if (cfg === 0x00) {
  110.         raw = raw & ~7;
  111.     } else if (cfg === 0x20) {
  112.         raw = raw & ~3;
  113.     } else if (cfg === 0x40) {
  114.         raw = raw & ~1;
  115.     }
  116.  
  117.     return raw / 16.0;
  118. };
  119.  
  120. RPC.addHandler('temperature', function(args) {
  121.  
  122.    let msg='';
  123.    let device = '*';
  124.    let status = 'ok';
  125.    if (args.device  && args.devices !== '') {
  126.        print("selecting device");
  127.        device = args.device;
  128.    }
  129.  
  130. // Try to get list of attached sensors
  131.    if (args.init) {
  132.        print("init");
  133.    }
  134.     if (dsDeviceCnt === 0 || args.init )
  135.     {
  136.         print('Searching for OneWire DS18B20 devices');
  137.         searchForDevice();
  138.     }
  139. // Stop if no sensor attached
  140.     if (dsDeviceCnt === 0) {
  141.         status = 'No device found';
  142.         print(status);
  143.         return;
  144.     }
  145.    
  146. // Get temperature from each attached sensor
  147.   let d = '';
  148.     for (let i = 0; i < dsDeviceCnt; i++)
  149.     {
  150.         let t = getTemp(dsRom[i]);
  151.         let dsdev = dsAddr2Str(dsRom[i]);
  152.         let topic = 'temperature/ds' + dsdev;
  153.         if (device === '*' || device === dsdev) {
  154.             msg += d + JSON.stringify({'device': dsdev, 'Temperature' : t});
  155.             }
  156.         d = ',';
  157.     }
  158.     msg = '{ "status" :' + '"' + status + '", "devices": [ ' + msg + " ] }";
  159.     print("devices:" , dsDeviceCnt, "msg:", msg);
  160.   return JSON.parse(msg);
  161. });
  162.  
  163. RPC.addHandler('relay', function(args) {
  164.    
  165.    let msg='';
  166.    let relay ='';
  167.    let relaystate ='';
  168.    let a='b';
  169.  
  170.  
  171.   return {'status': 'ok'};
  172. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement