Index: python/webiopi.py =================================================================== --- python/webiopi.py (revision 605) +++ python/webiopi.py (working copy) @@ -24,6 +24,7 @@ import mimetypes as mime import re import base64 +import subprocess import _webiopi.GPIO as GPIO try: import BaseHTTPServer @@ -37,7 +38,8 @@ "I2C0": {"enabled": False, "gpio": {0:"SDA", 1:"SCL"}}, "I2C1": {"enabled": True, "gpio": {2:"SDA", 3:"SCL"}}, "SPI0": {"enabled": False, "gpio": {7:"CE1", 8:"CE0", 9:"MISO", 10:"MOSI", 11:"SCLK"}}, - "UART0": {"enabled": True, "gpio": {14:"TX", 15:"RX"}} + "UART0": {"enabled": True, "gpio": {14:"TX", 15:"RX"}}, + "ONEWIRE": {"enabled": False, "gpio": {4:"ONEWIRE"}} } MAPPING = [[], [], []] @@ -85,6 +87,10 @@ self.callbacks[callback.__name__] = callback def writeJSON(self, out): + if GPIO.getFunction(4) == GPIO.ALT0: + FUNCTIONS['ONEWIRE']['enabled'] = True + else: + FUNCTIONS['ONEWIRE']['enabled'] = False json = "{" first = True for (alt, value) in FUNCTIONS.items(): @@ -111,7 +117,7 @@ json += "\n}}" out.write(json.encode()) - + def run(self): host = "[RaspberryIP]" s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -143,6 +149,12 @@ def version_string(self): return SERVER_VERSION + ' ' + self.sys_version + + def onewireDevices(self): + devices = os.listdir("/sys/bus/w1/devices/") + devices.remove('w1_bus_master1') + return "%s" % devices + def checkAuthentication(self): auth = self.headers.get('Authorization') @@ -182,6 +194,31 @@ self.end_headers() self.wfile.write(SERVER_VERSION.encode()) + # List onewire devices + elif relativePath == "onewire/devicelist": + if GPIO.getFunction(4) == GPIO.ALT0: + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write(self.onewireDevices().replace("'", '"').encode()) + else: + self.send_error(404, "Onewire not enabled") + elif (relativePath.startswith("onewire/device/")): + (mode, operation, device) = relativePath.split("/") + try: + tfile = open("/sys/bus/w1/devices/"+device+"/w1_slave") + text = tfile.read() + tfile.close() + except(IOError): + self.send_response(404) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write("No device: "+device) + else: + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.wfile.write(text) # Single GPIO getter elif (relativePath.startswith("GPIO/")): (mode, s_gpio, operation) = relativePath.split("/") @@ -375,6 +412,21 @@ else: self.send_error(404, fname + " Not Found") return + elif (relativePath.startswith("onewire/")): + (mode, operation) = relativePath.split("/") + if (operation == "enable"): + subprocess.call(['modprobe', 'w1-gpio']) + subprocess.call(['modprobe', 'w1_therm']) + self.send_response(200) + self.send_header("Content-type", "text/plain"); + self.end_headers() + self.wfile.write(operation.encode()) + if (operation == "disable"): + subprocess.call(['modprobe', '-r', 'w1_gpio', 'w1_therm']) + self.send_response(200) + self.send_header("Content-type", "text/plain"); + self.end_headers() + self.wfile.write(operation.encode()) else: # path unknowns self.send_error(404, "Not Found") Index: python/native/gpio.c =================================================================== --- python/native/gpio.c (revision 605) +++ python/native/gpio.c (working copy) @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -140,6 +141,7 @@ if ((value == OUT) && isPWMEnabled(gpio)) { value = PWM; } + if ((gpio == 4) && oneWireEnabled()) value = ALT0; return value; // 0=input, 1=output, 4=alt0 } @@ -322,7 +324,21 @@ return gpio_threads[gpio] != NULL; } +//added Stuart Marsden +char* oneWireEnabled() +{ + FILE *f = fopen("/proc/modules", "r"); + char *bytes = malloc(10000); // No way to get real size in /proc shold be large enough + char *pos; + fread(bytes, 10000, sizeof(char), f); + fclose(f); + pos = strstr(bytes, "w1_gpio "); + free(bytes); + return pos; +} + + void cleanup(void) { // fixme - set all gpios back to input Index: python/native/gpio.h =================================================================== --- python/native/gpio.h (revision 605) +++ python/native/gpio.h (working copy) @@ -69,6 +69,7 @@ void enablePWM(int gpio); void disablePWM(int gpio); int isPWMEnabled(int gpio); +char* oneWireEnabled(void); void cleanup(void); Index: htdocs/webiopi.css =================================================================== --- htdocs/webiopi.css (revision 605) +++ htdocs/webiopi.css (working copy) @@ -11,6 +11,10 @@ padding: 0 0 0 0; } +.oneWireList button{ + width: auto; +} + input[type="range"] { -webkit-appearance: slider-horizontal; } Index: htdocs/webiopi.js =================================================================== --- htdocs/webiopi.js (revision 605) +++ htdocs/webiopi.js (working copy) @@ -17,6 +17,10 @@ var _gaq = _gaq || []; var _webiopi; +Array.prototype.diff = function(a) { + return this.filter(function(i) {return !(a.indexOf(i) > -1);}); +}; + function w() { if (_webiopi == undefined) { _webiopi = new WebIOPi(); @@ -46,7 +50,8 @@ I2C0: {name: "I2C0", enabled: false, gpios: []}, I2C1: {name: "I2C1", enabled: false, gpios: []}, SPI0: {name: "SPI0", enabled: false, gpios: []}, - UART0: {name: "UART0", enabled: false, gpios: []} + UART0: {name: "UART0", enabled: false, gpios: []}, + ONEWIRE: {name: "ONEWIRE", enabled: false, gpios: []} }; // init GPIOs @@ -119,6 +124,7 @@ this.addALT(this.ALT.UART0, 14, "TX"); this.addALT(this.ALT.UART0, 15, "RX"); + this.addALT(this.ALT.ONEWIRE, 4, "DATA"); } WebIOPi.prototype.init = function() { @@ -217,7 +223,7 @@ w().updateALT(w().ALT.I2C1, data["I2C1"]); w().updateALT(w().ALT.SPI0, data["SPI0"]); w().updateALT(w().ALT.UART0, data["UART0"]); - + w().updateALT(w().ALT.ONEWIRE, data["ONEWIRE"]); $.each(data["GPIO"], function(gpio, data) { w().updateFunction(gpio, data["function"]); if ((data["function"] == "IN") || (data["function"] == "OUT")) { @@ -230,6 +236,12 @@ }); }); + if (w().ALT['ONEWIRE'].enabled) { + $('.oneWireList').show() + w().updateOneWireList(); + } else { + $('.oneWireList').hide(); + } setTimeout(w().updateUI, 1000); } @@ -409,6 +421,62 @@ return button; } +WebIOPi.prototype.addToOneWireList = function (newDevices, selectors) { + $.each(newDevices, function(key, val) { + $('').appendTo(selectors); + }); +} + +WebIOPi.prototype.updateOneWireList = function () { + var selectors = $('.oneWireList select'); + var options = selectors.find('option'); + var values = $.map(options, function(elt, i) { return $(elt).val();}); + $.getJSON(w().context + "onewire/devicelist", function(data) { + var newDevices = data.diff(values); + var oldDevices = values.diff(data); + w().addToOneWireList(newDevices, selectors); + $.each(oldDevices, function(key, val) { + $('.oneWireList select option[value="' + val + '"]').remove();; + }); + + }); + +} + +WebIOPi.prototype.createOneWireSelector = function (id) { + var div = $('
'); + div.attr("id", id); + div.addClass("oneWireList"); + var selector = $('Extract Temperature
'); + div.append(checkbox); + var valueOneWire = $('
'); + div.append(valueOneWire); + + button.bind("click", function(event) { + $.get("onewire/device/"+selector.val(), + function(returned_data) + { + if (checkbox.is(':checked')) { + valueOneWire.text(returned_data.split('t=')[1] / 1000); + } else { + valueOneWire.text(returned_data); + } + }); + }); + + $.getJSON(w().context + "onewire/devicelist", function(data) { + w().addToOneWireList(data, selector); + }); + w().updateOneWireList(); + return div; +} + WebIOPi.prototype.createRatioSlider = function(gpio) { var slider = $(''); slider.attr("id", "ratio"+gpio); @@ -501,8 +569,9 @@ if (containerId != undefined) { $("#"+containerId).append(table); + $("#"+containerId).append(w().createOneWireSelector('oneWire')); } - + return table; } Index: doc/RESTAPI =================================================================== --- doc/RESTAPI (revision 605) +++ doc/RESTAPI (working copy) @@ -80,4 +80,19 @@ }} "UART0": 1, "I2C0": 0, "I2C1": 1, "SPI0": 0 mean that both UART0 and I2C1 are enabled, whereas both I2C0 and SPI0 are disabled. So GPIOs used by UART0 (14 and 15) and I2C1 (2 and 3) are disabled and unusable. - \ No newline at end of file +=Get a list of all one wire devices + HTTP GET /onewire/devicelist + Returns list of all devices: + ["28-00000439ee64", 28-00000123ab45", 28-00000678cd90"] + +=Get value from one wire device + HTTP GET /onewire/device/(device Id) + Returns raw result: + 7f 01 4b 46 7f ff 01 10 33 : crc=33 YES + 7f 01 4b 46 7f ff 01 10 33 t=23937 + +=Enable OneWire GPIO= + HTTP POST /onewire/enable + +=Disable OneWire GPIO= + HTTP POST /onewire/disable \ No newline at end of file