From 76398c4dbe6ab2b657294ee05f5716c5689b8763 Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Sat, 24 Oct 2015 20:27:00 +0200
Subject: o Renaming 'sensor' to property. o Adding support for configuring
 wlan SSID and password.

---
 diller/README.md | 56 +++++++++++++++++++--------------------
 diller/main.lua  | 80 ++++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 85 insertions(+), 51 deletions(-)

(limited to 'diller')

diff --git a/diller/README.md b/diller/README.md
index b2d96f8..b454862 100644
--- a/diller/README.md
+++ b/diller/README.md
@@ -4,18 +4,18 @@ Is registration required? Can't the firmware just send an update without a value
 
 # MQTT
 
-## Device / Sensor hierarchy 
-
-	/<device id>
-	  /sensor
-		/<sensor id>
-		  /value
-		  /type				(retained)
-		  /name				(retained)
-		  /description		(retained)
-	  /type                 TODO: remove?
-	  /status               TODO: remove? type specified what the message looks like
-	  /firmware				For updating the device's firmware
+## Device / Property hierarchy
+
+    /<device id>
+      /property
+        /<property id>
+          /value
+          /type             (retained)
+          /name             (retained)
+          /description      (retained)
+      /type                 TODO: remove?
+      /status               TODO: remove? type specified what the message looks like
+      /firmware             For updating the device's firmware
 
 # Diller serial API
 
@@ -25,13 +25,13 @@ Request:
 
 Update or query network settings. If no paramters are given, no changes are done. If ip is set to a blank string, it will use DHCP.
 
-	network [ip=..] [gateway=..] [netmask=..]
+    network [ip=..] [gateway=..] [netmask=..]
 
 Response:
 
 The command will always return the current values.
-	
-	ok ip=.. gateway=.. netmask=.. netmask=..
+    
+    ok ip=.. gateway=.. netmask=.. netmask=..
 
 ## Wlan settings
 
@@ -39,21 +39,21 @@ Request:
 
 Update or query wlan settings. If no paramters are given, no changes are done.
 
-	wlan [ssid=..] [password=..]
+    wlan [ssid=..] [password=..]
 
 Response:
 
 The command will always return the current values.
-	
-	ok ssid=..
+    
+    ok ssid=..
 
 ## Set property value
 
-TODO: Implement description? A longer string describing the sensor.
+TODO: Implement description? A longer string describing the property.
 
 Request:
 
-	set-property id=.. [value=..] [name=..]
+    set-property id=.. [value=..] [name=..]
 
 Type examples:
 
@@ -64,7 +64,7 @@ Type examples:
 
 Response:
 
-	ok
+    ok
 
 The value might not be updated directly, but may be buffered on the device if it is not yet connected.
 
@@ -72,23 +72,23 @@ The value might not be updated directly, but may be buffered on the device if it
 
 Request:
 
-	reset
+    reset
 
 Response
 
-	ok
+    ok
 
 # Example session
 
-	require('main').main();
+    require('main').main();
 
 Get the current network configuration to update the Arduino's LCD display
 
-	> network 
-	< ok ip=1.3.3.7 netmask=255.255.255.0 gateway=1.3.3.1 ssid=awesome
+    > network
+    < ok ip=1.3.3.7 netmask=255.255.255.0 gateway=1.3.3.1 ssid=awesome
 
 Register the properties. This is done on every boot to keep the server in sync with the firmware's features. Old properties will not be removed.
 
-	> set-property id=temp-0 value=12.3 type=temperature name=Water
-	< ok
+    > set-property id=temp-0 value=12.3 type=temperature name=Water
+    < ok
 
diff --git a/diller/main.lua b/diller/main.lua
index 533ecc8..dc1032d 100644
--- a/diller/main.lua
+++ b/diller/main.lua
@@ -11,6 +11,50 @@ local mq = require('mq')
 
 local properties = {}
 
+local function read_cfg(name, required)
+  local filename = "cfg-"..name
+  if not file.open(filename, "r") then
+    if required then
+      panic("Could not read configuration file: "..filename)
+    else
+      return nil
+    end
+  end
+  local value = file.readline()
+  file.close()
+  if value == nil or #value == 0 then
+    if required then
+      panic("Empty configuration file: "..filename)
+    else
+      return nil
+    end
+  end
+  return string.sub(value, 1, -2)
+end
+
+local function write_cfg(name, value)
+  local filename = "cfg-"..name
+  file.open(filename, "w+")
+  local ok = file.writeline(value)
+  file.flush()
+  file.close()
+  return ok
+end
+
+local function configure_wlan()
+  local wlan_ssid = read_cfg("wlan-ssid", false)
+  local wlan_password = read_cfg("wlan-password", false)
+
+  if not wlan_ssid or not wlan_password then
+    print("Missing SSID and/or password configuration, use 'wlan' to configure")
+    return
+  end
+
+  print("Connecting to SSID: "..wlan_ssid)
+  wifi.setmode(wifi.STATION)
+  wifi.sta.config(wlan_ssid, wlan_password)
+end
+
 local function on_cmd(cmd, args)
   if not cmd then
     return
@@ -22,13 +66,20 @@ local function on_cmd(cmd, args)
   end
 
   if cmd == "reset" then
-  print("ok")
+    print("ok reset")
     panic("Reset requested")
   elseif cmd == "wlan" then
     local ssid = args.ssid password = args.password
 
+    local reconfigured = false
     if args.ssid then
-      wifi.sta.config(args.ssid, args.password, 1)
+      write_cfg("wlan-ssid", args.ssid)
+    end
+    if args.password then
+      write_cfg("wlan-password", args.password)
+    end
+    if args.ssid and args.password then
+      configure_wlan()
     end
     ssid, password, bssid_set, bssid = wifi.sta.getconfig()
     print("ok ssid="..(ssid or ''))
@@ -48,9 +99,9 @@ local function on_cmd(cmd, args)
     else
       property = properties[id]
 
-      local value_path = "sensors/"..id.."/value"
-      local name_path = "sensors/"..id.."/name"
-      local description_path = "sensors/"..id.."/description"
+      local value_path = "property/"..id.."/value"
+      local name_path = "property/"..id.."/name"
+      local description_path = "property/"..id.."/description"
 
       if not property then
         print("new property: "..id)
@@ -87,19 +138,6 @@ local function on_cmd(cmd, args)
   end
 end
 
-local function read_cfg(name)
-  local filename = "cfg-"..name
-  if not file.open(filename, "r") then
-    panic("Could not read configuration file: "..filename)
-  end
-  local value = file.readline()
-  file.close()
-  if value == nil or #value == 0 then
-    panic("Empty configuration file: "..filename)
-  end
-  return string.sub(value, 1, -2)
-end
-
 local function print_status()
   print("System Status")
   print("Uptime   : "..tmr.time())
@@ -121,11 +159,7 @@ function P.main()
     mqtt = 2
   }
 
-  local wlan_ssid = read_cfg("wlan-ssid")
-  local wlan_password = read_cfg("wlan-password")
-  print("Connecting to SSID: "..wlan_ssid)
-  wifi.setmode(wifi.STATION)
-  wifi.sta.config(wlan_ssid, wlan_password)
+  configure_wlan(wlan_ssid, wlan_password)
 
   local mac = wifi.sta.getmac()
   local client_id = "esp8266-"..mac
-- 
cgit v1.2.3