etisab 10 lat temu
rodzic
commit
7f1a05a23d
5 zmienionych plików z 54 dodań i 47 usunięć
  1. 0 0
      BUILD/index.html
  2. 11 3
      BUILD/init.lua
  3. 0 6
      BUILD/interrupt.lua
  4. 19 23
      BUILD/sendYo.lua
  5. 24 15
      BUILD/wifiSetup.lua

Plik diff jest za duży
+ 0 - 0
BUILD/index.html


+ 11 - 3
BUILD/init.lua

@@ -9,18 +9,26 @@ SETUP_INACTIVITY_TIMER = 3
 WIFI_WAIT_TIMER = 4
 SUCCESS_SETUP_TIMER = 5
 
+YO_RECIPIENT = nil
+
 print("3 second startup delay using timer " .. STARTUP_DELAY_TIMER .. '...')
 tmr.alarm(STARTUP_DELAY_TIMER, 3000, 0, function ()
     print("Starting.")
 
+    function debugMsg(msg)
+      print("Yo debug: " .. msg)
+    end
+
     -- yo recipient file must exist
     yoRecipientExists = file.open('yorecipient.txt', 'r')
-    file.close()
     if yoRecipientExists == nil then
-      file.open('yorecipient.txt', 'w+')
       file.close()
+      file.open('yorecipient.txt', 'w+')
+      file.write('')
+    else
+      YO_RECIPIENT = file.read()
     end
-
+    file.close()
 
     dofile("interrupt.lua")
   end)

+ 0 - 6
BUILD/interrupt.lua

@@ -2,12 +2,6 @@
 local longPress = 3000 -- 3 seconds
 local buttonPin = 6 -- GPIO6
 
-function debugMsg(msg)
-  if DEBUG then
-    print("Yo debug:", msg)
-  end
-end
-
 function shortOrLongPress()
   local level = gpio.read(buttonPin)
 

+ 19 - 23
BUILD/sendYo.lua

@@ -1,27 +1,23 @@
---This code assumes that the WIFI AP has already been set up
-file.open("yorecipient.txt", "r")
-print("sending yo to "..file.readline())
-file.close()
 wifi.sleeptype(wifi.NONE_SLEEP)
 wifi.setmode(wifi.STATION)
 
-function generateContentString()
-	file.open("yorecipient.txt", "r")
-	return "api_token=0c6ac771-71fa-420f-810c-2853989a8ca6&username="..string.upper(file.readline())
-end
-
-local contentString = generateContentString()
-local contentLength = string.len(contentString)
+if YO_RECIPIENT then
+	debugMsg('sending yo to' .. YO_RECIPIENT)
+	local contentString = "api_token=0c6ac771-71fa-420f-810c-2853989a8ca6&username="..string.upper(YO_RECIPIENT)
+	local contentLength = string.len(contentString)
 
-http.post(
-	'https://api.justyo.co/yo/',
-	'Content-Type: application/x-www-form-urlencoded\r\n'..
-	'Content-length: ' ..contentLength.. '\r\n',
-	contentString,
-	function(code, data)
-		debugMsg("POST REQUEST CALLBACK")
-		debugMsg("code: " .. code)
-		debugMsg("data: " .. data)
-		wifi.sleeptype(wifi.LIGHT_SLEEP)
-	end
-)
+	http.post(
+		'https://api.justyo.co/yo/',
+		'Content-Type: application/x-www-form-urlencoded\r\n'..
+		'Content-length: ' .. contentLength .. '\r\n',
+		contentString,
+		function(code, data)
+			debugMsg("POST REQUEST CALLBACK")
+			debugMsg("code: " .. code)
+			debugMsg("data: " .. data)
+			wifi.sleeptype(wifi.LIGHT_SLEEP)
+		end
+	)
+else
+	debugMsg("Yo not sent - invalid YO_RECIPIENT")
+end

+ 24 - 15
BUILD/wifiSetup.lua

@@ -36,14 +36,14 @@ end
 
 function waitForWifiStatus(conn)
   tmr.alarm(WIFI_WAIT_TIMER, 1000, 1, function()
-    if wifi.sta.status() ~= 0 and wifi.sta.status() ~= 1 then
+    if wifi.sta.status() == 0 or wifi.sta.status() == 1 then
+      debugMsg ("Waiting for Wifi status, currently " .. wifi.sta.status())
+      restartSetupTimeout()
+    else
       local newStatus = wifi.sta.status()
       debugMsg("Wifi status: " .. newStatus)
       tmr.stop(WIFI_WAIT_TIMER)
       sendIndex(conn)
-    else
-      debugMsg ("Waiting for Wifi status, currently " .. wifi.sta.status())
-      restartSetupTimeout()
     end
   end)
 end
@@ -75,8 +75,11 @@ function updateSettings(payload)
         return false
       end
 
+      debugMsg("updating settings")
       wifi.sta.config(newssid, newpassword)
-      file.open("yorecipient.txt", "w+")
+
+      YO_RECIPIENT = newrecipient
+      file.open('yorecipient.txt', "w+")
       file.write(newrecipient)
       file.close()
 
@@ -88,24 +91,26 @@ function updateSettings(payload)
 end
 
 function setupServer()
-  srv = net.createServer(net.TCP)
+  srv = net.createServer(net.TCP, 60)
   srv:listen(80, function(conn)
     conn:on("receive", function(conn, payload)
       debugMsg("request received")
       debugMsg(payload)
 
       restartSetupTimeout()
-      updated = updateSettings(payload)
+      local updated = updateSettings(payload)
       waitForWifiStatus(conn)
     end)
 
     conn:on("sent", function(conn)
       conn:close()
-      if updated and wifi.sta.status() == 5 then
+      if updated then
         debugMsg("updated and connected")
-        tmr.alarm(SUCCESS_SETUP_TIMER, 3000, tmr.ALARM_SINGLE, function()
-          debugMsg("closing AP")
-          stopBroadcastAP()
+        tmr.alarm(SUCCESS_SETUP_TIMER, 5000, tmr.ALARM_SINGLE, function()
+          if wifi.sta.status() == 5 then
+            debugMsg("closing AP")
+            stopBroadcastAP()
+          end
         end)
       end
     end)
@@ -122,12 +127,15 @@ function sendIndex(conn)
   statusMessages[4] = 'connection fail'
   statusMessages[5] = 'connected'
 
+  debugMsg('preparing indexhtml')
+
   local ssid = wifi.sta.getconfig()
   local status = statusMessages[wifi.sta.status()]
-
-  file.open("yorecipient.txt", "r")
-  local recipient = string.gsub(file.readline(), "\n", "", 1)
-  file.close()
+  local recipient = YO_RECIPIENT
+  if not recipient then
+    recipient = ''
+    debugMsg("recipient" .. recipient)
+  end
 
   file.open('index.html')
   local indexhtml = file.read()
@@ -137,6 +145,7 @@ function sendIndex(conn)
   indexhtml = string.gsub(indexhtml, "_T_", status)
   indexhtml = string.gsub(indexhtml, "_R_", recipient)
 
+  debugMsg('sending indexhtml')
   conn:send(indexhtml)
 end
 

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików