ferdinandkeil.com

Archive for the ‘lua’ tag

Drahtloser Temperatur- und Luftfeuchtesensor mit dem ESP8266

without comments

20150629_224111-600

Über den ESP8266 der chinesischen Firma Espressif ist ja schon an vielen Stellen berichtet worden, aber hier nochmal die Kurzfassung: für ca. 3,– € bringen die verschiedenen ESP8266-Platinen einen Mikrocontroller mit WLAN-Schnittstelle, mehrere MB Flash und diverse Schnittstellen mit. Der Mikrocontroller kann mit eigenen Programmen beschrieben werden und damit eigene Projekte an das Internet anbinden.

Ich habe vor Kurzem ein ESP12-Modul mit passender Adapterplatine geschenkt bekommen. Als ersten Versuch mit diesem neuen Modul habe ich einen drahtlosen Temperatur- und Luftfeuchtesensor gebaut. Als Sensor habe ich den günstigen DHT11 verwendet. Dazu noch ein 3,3 V Linearregler und eine Spannungsquelle – das war’s. Das Programm habe ich in Lua geschrieben und mittels dem genialen NodeMCU Projekt auf dem ESP8266 laufen lassen. Dabei habe ich auf Code von Martin Han aufgebaut.

Der Code setzt die dht_lib ein um den Sensor auszulesen. Diese liegt im Repository von NodeMCU und muss mit auf den ESP8266 hochgeladen werden. Für den Upload des Programms habe ich ein günstiges USB-zu-seriell Modul und den ESPlorer von 4refr0nt benutzt.

-- ***************************************************************************
-- Drahtloser Temperatur- und Luftfeuchtesensor
--
-- von Ferdinand Keil
-- basierend auf Code von Martin Han
--
-- Public Domain
-- ***************************************************************************
--==================================================================
-- HTML Template
--==================================================================
CSS_Sheet=[[<html>
<head>
<meta http-equiv="refresh" content="2">
<style type="text/css">
#sensor {
  font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
  width:800px;
  border-collapse:collapse;
}
#sensor td, #sensor th {
  font-size:1em;
  border:1px solid #98bf21;
  padding:3px 7px 2px 7px;
}
#sensor th {
  font-size:1.1em;
  text-align:left;
  padding-top:5px;
  padding-bottom:4px;
  background-color:#A7C942;
  color:#ffffff;
}
#sensor tr.alt td {
  color:#000000;
  background-color:#EAF2D3;
}
</style>
</head>
<body>
<table id="sensor">
<tr>
<th>Input Source</th>
<th>Temperature</th>
<th>Humidity</th>
</tr>
<tr>
<td>DHT Temp&Humi Sensor</td>
<td>]]
CSS_2=[[ deg C</td>
<td>]]
CSS_3=[[ %</td>
</tr>
</table>
</body>
</html>
]]
--==================================================================
-- Hauptprogramm
--==================================================================
pin = 4 -- Pin mit dem der DHT11 verbunden ist
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASSWORD") -- WLAN Zugangsdaten
wifi.sta.connect()
dht = require("dht_lib")
humi = "Fehler"
temp = "Fehler"
-- Server starten wenn Verbindung steht
tmr.alarm(0,1000,1, function()
  if wifi.sta.getip() ~= nil then
    tmr.stop(0)
    print("NodeMcu's IP Address:"..wifi.sta.getip())
    srv=net.createServer(net.TCP, 10)
    srv:listen(80,function(conn)
      conn:on("receive",function(conn,payload)
        payload = nil
        print("Got request")
        conn:send("HTTP/1.0 200 OK\r\nServer: nodemcu-httpserver\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
        conn:send(CSS_Sheet..temp..CSS_2..humi..CSS_3)
        conn:close()
      end)
    end)
  end
end)
-- regelmäßig den Sensor abfragen
tmr.alarm(1,1000,1, function()
  repeat
    dht.read(pin)
    tmr.wdclr()
  until dht.getHumidity() ~= nil
  temp = dht.getTemperature()/10
  humi = dht.getHumidity()/10
  print("Temperature: "..temp.." deg C, Humidity: "..humi.." %relH")
end)

Sobald sich das Modul mit dem WLAN verbunden hat können die Messwerte über einen Browser angezeigt werden. Dazu muss nur die IP-Adresse die dem Modul zugewiesen wurde aufgerufen werden. Sie Ausgabe sieht dann in etwa so aus:

browser_esp8266_dht11-600

Written by Ferdinand

Juli 1st, 2015 at 21:47

Connect V-USB devices to the Internet with the TL-WR703N – Part 1

without comments

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 object usb.
  • 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.

Written by Ferdinand

März 30th, 2013 at 17:07

Adding Lua support to SyntaxHighlighter Evolved

without comments

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.

Download Icon syntaxhighlighter-brush-lua.zip

Written by Ferdinand

März 30th, 2013 at 16:28