Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By russm
#53947 If your ESP8266 subnet may change but you still want to send a UDP broadcast, here is a simple routine to determine your broadcast address:

Code: Select all'Determine Broadcast Address

'Define these variables somewhere at the beginning
i = 0
x = 0
x1 = 0
broadcastaddr = " "

'Create broadcast address (place somewhere at beginning)
ipaddr = ip()
for i = 1 to 3                         'Last "." in the string will always be the 3rd
 x = instr(ipaddr,".",x1+1)  'Starts the next search cycle one char past the last time a "." was found
 x1 = x                                  'Sets next search location
next
broadcastaddr = left(ipaddr,x)&"255"

'Example UDP code:
'  udpwrite broadcastaddr, 5001, "Help me Spock"



If there is a better way I would love to know about it. If nothing else it was fun playing with strings.
User avatar
By Electroguard
#53966 This EasyNet snippet in not necessarilly better, but it offers easy access to all 3 IP address components (there are times when it may be useful to have all 3 available individually).

The full localIP address is returned with ip(), then a gosub to [IPSPLIT] chops the localIP into the 2 separate components of nodeIP and netIP, with netIP retaining a trailing dot for easy appending of a node or broadcast (255)
address when wished.

Code: Select alllet localIP = ip()
gosub [IPSPLIT]
let broadcastIP = netIP & "255"

[IPSPLIT]
pos = instrrev(localIP,".")
netIP = left(localIP,pos)
nodeIP = mid(localIP,pos+1)
return
User avatar
By Oldbod
#54360 If you want to code to broadcast across any network, you perhaps need to consider the scope of the network. It may not use the entire last byte, or it might use more than the last byte. Subnet mask can be important here, especially if not using default.

Have you considered using 255.255.255.255? Can be hardcoded and requires no processing?