Ändern der I2C Adresse eines HYT-131 Sensors
Auf der Suche nach genauen und günstigen Sensoren für meine Humidor Steuerung bin ich auf den HYT-131 von B+B Sensors aufmerksam geworden. Für knapp 20 Euro inkl. MwSt. bekommt man einen maximal ±2,0%relF genauen, kombinierten Temperatur- und Feuchtigkeitssensor. Das ist deutlich günstiger als der Sensirion SHT15 mit vergleichbaren Werten. Ein weiterer Vorteil des Sensors ist, dass er über ein Standard I²C-Interface kommuniziert.
Der I²C-Bus unterstützt bis zu 112 Geräte, vorausgesetzt diese haben unterschiedliche Adressen. Das Datenblatt des HYT-131 erwähnt zwar die Möglichkeit die voreingestellte Adresse zu ändern, erläutert aber nicht wie. Über einen Beitrag im Raspberry Pi Forum wurde ich dann darauf aufmerksam, dass der HYT-131 genauso wie der HYT-221 und HYT-271 auf einem IC von ZMDI basieren. Der IC heißt ZSSC3122, das Datenblatt habe ich angehängt. Im Datenblatt zu diesem IC finden sich allerhand Informationen, darunter wie die I²C-Adresse geändert werden kann. Der Ablauf ist wie folgt:
- Sobald der Sensor mit der Versorgungsspannung verbunden wurde muss innerhalb von 3-10 ms (abhängig von der internen Konfiguration) ein Befehl gesendet werden um in den Command Mode zu gelangen.
- In diesem Modus kann der interne EEPROM beschrieben werden, wobei die I²C-Adresse im Register Cust_Config hinterlegt ist.
Um diesen Vorgang automatisch und zuverlässig durchführen zu können habe ich eine Arduino Sketch geschrieben. Die bisherige (default 0×28) und neue Adresse werden über defines angegeben. Der Sensor wird mit dem Arduino verbunden und dieser mit der Sketch programmiert. Dann muss nur noch der Serielle Monitor gestartet und ein beliebiges Zeichen gesendet werden um den Prozess zu starten.
Bei den Sensoren die mir von B+B Sensors geliefert worden sind war das Comm_lock Flag im Register ZMDI_Config nicht gesetzt. Dadurch antwortet der Sensor auf allen I²C-Adressen. Die Sketch setzt dieses Flag, da sonst nicht getestet werden kann ob die Änderung der Adresse erfolgreich war. Mit der enthaltenen Funktion changeCommLock(...) kann diese Änderung aber auch wieder rückgängig gemacht werden.
Hinweis: Falls ein Fehler auftritt ist der Sensor unter Umständen weder unter seiner alten noch unter der neuen Adresse ansprechbar. In diesem Fall hilft es den gesamten I²C-Adressraum zu durchlaufen und auf Geräte zu prüfen.
Connect V-USB devices to the Internet with the TL-WR703N – Part 1
Note: This article is a follow-up to Use the TP-LINK TL-WR703N as a WiFi client with OpenWrt.
The reason why I bought the TL-WR703N orignally was to connect my cigar humidifier circuit to the internet. In the last article I established a WiFi connection to my local network. Now I needed a way to talk to my circuit over USB. It includes a USB port, which I used so far to update the firmware. So all that was left was some firmware based on V-USB and a script/program on the router side talking to it.
I soon found out, that the easiest way to get talking to USB devices was using Lua with libusb. However, the original OpenWrt package for the TL-WR703N doesn’t include a libusb binding for Lua. I’m not going to compile OpenWrt, so… to the Googles! As it happenend to be, Madox had already done all the hard work for me. He posted several firmware images for the TL-WR703N, of which I chose the Standard version. Updating OpenWrt given a proper firmware image is really easy, so I won’t go into detail here.
Now I needed a known-good firmware for my circuit to get started developing Lua scripts. Objective Development, the people behind V-USB, developed several example firmwares demonstrating their library. I chose the PowerSwitch, as it comes with a command-line utility that works under Windows.
I then adapted the firmware for my needs. In the end it would only toggle one PIN which is connected to an LED. To make sure it’s working, I then connected the device to my PC and ran the command-line utility. It worked flawlessly first try. Now, I went on to duplicate this functionality in Lua. The source for the command-line utility is included with the PowerSwitch example, so I started from that. The resulting Lua script is shown below. The script does not implement all functions of the PowerSwitch. It only works on port 0 and can not return the status of the device.
-- usage: lua powerswitch.lua on|off [duration]
-- duration (in seconds) is optional
PSCMD_STATUS = 1
PSCMD_ON = 2
PSCMD_OFF = 3
usb = require('libusb1')
local handle = usb.open_device_with_vid_pid(0x16C0, 0x05DC);
if (handle~=nil) then
print('Device initialised with success !!')
local port = 0
local requesttype = usb.LIBUSB_REQUEST_TYPE_VENDOR + usb.LIBUSB_RECIPIENT_DEVICE + usb.LIBUSB_ENDPOINT_IN
local request = PSCMD_STATUS
if (arg[1] == 'on') then
request = PSCMD_ON
end
if (arg[1] == 'off') then
request = PSCMD_OFF
end
local duration = 0
if (arg[2] ~= nil) then
duration = arg[2] * 5
end
usb.control_transfer(handle, requesttype, request, duration, port, 8, 5000)
else
print('Device initialisation failed !!')
end
There is not much documentation available on how to use libusb with Lua. I first looked at the Interfacing with Lua tutorial for the Pinguino, but soon found out it was written for a different libusb binding. The binding for Lua included in Madox’s package is lualibusb1, for which a function reference is available. Some important things to know about lualibusb1 are:
open_device_with_vid_pid([ctx, ] vendorid, productid)is implemented, which makes finding the right device really easy.- When requiring the library, type
usb = require('libusb1')and then work with the objectusb. - Constants from libusb are available, but some have different names. All are prefixed with LIBUSB_, but I still had to look up the names in the source of lualibusb1.
One important thing I almost forgot to mention: it is not possible to hook up a V-USB device directly to a TL-WR703N. That is because the TL-WR703N only supports USB2.0 or high-speed devices. So the V-USB device has to be connected to a USB hub, which is in turn connected to the router. Any cheap USB2.0 hub will do, I used this one: LOGILINK UA0136.
Adding Lua support to SyntaxHighlighter Evolved
Following the tutorial on adding a new language to the SyntaxHighlighter Evolved Plugin for WordPress, I generated a plugin for the Lua language. The SyntaxHighlighter brush I used was created by HyukYi, Kwon.
The plugin can be installed through the WordPress admin panel.
