Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By Electroguard
#45069 Ok picstart... but between you aking the question and then changing it, I have done you an answer - so I'll post it anyway in case it answers anyone elses questions.

The intention is to create a simple non-technical way for nodes to communicate with each other, and the crucial thing with udp is that if you send to node address 255 it broadcasts the msg to all nodes.

So if a node needs to send a msg to another node it shouts out a plain text msg for all to hear, rather than needing to know everyones individual phone numbers to make point to point calls. The broadcasts are targeted solely by their contents which includes the target name. Names are just arbirarilly assigned unique monikers that get parsed by all, so can be whatever best suits purposes.

Lets say the ESP Basic udp network had nodes called Peter, Paul and Mary - Peter would send msgs to Mary beginning with her name followed by whatever he wanted to say.
Paul would get the msg and ignore it because it didn't start with his name, but Mary would recognise hers and know that the rest of the msg was for her.

If Peter sends to Mary and gets her attention, the msg actually contains his unique return network address allowing her to acknowledge with private love and kisses if she wishes.

Mary could even send msgs to both the boys at the same time by using just the common part of their names "P", which they could both recognise when parsing the msg.

So a bit of thought about a naming convention allows partial-name groups to be addressed, as well as individuals, and of course everyone.

You might get a better idea of things from this draft 'common' network framework I just started working on today - but don't even bother trying to run it, cos it's pure incomplete untested theory from someone with 1 weeks ESP Basic hands-on and no way to actually start testing and debugging yet... so it'll have more bugs in it than a wasp nest. Hopefully it'll show what's intended though.


Code: Select all'let local_name = "default"   ' Default node name must be changed to something unique (currently not case sensitive)
let local_name = "MP3lounge"  ' Unique node name
let system = "All"            ' Allows different system groups of nodes to be addressed separately if required
udpbegin 5001
udpbranch [udpmsg]
wait
end

[udpmsg] 
msg = udpread()
print "UDP msg received: " & msg                                         ' For test purposes only
if left(upper(msg), len(name)) == upper(name) then goto [local_commands] ' handle specifically local "name" commands
if instr(upper(msg), upper(name)) > 0 then goto [group_commands]         ' handle partial-name group commands
if left(upper(msg), 3) == upper(system) then goto [global_commands]      ' handle this global "system"s node commands
print "msg not recognised by " & name                                    ' For test purposes only
udpreply name & " ERROR: unrecognised msg: " & msg
return

[global_commands] 
' (Sent prefixed by system, eg: "All ")
if instr(upper(msg), upper("?")) then goto [declare]
if instr(upper(msg), upper("Report")) then goto [report]   'change [branch] to report back something else
if instr(upper(msg), upper("Blink")) then goto [blink]     'for physically spotting the "system" nodes
if instr(upper(msg), upper("FlashIP")) then goto [flashIP] 'for physically identifying node IP addresses
if instr(upper(msg), upper("Reset")) then goto [reset]     'to reboot the complete "system"
udpreply name & " ERROR: no recognised global command in msg: " & msg
returm

[group_commands]
' (Sent to partial names, eg: "PIR ?" would be recognised by PIR1 and PIR2 and PIR3 etc)
print "Checking Group commands"        'For test purposes only
if instr(upper(msg), upper("GroupTest")) then print "Group test"
' if only interested in specific Group commands (partial local name) then uncomment return
' else drop through to also action local_commands
'return

[local_commands]
' (Sent to a specific unique node name)
  print "Checking Local commands"         'For test purposes only

' Unique commands for this local node
' Subsequent print statements are test only, they should actually branch to appropriate subroutines, eg: goto [play]
if instr(msg,"PLAY") <> 0 then print "Play"                                             
if instr(msg,"PAUSE") <> 0 then print "Pause"
if instr(msg,"NEXT") <> 0 then print "Next"
if instr(msg,"PREVIOUS") <> 0 then print "Previous"
if instr(msg,"VUP") <> 0 then print "Volume Up"
if instr(msg,"VDOWN") <> 0 then print "Volume Down"

' Common commands for all system nodes
if instr(upper(msg), upper("Report")) then goto [report]
if instr(upper(msg), upper("Blink")) then goto [blink]
if instr(upper(msg)A, upper("FlashIP")) then goto [flashIP]
if instr(upper(msg), upper("Reset")) then goto [reset]
udpreply name & " ERROR: no recognised local command in msg: " & msg
return

[declare]
udpreply name
return

[report]
udpreply name   ' change name to whatever info needs to be sent back
return

[blink]
print "cause GPIO01 led to blink"
return

[flashIP]
print "cause GPIO01 led to flash the significant last IP address byte"
return

[reset]
print "cause node to reset"
return

[button]
' Broadcast system nodes to declare themselves (Who's there)
print "GPIO00 button pressed"
return