Skip to content

Commit

Permalink
First functional commit. Lots of features.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgillich committed Sep 25, 2016
1 parent 2bc5172 commit f31f270
Show file tree
Hide file tree
Showing 22 changed files with 1,423 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.README.md.html
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ESP4S2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
8 changes: 8 additions & 0 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}/src</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
183 changes: 127 additions & 56 deletions README.md

Large diffs are not rendered by default.

Binary file added doc/analogPairWrite.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/analogRead_cycle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/analogWrite.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/digitalRead.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/digitalWrite_high.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/digitalWrite_low.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/initNet.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/pinMode_tank-chassis.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/pinMode_tank-tower.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions lua/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/config.lua
/secure.lua
48 changes: 48 additions & 0 deletions lua/config.lua.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
WIFI_CFG_NAME="Home"

ROBOREMO_PORT=9876

MAC_config={
["Home"]={
["5ccf7f0fc2cc"]={name="tank-tower",
wifiMode=wifi.STATION,
ip="192.168.10.102",
static_ip=false,
net_type=net.UDP,
listen={port=ROBOREMO_PORT},
target={addr="192.168.10.103", port=ROBOREMO_PORT},
devices={["hcsr"]={["absorber"]=3,["tmr_ms"]=500}}
},
["18fe34d40583"]={name="tank-chassis",
wifiMode=wifi.STATION,
ip="192.168.10.103",
static_ip=false,
net_type=net.UDP,
listen={port=ROBOREMO_PORT},
target={addr="", port=ROBOREMO_PORT},
devices={}
}
},
["Demo"]={
["5ccf7f0fc2cc"]={name="tank-tower",
wifiMode=wifi.STATION,
ip="192.168.11.102",
static_ip=false,
netmask="255.255.255.0",
net_type=net.UDP,
listen={port=ROBOREMO_PORT},
target={addr="192.168.11.103", port=ROBOREMO_PORT},
devices={}
},
["18fe34d40583"]={name="tank-chassis",
wifiMode=wifi.SOFTAP,
ip="192.168.11.103",
static_ip=false,
netmask="255.255.255.0",
net_type=net.UDP,
listen={port=ROBOREMO_PORT},
target={addr="", port=ROBOREMO_PORT},
devices={}
}
}
}
32 changes: 32 additions & 0 deletions lua/conn_udp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function initUdp(dataReceiver)
if config.listen.port>0 then
print("UDP: Init listener :"..config.listen.port)
connListener=net.createServer(net.UDP)
connListener:on("receive", dataReceiver)
connListener:listen(config.listen.port)
end
if config.target.port>0 and string.len(config.target.addr)>0 then
print("UDP: Init sender "..config.target.addr..":"..config.target.port)
connSender=net.createConnection(net.UDP,0)
connSender:on("sent", function(sck) print("UDP: Sent") end )
connSender:connect(config.target.port, config.target.addr)
end
end

function closeUdp()
print("UDP: Close")
connListener=nil
connSender=nil
end

function sendUdp(body)
connListener(body)
-- if connSender~=nil then
-- print("UDP: Sending "..body)
-- connSender:send(body)
-- end
end

sendBody=sendUdp
initConnection=initUdp
closeConnection=closeUdp
125 changes: 125 additions & 0 deletions lua/hcsr.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
-- HC-SR04 example for NodeMCU
-- Original source from https://github.com/sza2/node_hcsr04
-- main change: replacing tmr.delay to tmr.alert
-- Optimized for WeMos D1 mini, see more:
-- http://www.wemos.cc/Products/d1_mini.html
-- Sensor pins:
-- ECHO: D8 (GPIO15) pulled down by 10k (R2) on WeMos D1 mini
-- R1 between ECHO and D8 as voltage divider: 4k7, see more:
-- http://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi
-- TRIG: D0 (GPIO16)
-- Can only be used as gpio read/write. No support for open-drain/interrupt/pwm/i2c/ow.
-- VCC: 5V
-- GND: G
-- Trig time: min. 10 us
-- Max echo time: 38 ms
-- Usage:
-- dofile("hcsr.lua") device=hcsr.init() device.start()

HCSR_TRIG_DEFAULT = 0
HCSR_ECHO_DEFAULT = 8
HCSR_ABSORBER_DEFAULT = 2
HCSR_TMR_ID_DEFAULT = 6
HCSR_TMR_MS_DEFAULT = 500
HCSR_TRIG_US = 10

hcsr = {};

function hcsr.init(pin_trig, pin_echo, absorber, tmr_id, tmr_ms)
local self = {}
self.tmr_id = tmr_id or HCSR_TMR_ID_DEFAULT
tmr.stop(self.tmr_id)

self.time_start = 0
self.time_end = 0
self.trig = pin_trig or HCSR_TRIG_DEFAULT
self.echo = pin_echo or HCSR_ECHO_DEFAULT
gpio.mode(self.trig, gpio.OUTPUT)
gpio.mode(self.echo, gpio.INT)
self.absorber = absorber or HCSR_ABSORBER_DEFAULT
self.tmr_ms = tmr_ms or HCSR_TMR_MS_DEFAULT
self.last = 0
self.value = 0
self.dus = 0

print("HCSR: trig="..tostring(self.trig)..", echo="..tostring(self.echo)..", absorber="..tostring(self.absorber)..", tmr_id="..tostring(self.tmr_id)..", tmr_ms="..tostring(self.tmr_ms))

function self.sleep(tus)
local start = tmr.now()
self.dus = 0
while(self.dus < tus) do
self.dus = tmr.now() - start
end
end

function self.echo_cb(level)
--print(" self.echo_cb "..level)
if level == 1 then
self.time_start = tmr.now()
gpio.trig(self.echo, "down")
else
self.time_end = tmr.now()
gpio.trig(self.echo, "none")
end
end

function self.sendTrig()
--print(" self.sendTrig")
self.time_start = 0
self.time_end = 0
self.last = 0
gpio.trig(self.echo, "up", self.echo_cb)
gpio.write(self.trig, gpio.HIGH)
self.sleep(HCSR_TRIG_US)
gpio.write(self.trig, gpio.LOW)
end

function self.updateValue()
local delta = -1
if self.time_end > 0 then
delta = self.time_end - self.time_start
if delta < 0 then delta = delta + 2147483647 end
self.last = delta
self.value = self.value + (self.last-self.value)/self.absorber
else
--self.last = -1
--self.value = -1
end

--print("cm="..tostring(self.value/58).." start="..self.time_start.." end="..self.time_end)
if self.value > 0 then
--print(string.rep(" ", self.value/58).."#")
else
--print("-")
end
end

function self.measureWorker()
--print()
self.updateValue()
self.sendTrig()

--print(" "..node.heap())
tmr.start(self.tmr_id)
end

function self.measure()
local status, err = pcall(self.measureWorker)
if not status then
print("HCSR: ERR: "..tostring(err))
end
end

function self.stop()
tmr.stop(self.tmr_id)
end

function self.start()
self.stop()
self.measure()
end

tmr.register(self.tmr_id, self.tmr_ms, tmr.ALARM_SEMI, self.measure)

return self
end
Loading

0 comments on commit f31f270

Please sign in to comment.