Parcourir la source

Merge pull request #1 from epukaza/refactor_and_cleanup

Refactor and cleanup
etisab il y a 10 ans
Parent
commit
e5ee59029b

+ 3 - 1
.gitignore

@@ -1,3 +1,5 @@
 .DS_Store
 *.pdf
-*.stl
+*.stl
+/3d_models/*
+/test_certs/*

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 1
BUILD/index.html


+ 44 - 5
BUILD/init.lua

@@ -1,6 +1,45 @@
-startupDelayTimer = 1
-print("3 second startup delay using timer " .. startupDelayTimer .. '...')
-tmr.alarm(startupDelayTimer, 3000, 0, function ()
-  print("Starting.")
-  dofile("interrupt.lua")
+--[[ ASSUMPTIONS
+yorecipient.txt exists
+apikey.txt exists and contains a valid API key
+]] 
+
+DEBUG = true
+SETUP = false
+SETUP_TIMEOUT = 120000
+
+STARTUP_DELAY_TIMER = 0
+INTERRUPT_TIMER = 1
+INDEX_TIMER = 2
+SETUP_INACTIVITY_TIMER = 3
+WIFI_WAIT_TIMER = 4
+SUCCESS_SETUP_TIMER = 5
+
+YO_RECIPIENT = nil
+API_KEY = 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
+
+  debugMsg('Booting button ' .. node.chipid())
+
+  yoRecipientExists = file.open('yorecipient.txt', 'r')
+  YO_RECIPIENT = file.read()
+  file.close()
+  if YO_RECIPIENT then
+    YO_RECIPIENT = string.gsub(YO_RECIPIENT, '\n', '')
+    YO_RECIPIENT = string.gsub(YO_RECIPIENT, ' ', '')
+  end
+  debugMsg('found recipient:' .. tostring(YO_RECIPIENT) .. '.')
+
+  apiKeyExists = file.open('apikey.txt', 'r')
+  API_KEY = file.read()
+  file.close()
+  debugMsg('api key: ' .. API_KEY)
+
+  dofile("interrupt.lua")
 end)

+ 6 - 14
BUILD/interrupt.lua

@@ -1,30 +1,22 @@
 -- using timer 5 for short/long press detection
-longPress = 3000 -- 3 seconds
-buttonPin = 6 -- GPIO6
-DEBUG = true
-SETUP = false
-
-function debugMsg(msg)
-  if DEBUG then
-    print("Yo debug:", msg)
-  end
-end
+local longPress = 3000 -- 3 seconds
+local buttonPin = 6 -- GPIO6
 
 function shortOrLongPress()
-  level = gpio.read(buttonPin)
+  local level = gpio.read(buttonPin)
 
   debugMsg('The pin value has changed to '..gpio.read(buttonPin))
   debugMsg("detected level " .. level)
 
   if level == 1 then -- button depressed
     debugMsg("LONG PRESS TIMER START")
-    tmr.alarm(5, longPress, 0, function()
+    tmr.alarm(INTERRUPT_TIMER, longPress, 0, function()
       debugMsg("LONG PRESS")
       dofile('wifiSetup.lua')
     end)
   else -- button released
     debugMsg("SETUP STATUS " .. tostring(SETUP))
-    tmr.stop(5)
+    tmr.stop(INTERRUPT_TIMER)
     if not SETUP then
       debugMsg("SHORT PRESS")
       dofile('sendYo.lua')
@@ -39,7 +31,7 @@ function debounce (func)
   return function (...)
     local now = tmr.now()
     if now - last < delay then
-      tmr.stop(5)
+      tmr.stop(INTERRUPT_TIMER)
       debugMsg("DEBOUNCE PREVENTED EXTRA PRESS")
       if not SETUP then
         debugMsg("DEBOUNCE INTERPRETED AS SHORT PRESS")

+ 20 - 24
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)
-
-function generateContentString()
-	file.open("yorecipient.txt", "r")
-	return "api_token=0c6ac771-71fa-420f-810c-2853989a8ca6&username="..string.upper(file.readline())
-end
-
 wifi.setmode(wifi.STATION)
 
-contentString = generateContentString()
-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)
-		print("POST REQUEST CALLBACK")
-		print("code: ", code)
-		print("data: ", data)
-		wifi.sleeptype(wifi.LIGHT_SLEEP)
-	end
-)
+if YO_RECIPIENT ~= nil and YO_RECIPIENT ~= '' then
+	debugMsg('sending yo to ' .. YO_RECIPIENT)
+	local contentString = "api_token=" .. API_KEY .. "&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
+	)
+else
+	debugMsg("Yo not sent - invalid YO_RECIPIENT")
+end

+ 68 - 40
BUILD/wifiSetup.lua

@@ -1,9 +1,5 @@
---hoisting globals for easy future removal
-settingsUpdated = false
-srv = nil
-indexTimer = 3
-
 function broadcastAP()
+  SETUP = true
   wifi.setmode(wifi.STATIONAP)
 
   gpio.mode(4, gpio.OUTPUT)
@@ -21,6 +17,7 @@ function broadcastAP()
 end
 
 function stopBroadcastAP()
+  tmr.stop(SETUP_INACTIVITY_TIMER)
   wifi.setmode(wifi.STATION)
   gpio.write(4, gpio.HIGH)
   srv:close()
@@ -28,12 +25,34 @@ function stopBroadcastAP()
   SETUP = false
 end
 
+function restartSetupTimeout(millisec)
+  local ms = millisec or SETUP_TIMEOUT
+  tmr.unregister(SETUP_INACTIVITY_TIMER)
+  tmr.alarm(SETUP_INACTIVITY_TIMER, ms, tmr.ALARM_SINGLE, function()
+      debugMsg("Setup mode timed out")   
+      stopBroadcastAP()
+    end)
+end
+
+function waitForWifiStatus(conn)
+  tmr.alarm(WIFI_WAIT_TIMER, 1000, 1, function()
+    if wifi.sta.status() == 0 or wifi.sta.status() == 1 then
+      debugMsg ("Waiting for Wifi status, currently " .. wifi.sta.status())
+      restartSetupTimeout()
+    else
+      debugMsg("Wifi status: " .. wifi.sta.status())
+      tmr.stop(WIFI_WAIT_TIMER)
+      sendIndex(conn)
+    end
+  end)
+end
+
 function updateSettings(payload)
   if payload then
-    local ssidIndex = {payload:find("ssid=")}
-    local passIndex = {payload:find("&pass=")}
-    local recipientIndex = {payload:find("&recipient=")}
-    local submitIndex = {payload:find("&Submit=")}
+    local ssidIndex = {payload:find("s=")}
+    local passIndex = {payload:find("&p=")}
+    local recipientIndex = {payload:find("&r=")}
+    local submitIndex = {payload:find("&s=")}
 
     if ssidIndex[1] ~= nil then
       local newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
@@ -55,13 +74,14 @@ 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()
 
-      stopBroadcastAP()
-
       return true
     end
   else
@@ -70,40 +90,35 @@ function updateSettings(payload)
 end
 
 function setupServer()
-  if(srv ~= nil) then
-    srv:close()
-    srv = nil
-  end
-  srv = net.createServer(net.TCP)
+  local updated
+  srv = net.createServer(net.TCP, 60)
   srv:listen(80, function(conn)
     conn:on("receive", function(conn, payload)
       debugMsg("request received")
       debugMsg(payload)
-      updateSettings(payload)
-      
-      tmr.alarm(indexTimer, 100, 0, function ()
-        sendIndex(conn)
-      end)
+
+      restartSetupTimeout()
+      updated = updateSettings(payload)
+      waitForWifiStatus(conn)
     end)
 
     conn:on("sent", function(conn)
       conn:close()
+      debugMsg("sent: " .. tostring(updated) .. ' ' .. wifi.sta.status())
+      if updated and wifi.sta.status() == 5 then
+        
+        debugMsg("updated and connected")
+        tmr.alarm(SUCCESS_SETUP_TIMER, 1000, tmr.ALARM_SINGLE, function()
+          debugMsg("closing AP")
+          stopBroadcastAP()
+        end)
+
+      end
     end)
   end)
 end
 
 function sendIndex(conn)
-  file.open('index.html')
-  local indexhtml = file.read()
-  file.close()
-
-  --[[ STATUS CODES:
-  0: STATION_IDLE
-  1: STATION_CONNECTING
-  2: STATION_WRONG_PASSWORD
-  3: STATION_NO_AP_FOUND
-  4: STATION_CONNECT_FAIL
-  5: STATION_GOT_IP ]]
 
   local statusMessages = {}
   statusMessages[0] = 'not enabled'
@@ -113,23 +128,36 @@ 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)
+  local recipient = YO_RECIPIENT
+  if not recipient then
+    recipient = ''
+    debugMsg("recipient: " .. recipient)
+  end
+
+  file.open('index.html')
+  local indexhtml = file.read()
   file.close()
 
-  indexhtml = string.gsub(indexhtml, "_S_", ssid)
-  indexhtml = string.gsub(indexhtml, "_T_", status)
-  indexhtml = string.gsub(indexhtml, "_R_", recipient)
+  indexhtml = string.gsub(indexhtml, "S_", ssid)
+  indexhtml = string.gsub(indexhtml, "T_", status)
+  indexhtml = string.gsub(indexhtml, "R_", recipient)
 
+  debugMsg('sending indexhtml')
+  debugMsg('____________')
+  debugMsg(indexhtml)
+  debugMsg('____________')
   conn:send(indexhtml)
-  
 end
 
 function setupMode()
+  local srv = nil
+
   if SETUP ~= true then
-    SETUP = true    
+    restartSetupTimeout()
     broadcastAP()
     setupServer()
   end

+ 12 - 0
TEST/testHTTPS.py

@@ -0,0 +1,12 @@
+import requests
+import json
+username = 'ARIYEAH'
+api_token = '99c680b7-5f9f-48fd-9459-46cda7e1c8fa'
+
+r = requests.post("https://api.justyo.co/yo/", data={'api_token': api_token, 'username': username})
+
+print r.headers
+
+print ""
+
+print r.json()

+ 0 - 0
nodeMCU firmware version/.gitignore → firmware/.gitignore


BIN
firmware/1.4.0_build_20151006/0x00000.bin


BIN
firmware/1.4.0_build_20151006/0x10000.bin


+ 0 - 0
nodeMCU firmware version/1.5.1/0x00000.bin → firmware/1.5.1/0x00000.bin


+ 0 - 0
nodeMCU firmware version/1.5.1/0x10000.bin → firmware/1.5.1/0x10000.bin


+ 0 - 0
nodeMCU firmware version/nodemcu_integer_0.9.6-dev_20150704.bin → firmware/nodemcu_integer_0.9.6-dev_20150704.bin


+ 10 - 11
index_unminified.html

@@ -27,7 +27,7 @@
       font-size: 50px;
     }
 
-    #h {
+    h1 {
       background-color: #813a9d;
       width: 100%;
       height: 70px;
@@ -44,20 +44,19 @@
     } 
   </style>
   <body>
-  <h1 id="h">Yo Button</h1>
+  <h1>Yo Button</h1>
 
-  <h2>Current settings</h2>
-  <input type="text" value="network: _S_" readonly><br>
-  <input type="text" value="status: _T_" readonly><br>
-  <h2>Will send Yos to</h2>
-  <input type="text" value="_R_" readonly><br>
+  <input type="text" value="network: S_" readonly><br>
+  <input type="text" value="status: T_" readonly><br>
+  <h2>Send Yos to:</h2>
+  <input type="text" value="R_" readonly><br>
 
   <h2>New settings:</h2>
   <form method="post">
-    <input class="n" value="_S_" type="text" name="ssid" autocorrect="off" autocapitalize="none" placeholder="WiFi network"><br>
-    <input class="n" type="text" name="pass" autocorrect="off" autocapitalize="none" placeholder="WiFi password"><br>
-    <input class="n y" value="_R_" type="text" name="recipient" placeholder="Yo recipient"><br><br>
-    <input type="submit" value="Update" name="Submit">
+    <input class="n" value="S_" type="text" name="s" autocorrect="off" autocapitalize="none" placeholder="WiFi network"><br>
+    <input class="n" type="text" name="p" autocorrect="off" autocapitalize="none" placeholder="WiFi password"><br>
+    <input class="n y" value="R_" type="text" name="r" placeholder="Yo recipient"><br><br>
+    <input type="submit" value="Update" name="s">
   </form>
 
   </body>

+ 1 - 0
proto_api_keys/button1/apikey.txt

@@ -0,0 +1 @@
+69cb52f6-fa53-44f7-9723-f4b85bf3b464

+ 1 - 0
proto_api_keys/button2/apikey.txt

@@ -0,0 +1 @@
+03133110-b3a0-4f14-a8cc-466ff2761052

+ 1 - 0
proto_api_keys/button3/apikey.txt

@@ -0,0 +1 @@
+60e5a06e-cfd3-4f87-a3f7-e4f34b16c89a

+ 1 - 0
proto_api_keys/button4/apikey.txt

@@ -0,0 +1 @@
+aba2734b-97a6-4bd0-ac67-b598be331687

+ 1 - 0
proto_api_keys/button5/apikey.txt

@@ -0,0 +1 @@
+3cc8cabe-3290-4f1f-b5db-2d06746c8bee

+ 1 - 0
proto_api_keys/button6/apikey.txt

@@ -0,0 +1 @@
+c8bb435f-70b4-4524-8d2c-91e83dd3ec6c

+ 1 - 0
proto_api_keys/yotouch/apikey.txt

@@ -0,0 +1 @@
+0c6ac771-71fa-420f-810c-2853989a8ca6

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff