Skip to content

Commit be8dada

Browse files
author
pgillich
committed
Broadcast, tankWrite, resetAll, name resolution
- Send to broadcast address - Separator ; to \n - tankWrite - resetAll - Name resolution - Removed unused UDP features - More documentation
1 parent f31f270 commit be8dada

18 files changed

+209
-76
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.lua linguist-language=Lua

README.md

+107-31
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,41 @@ Example setups:
4040
| | | | WiFi | +--+ +----------+
4141
| | RoboRemo +--------->| Controller |
4242
| | | | | +--+ +----------+
43-
| +------------+ | +------+-------+ +-->| HC-SR04 |
44-
| | | +----------+
45-
| | WiFi |
46-
+------------------+ V
47-
+--------------+
48-
| | +----------+
49-
| Controller +----->| H-bridge |
50-
| | +----------+
51-
+--------------+
43+
| +------------+ | +--------------+ +-->| HC-SR04 |
44+
| | +----------+
45+
| |
46+
+------------------+
47+
48+
49+
+----------+
50+
+------------------+ +--------------+ +-->+ HC|SR04 |
51+
| Tablet, Phone | | +--+ +----------+
52+
| | +--->+ Controller |
53+
| +------------+ | | | +--+ +----------+
54+
| | +-----+ +--------------+ +-->+ H|bridge |
55+
| | RoboRemo | | WiFi +----------+
56+
| | +-----+ +--------------+
57+
| +------------+ | | | | +----------+
58+
| | +--->+ Controller +----->+ H|bridge |
59+
| | UDP | | +----------+
60+
+------------------+ broad +--------------+
61+
-cast
62+
5263
```
5364

5465
## Feature List
5566
Supported Scratch commands:
56-
- [x] `initNet`: Initialize WiFi subnet (only one subnet is supported simultaneously), IP address is a sum of subnet address and device id.
57-
- [x] `pinMode`: NodeMCU command(s): `gpio.mode` or `pwm.setup` + `pwm.start`
58-
- [x] `digitalWrite`: NodeMCU command(s): `gpio.write`
59-
- [x] `analogWrite`: NodeMCU command(s): `pwm.setduty`
60-
- [x] `analogPairWrite`: NodeMCU command(s): `pwm.setduty`
61-
- [ ] `tankWrite`: NodeMCU command(s): `pwm.setduty`
62-
- [ ] `servoWrite`: NodeMCU command(s): `gpio.write`
63-
- [x] `digitalRead`: NodeMCU command(s): `gpio.read`
64-
- [x] `analogRead`: NodeMCU command(s): `adc.read` or custom sensor command
65-
- [ ] `reset_all`: Reset state machine, NodeMCU command(s): `gpio.write`, `pwm.setduty`
66-
- [x] `poll`: return cached values of `digitalRead`, `analogRead`
67+
- [x] `Set network` (`initNet`): Initialize WiFi subnet (only one subnet is supported simultaneously), IP address is a sum of subnet address and device id.
68+
- [x] `set pin` (`pinMode`): NodeMCU command(s): `gpio.mode` or `pwm.setup` + `pwm.start`
69+
- [x] `digital write pin` (`digitalWrite`): NodeMCU command(s): `gpio.write`
70+
- [x] `analog write pin` (`analogWrite`): NodeMCU command(s): `pwm.setduty`
71+
- [x] `analog write pin pair` (`analogPairWrite`): NodeMCU command(s): `pwm.setduty`
72+
- [x] `tank write pin pair` (`tankWrite`): NodeMCU command(s): `pwm.setduty`
73+
- [ ] `servo write pair` (`servoWrite`): NodeMCU command(s): `gpio.write`
74+
- [x] `digital read pin` (`digitalRead`): NodeMCU command(s): `gpio.read`
75+
- [x] `analog read pin` (`analogRead`): NodeMCU command(s): `adc.read` or custom sensor command
76+
- [x] ![Stop](doc/stop-sign-small.png) (`reset_all`): Reset state machine, NodeMCU command(s): `gpio.write`, `pwm.setduty`
77+
- [x] - (`poll`): return cached values of `digitalRead`, `analogRead`
6778

6879
Bridge Features:
6980
- [x] Supporting more NodeMCUs in one WiFi network
@@ -73,14 +84,15 @@ Bridge Features:
7384
- [x] Overload protection by batch command sending (programmatically configured)
7485
- [x] Overload protection by rare poll and caching digitalRead/analogRead values
7586
- [ ] Overload protection by queue size limitation (drop)
76-
- [ ] Name resolution (instead of IP address)
87+
- [x] Name resolution (instead of IP address)
7788
- [ ] Unit tests
7889

7990
Controller Features:
91+
- [x] Basic digital pin handling (mode, high/low, PWM)
8092
- [ ] `analogRead`: `adc.read` registers value to D16 (virtual)
8193
- [x] `analogPairWrite`: transform a [-100,+100] value to 2 pins of H-bridge for a DC motor
82-
- [ ] `tankWrite`: transform a joystick value pair ([-100,+100], [-100,+100]) to pins of H-bridge for 2 DC motor
83-
- [ ] `getName`: returns Bridge name
94+
- [x] `tankWrite`: transform a joystick value pair ([-100,+100], [-100,+100]) to pins of H-bridge for 2 DC motor
95+
- [x] `getName`: returns Bridge name
8496
- [ ] Too small PWM value is overwritten to 0 (for DC motors)
8597
- [x] WiFi station and AP mode
8698
- [x] MAC-based configuration
@@ -90,7 +102,7 @@ Controller Features:
90102
- [ ] Send values back to RoboRemo
91103
- [ ] TCP
92104
- [x] Supporting more NodeMCUs in one WiFi network, for Bridge
93-
- [ ] Supporting more NodeMCUs in one WiFi network, for Roboremo (Controller proxy)
105+
- [x] Supporting more NodeMCUs in one WiFi network, for Roboremo (UDP broadcast)
94106
- [x] HC-SR04 sensor support
95107
- [ ] DHT sensor support
96108
- [ ] BMP180 sensor support
@@ -120,7 +132,7 @@ D8 pin works well with Pololu DRV8833 as B2 input, but activates motor B with ch
120132

121133
HC-SR04 needs 5V power, Echo pin output is 5V, too (3.3V input is good for Trig). A 4k7 with built-in 10k Pull-down resistor behave as a voltage divider, see: [HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi](http://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi).
122134

123-
Pin D4 is used by shields [DHT](http://www.wemos.cc/Products/dht_shield.html) and [DHT Pro](http://www.wemos.cc/Products/dht_pro_shield.html).
135+
Pin D4 is used by WeMos shields [DHT](http://www.wemos.cc/Products/dht_shield.html) and [DHT Pro](http://www.wemos.cc/Products/dht_pro_shield.html).
124136

125137
Other pinout can also be used.
126138

@@ -133,6 +145,29 @@ Copy `secure.lua.example` to `secure.lua` and edit own WiFi authentication confi
133145

134146
[ESPlorer](http://esp8266.ru/esplorer/) can be used to upload Lua files to ESP. Upload all `*.lua` files of directory `lua` to NodeMCU. After reset, NodeMCU will be ready to receive commands and send back input values.
135147

148+
#### Socat
149+
`socat` can be used for testing Controller without any GUIs (Scratch, RoboRemo). Socat can be installed on Cygwin and Linux. Anoter famous program, `netcat`, also can send UDP messages, but cannot send from and receive to same port.
150+
151+
Example for sending commands to a specific IP address (Scratch use case):
152+
```
153+
socat readline UDP4-DATAGRAM:192.168.10.102:9876,bind=:9877
154+
getName
155+
pinMode 4 1
156+
digitalWrite 4 0
157+
digitalWrite 4 0
158+
```
159+
160+
Example for sending commands to broadcast address (RoboRemo use case):
161+
```
162+
socat readline UDP4-DATAGRAM:192.168.10.255:9876,bind=:9877
163+
getName
164+
tank-tower pinMode 4 1
165+
tank-tower digitalWrite 4 0
166+
tank-tower digitalWrite 4 0
167+
```
168+
169+
There are several online portals, where broadcast address can be calculated, for example: [http://www.subnet-calculator.com/](IP Subnet Calculator).
170+
136171
### Bridge
137172
Bridge requires Python 2.7. Command line options will be printed out by `--help` parameter.
138173

@@ -155,26 +190,38 @@ Pyton 2.7 package installation is described at the Linux distributor. Example fo
155190
Install [Scratch 2 Offline Editor](https://scratch.mit.edu/scratch2download). Import ESP42S extension description `src/ESP4S2.s2e` (shift-click on "File" and select "Import Experimental Extension" from the menu). The new extension blocks will appear in the More Blocks palette.
156191

157192
### RoboRemo
158-
[RoboRemo](http://www.roboremo.com) can be installed on Android by [Google Play](https://play.google.com/store/apps/details?id=com.hardcodedjoy.roboremo).
193+
[RoboRemo](http://www.roboremo.com) can be installed for Android by [Google Play](https://play.google.com/store/apps/details?id=com.hardcodedjoy.roboremo).
194+
195+
#### Single Controller
159196
To connect RoboRemo to Controller, use "Internet (UDP)" connection. Example for a connection string: `192.168.10.102:9876`, where the ip and port was set up in `lua/config.lua`.
160197
A button should be created for initialize pins. Example init button configuration for a H-bridged DC motor on pins 5, 6 and a LED on pin 4:
161-
* set press action: `pinMode 4 1\npinMode 5 3\npinMode 6 3`
198+
* set press action (`\n` is also supported instead of Enter):
199+
```
200+
pinMode 4 1
201+
pinMode 5 3
202+
pinMode 6 3
203+
```
162204
* repeat: delay, period = `0`, `only press action`
163205

164206
Example on/off button configuration for a LED on pin 4:
165-
* set press action: `digitalWrite 4 1` or `digitalWrite 4 0`, depending on a pull-up resistor on the pin.
207+
* set press action: `digitalWrite 4 1` or `digitalWrite 4 0`, depending on a pull-up resistor of the pin.
166208
* repeat: delay, period = `0`, `only press action`
167209

168210
Example slider for a H-bridged DC motor on pins 5, 6:
169211
* set id: `analogPairWrite 5 6`
170212
* set min, max: min = `-100`, max = `100`
171-
* send when released (tricky: `send when mover` should be seen)
213+
* send when released (tricky: `send when move` should be seen)
172214
* set repeat period: `500` ms
173215

216+
#### Multiple Controllers
217+
RoboRemo cannot connect to multiple IP addresses. In this case, the boradcast IP address of subnet can be used. For example, if the subnet is 192.168.10.0/24, the broadcast address is 192.168.10.255. There are several online portals, where broadcast address can be calculated, for example: [http://www.subnet-calculator.com/](IP Subnet Calculator). The command sending to this address will be received by all of Controllers. The target Controller name must be marked by the beginning of the command, for example: `tank-tower pinMode 4 1`, `tank-tower pinMode 4 1`, `tank-tower digitalWrite 4 1`. Without marking the Controller name, all of Controller will execute the command.
218+
174219
## Scratch Programer's Guide
175-
After starting Bridge (`src/ESP4S2.py`) and loading ESP42S extension description (`src/ESP4S2.s2e`), Scratch is ready to create block programs. The first block which must be executed is the `Set network`, for example: ![initNet](doc/initNet.jpg).
176-
This block initializes Bridge and requests Controllers to send its names for name resolution. One second must be wait to collect responses from Controllers.
177220

221+
### First steps
222+
After starting Bridge (`src/ESP4S2.py`) and loading ESP42S extension description (`src/ESP4S2.s2e`), Scratch is ready to create block programs. The first block which must be executed is the `Set network`. This block initializes Bridge and requests Controllers to send its names back for name resolution. Example for `Set network` block: ![initNet](doc/initNet.jpg), where `192.168.10.0` is the subnet ID and `24` is the subnet mask bits. There are several online portals, where subnet ID and subnet mask bits can be calculated, for example: [http://www.subnet-calculator.com/](IP Subnet Calculator). One second must be wait to collect responses from Controllers, for example: ![wait2s](doc/wait1s.jpg).
223+
224+
### Blocks
178225
Pin mode must be set before using a pin (`set pin`). A block can be executed immediately (`E`) or with the next (`W`). More blocks can be bundled to one group until the first `E` block. The last block of execution bundle must be `E`. Examples for bundled blocks:
179226
* ![pinMode_tank-tower](doc/pinMode_tank-tower.jpg)
180227
* ![pinMode_tank-chassis](doc/pinMode_tank-chassis.jpg)
@@ -190,3 +237,32 @@ H-bridged DC motors can be controlled by `analog write pin pair` block, for exam
190237
Values can be used by blocks `digital read pin` and `analog read pin`, for example:
191238
* ![digitalRead](doc/digitalRead.jpg)
192239
* ![analogRead_cycle](doc/analogRead_cycle.jpg)
240+
241+
### LED project
242+
It's the "Hello, World!" example of microcontroller world. Built-in blue LED of WeMos is connected to pin `4`. Because of built-in pull-up resistor, LED behaves opposite. After starting Controller and Bridge, please create the below project:
243+
244+
![LED](doc/led.jpg)
245+
246+
Click once on `Set network`. The `192.168.10.0/24` network will be used for communicating to Bridges. It will be valid until Brigde running. Depending on the network setup (WiFi router, ESP AP), the network can be different.
247+
248+
Click on ![Start](doc/green-flag.png). Block `set pin` will set pin `4` to OUTPUT.
249+
250+
When `o` key pressed, pin 4 will be set Low (`0`). Because of built-in pull-up resistor, LED will be turned on. When `x` key pressed, pin 4 will be set High (`1`). Because of built-in pull-up resistor, LED will be turned off.
251+
252+
Click on ![Stop](doc/stop-sign-small.png) to stop all pins.
253+
254+
### PWM project
255+
This project demonstrates PWM. Because of built-in pull-up resistor on pin `4`, LED behaves opposite: it will ligth strongest by setting PWM duty cycle to 0% and LED will be turned off by setting PWM duty cycle 100%. After starting Controller and Bridge, please create the below project:
256+
257+
![PWM](doc/pwm.jpg)
258+
259+
Click once on `Set network`. It will be valid until Brigde running.
260+
261+
Click on ![Start](doc/green-flag.png). Block `set pin` will set pin `4` to PWM.
262+
263+
Press keys 0, 1, 2, 3, 4 to try PWM duty cycles 0%, 50%, 80%, 95%, 100%.
264+
265+
Click on ![Stop](doc/stop-sign-small.png) to stop all pins.
266+
267+
### DC motor project
268+
This project demonstrates DC motor control using PWM and H-bridge.

doc/green-flag.png

753 Bytes
Loading

doc/initNet.jpg

-94 Bytes
Loading

doc/led.jpg

56.5 KB
Loading

doc/pinMode_tank-tower.jpg

-4.42 KB
Loading

doc/pwm.jpg

78.6 KB
Loading

doc/stop-sign-small.png

662 Bytes
Loading

doc/wait1s.jpg

1.89 KB
Loading

lua/config.lua.example

+4-8
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ MAC_config={
1010
static_ip=false,
1111
net_type=net.UDP,
1212
listen={port=ROBOREMO_PORT},
13-
target={addr="192.168.10.103", port=ROBOREMO_PORT},
14-
devices={["hcsr"]={["absorber"]=3,["tmr_ms"]=500}}
13+
devices={["hcsr"]={["absorber"]=2,["tmr_ms"]=500}}
1514
},
1615
["18fe34d40583"]={name="tank-chassis",
1716
wifiMode=wifi.STATION,
1817
ip="192.168.10.103",
1918
static_ip=false,
2019
net_type=net.UDP,
2120
listen={port=ROBOREMO_PORT},
22-
target={addr="", port=ROBOREMO_PORT},
23-
devices={}
21+
devices={}
2422
}
2523
},
2624
["Demo"]={
@@ -31,8 +29,7 @@ MAC_config={
3129
netmask="255.255.255.0",
3230
net_type=net.UDP,
3331
listen={port=ROBOREMO_PORT},
34-
target={addr="192.168.11.103", port=ROBOREMO_PORT},
35-
devices={}
32+
devices={}
3633
},
3734
["18fe34d40583"]={name="tank-chassis",
3835
wifiMode=wifi.SOFTAP,
@@ -41,8 +38,7 @@ MAC_config={
4138
netmask="255.255.255.0",
4239
net_type=net.UDP,
4340
listen={port=ROBOREMO_PORT},
44-
target={addr="", port=ROBOREMO_PORT},
45-
devices={}
41+
devices={}
4642
}
4743
}
4844
}

lua/conn_udp.lua

+1-12
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@ function initUdp(dataReceiver)
33
print("UDP: Init listener :"..config.listen.port)
44
connListener=net.createServer(net.UDP)
55
connListener:on("receive", dataReceiver)
6-
connListener:listen(config.listen.port)
7-
end
8-
if config.target.port>0 and string.len(config.target.addr)>0 then
9-
print("UDP: Init sender "..config.target.addr..":"..config.target.port)
10-
connSender=net.createConnection(net.UDP,0)
11-
connSender:on("sent", function(sck) print("UDP: Sent") end )
12-
connSender:connect(config.target.port, config.target.addr)
6+
connListener:listen(config.listen.port)
137
end
148
end
159

1610
function closeUdp()
1711
print("UDP: Close")
1812
connListener=nil
19-
connSender=nil
2013
end
2114

2215
function sendUdp(body)
2316
connListener(body)
24-
-- if connSender~=nil then
25-
-- print("UDP: Sending "..body)
26-
-- connSender:send(body)
27-
-- end
2817
end
2918

3019
sendBody=sendUdp

lua/hcsr.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
-- http://www.wemos.cc/Products/d1_mini.html
66
-- Sensor pins:
77
-- ECHO: D8 (GPIO15) pulled down by 10k (R2) on WeMos D1 mini
8-
-- R1 between ECHO and D8 as voltage divider: 4k7, see more:
9-
-- http://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi
8+
-- R1 between ECHO and D8 as voltage divider: 4k7, see more:
9+
-- http://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi
1010
-- TRIG: D0 (GPIO16)
1111
-- Can only be used as gpio read/write. No support for open-drain/interrupt/pwm/i2c/ow.
1212
-- VCC: 5V

0 commit comments

Comments
 (0)