etisab 10 лет назад
Родитель
Сommit
94bf735260
6 измененных файлов с 169 добавлено и 0 удалено
  1. 7 0
      BUILD/init.lua
  2. 29 0
      BUILD/interrupt.lua
  3. 31 0
      BUILD/sendYo.lua
  4. 4 0
      BUILD/testMQTT.lua
  5. 98 0
      BUILD/wifiSetup.lua
  6. 0 0
      BUILD/yorecipient.txt

+ 7 - 0
BUILD/init.lua

@@ -0,0 +1,7 @@
+tmr.alarm(1, 3000, 0, 
+
+function ()
+	print("Starting delayedInit")
+	dofile("interrupt.lua")
+end
+)

+ 29 - 0
BUILD/interrupt.lua

@@ -0,0 +1,29 @@
+gpio.mode(6, gpio.INT, gpio.PULLUP)
+delay = 0
+setupStarted = 0
+function buttonHandler(level)
+   x = tmr.now()
+   if x > delay then
+   	  --button has been debounced
+      delay = tmr.now()+50000
+      tmr.delay(2000)
+      level = gpio.read(6)
+      print("handler called with level: "..level)
+      if level == 0 then
+      	--button pressed down initially
+      	tmr.alarm(4, 2000, 0, function()
+			setupStarted = 1
+			dofile("wifiSetup.lua")
+			end
+		)
+	  else
+	    tmr.stop(4)
+		--button released
+		if setupStarted == 0 then
+  	      dofile("sendYo.lua")
+        end
+      end
+   end
+end
+
+gpio.trig(6, "both",buttonHandler)

+ 31 - 0
BUILD/sendYo.lua

@@ -0,0 +1,31 @@
+--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)
+sk = nil
+sk = net.createConnection(net.TCP,0)
+sk:on("receive", function(sck, c)
+    print(c)
+    wifi.sleeptype(wifi.MODEM_SLEEP)
+    end)
+    
+sk:on("connection", function(sk, payload)
+						contentString = generateContentString()
+						strlen = string.len(contentString)
+						sk:send("POST /yo/ HTTP/1.1\r\n"
+							.."Host: api.justyo.co\r\n"
+							.."Connection: close\r\n"
+							.."Content-Length: "..strlen.."\r\n"
+							.."Accept: */*\r\n"
+							.."Content-type: application/x-www-form-urlencoded\r\n\r\n"
+							..contentString)
+					end)
+sk:connect(80, "api.justyo.co")

+ 4 - 0
BUILD/testMQTT.lua

@@ -0,0 +1,4 @@
+-- Test simple unencrypted MQTT connection using Mosquitto test server
+m = mqtt.Client("clientid", 120, "user", "password")
+m:connect("192.168.1.13", 1883, 0, function(conn) print("connected!") end)
+m:on("connect", function(con) print("actually connected!") end)

+ 98 - 0
BUILD/wifiSetup.lua

@@ -0,0 +1,98 @@
+function startSetup()
+  wifi.sta.disconnect()
+  print("Wifi disconnected")
+  wifi.setmode(wifi.STATIONAP)
+  stationConfig = {}
+  stationConfig.ssid = "YoButtonSetup"
+  stationConfig.pwd = "password"
+  stationConfig.auth = wifi.OPEN
+  wifi.ap.config(stationConfig)
+  wifi.ap.dhcp.start()
+  print("Wifi station set up")
+  setupServerResponses()
+end
+
+function updateWithNewValues(payload)
+      ssidIndex = {payload:find("newssid=")}
+      passIndex = {payload:find("&newpass=")}
+      recipientIndex = {payload:find("&newrecipient=")}
+      submitIndex = {payload:find("&Submit=")}
+
+      if(ssidIndex[1]~=nil and payload:find("?")~=nil) then
+        print(ssidIndex[1]..", "..ssidIndex[2])
+        print(passIndex[1]..", "..passIndex[2])
+        print(recipientIndex[1]..", "..recipientIndex[2])
+        print(submitIndex[1]..", "..submitIndex[2])
+        newssid = string.gsub(string.sub(payload, ssidIndex[2]+1, passIndex[1]-1), "+", " ")
+        newpassword = string.gsub(string.sub(payload, passIndex[2]+1, recipientIndex[1]-1), "+", " ")
+        newrecipient = string.upper(string.sub(payload, recipientIndex[2]+1, submitIndex[1]-1))
+        print(newssid)
+        print(newpassword)
+        print(newrecipient)
+        wifi.sta.config(newssid, newpassword)
+        file.open("yorecipient.txt", "w+")
+        file.write(newrecipient)
+        file.close()
+      end
+      setupStarted=0
+end
+
+function serveFile(c, f)
+  
+end
+
+function setupServerResponses()
+  if(srv~=nil) then
+    srv:close()
+    srv=nil
+  end
+  srv=net.createServer(net.TCP)
+  srv:listen(80,function(conn)
+    conn:on("receive",function(conn,payload)
+      print("request received")
+      ind = {string.find(payload, "\n")}
+      if(ind[1] ~= nil)then
+        payload = string.sub(payload, 1, ind[1])
+      end
+      --payload is reduced to the first line
+      print(payload)
+      --[[requestedFile = string.match(payload, "/(.+) HTTP")
+      if file.open(requestedFile, "r") then
+        serveFile(conn, requestedFile)
+        return
+      end
+      --]]
+      updateWithNewValues(payload)
+
+      file.open("yorecipient.txt", "r")
+      recipient = string.gsub(file.readline(), "\n", "", 1)
+      file.close()
+
+      ssid, password = wifi.sta.getconfig()
+      ip = wifi.sta.getip()
+      if(ip==nil) then
+        ip="0.0.0.0"
+      end
+      conn:send("<body><h1>YO Button setup</h1>"
+      .."Current wifi SSID: <br>"
+      .."<input type=\"text\" value=\""..ssid .."\" readonly><br>"
+      .."Current wifi password: <br>"
+      .."<input type=\"text\" value=\""..password .."\" readonly><br>"
+      .."Yo Button's IP address: <br>"
+      .."<input type=\"text\" value=\""..ip.."\" readonly><br>"
+      .."Yo recipient: <br>"
+      .."<input type=\"text\" value=\""..recipient.."\" readonly><br>"
+      .."<form>New SSID: <br>"
+      .."<input type=\"text\" name=\"newssid\" value=\""..ssid .."\"><br>"
+      .."New password: <br>"
+      .."<input type=\"text\" name=\"newpass\" value=\""..password .."\"><br>"
+      .."New recipient: <br>"
+      .."<input type=\"text\" name=\"newrecipient\" value=\""..recipient.."\"><br>"
+      .."<input type=\"submit\" name=\"Submit\">"
+      .."</form></body>")
+    end)
+    conn:on("sent",function(conn) conn:close() end)
+  end)
+end
+
+startSetup()

+ 0 - 0
BUILD/yorecipient.txt