1// The driver for DS18B20 chip, it is a temperature sensor. 2 3// Require libjs/lib/onewire.js module. 4var onewire = require('onewire'); 5 6var DS18B20Dev; 7var start_flag = 0; 8 9/* Init DS18B20 10 * 11 * GPIO's options are configured in app.json. Specify the ID, e.g. GPIO below to init DS18B20. 12 { 13 "version": "1.0.0", 14 "io": { 15 "DS18B20": { 16 "type": "GPIO", 17 "port": 4, 18 "dir": "output", 19 "pull": "pullup" 20 } 21 }, 22 "debugLevel": "DEBUG" 23 } 24 25 */ 26function init(gpioid) { 27 DS18B20Dev = onewire.open({id: gpioid}); 28} 29 30// Get temperature 31function getTemperature() 32{ 33 var TL, TH; 34 var tem; 35 36 /* 37 { 38 DS18B20Dev.setspeed(1); 39 start_flag = 1; 40 } 41 42 DS18B20Dev.reset(); 43 44 DS18B20Dev.writeByte(0x55);*/ 45 46 if(1) 47 { 48 if(!start_flag) 49 { 50 DS18B20Dev.setspeed(1); 51 DS18B20Dev.reset(); 52 53 DS18B20Dev.writeByte(0xcc); 54 DS18B20Dev.writeByte(0x44); 55 56 start_flag = 1; 57 } 58 59 60 DS18B20Dev.reset(); 61 62 DS18B20Dev.writeByte(0xcc); 63 DS18B20Dev.writeByte(0xbe); 64 65 TL = DS18B20Dev.readByte(); /* LSB first */ 66 TH = DS18B20Dev.readByte(); 67 68 if (TH > 7) 69 { 70 TH =~ TH; 71 TL =~ TL; 72 tem = TH; 73 tem <<= 8; 74 tem += TL; 75 tem = (tem * 0.0625 * 10 + 0.5); 76 return -tem; 77 } 78 else 79 { 80 tem = TH; 81 tem <<= 8; 82 tem += TL; 83 tem = (tem * 0.0625 * 10 + 0.5); 84 return tem; 85 } 86 } 87 88} 89 90// De-init Si7006 91function deinit() { 92 DS18B20Dev.close(); 93} 94 95module.exports = { 96 init, 97 getTemperature, 98 deinit 99} 100