module main author unknown version 1 0 description '' variables BUFFER led 'line#' spec ' ' 'LINEinc' 'LINEinc' spec ' ' 'SERVERinfo' 'SERVERinfo' to LINEinc { if ('Pico W' == (boardType)) { line# += 8 if (line# > 56) { OLEDclear line# = 0 SERVERinfo } } ('M5Stack-Core' == (boardType)) { line# += 20 if (line# > 220) { '[display:mbDisplayOff]' line# = 0 SERVERinfo } } } to SERVERinfo { if ('Pico W' == (boardType)) { OLEDwrite 'UDP Server:' 0 line# false LINEinc OLEDwrite (getIPAddress) 0 line# false LINEinc } ('M5Stack-Core' == (boardType)) { '[tft:text]' 'UDP Server:' 0 line# (colorSwatch 255 252 7 255) 2 true LINEinc '[tft:text]' (getIPAddress) 0 line# (colorSwatch 255 252 7 255) 2 true LINEinc } } script 63 50 { comment 'UDP Demo HotSpot Version: - Creates a WIFI HotSpot with given credentials. - Network service will be visible in your PC or Cell WIFI scan with the name used. - Then waits for UDP messages on port 5000. - If the device has a button (A, BOOT, FLASH), it can be pressed to send a UDP broadcast "LED". - Broadcasts with payload "LED" will turn the on-board LEDs on and off. - Broadcast senders cannot see their own messages.. - One can also send a msg to any other device using its specific IP. - One can also send a msg to oneself using own IP. ' } script 529 171 { comment 'Use this block to send messages to specific devices, including your own.' '[net:udpSendPacket]' 'LED' '192.168.4.2' 5000 } script 745 269 { whenCondition (buttonA) comment 'Broadcasts udp message = LED IP 255.255.255.255 is a broadcast address.' waitUntil (not (buttonA)) '[net:udpSendPacket]' 'LED' '255.255.255.255' 5000 sayIt 'Sent...' } script 67 271 { whenStarted led = (booleanConstant false) wifiCreateHotspot 'UDPTEST' '0123456789' sayIt 'My IP:' (getIPAddress) '[net:udpStart]' 5000 forever { BUFFER = ('[net:udpReceivePacket]') if ((size BUFFER) > 0) { sayIt BUFFER } waitMillis 500 } } script 399 273 { whenCondition (BUFFER == 'LED') comment 'Turns LED on/off upon receiving udp message = LED' BUFFER = '' led = (not led) setUserLED led sayIt led } module UDP Comm author MicroBlocks version 1 0 depends WiFi tags udp network description 'Blocks to send and receive UDP packets.' spec ' ' '[net:udpStart]' 'UDP start port _' 'auto' 5000 spec ' ' '[net:udpStop]' 'UDP stop' spec ' ' '[net:udpSendPacket]' 'UDP send packet _ to ip _ port _' 'auto auto num' 'Hello!' '255.255.255.255' 5000 spec 'r' '[net:udpReceivePacket]' 'UDP receive packet : binary data _' 'bool' false spec 'r' '[net:udpRemoteIPAddress]' 'UDP remote IP address' spec 'r' '[net:udpRemotePort]' 'UDP remote port' module WiFi Comm author MicroBlocks version 1 5 tags communication network description 'Connect to a WiFi network. Used in conjunction with other network libraries, such as HTTP client, HTTP server or Web Thing. ' spec ' ' 'wifiConnect' 'wifi connect to _ password _ : IP _ gateway _ subnet _' 'str str auto auto auto' 'Network_Name' '' '192.168.1.42' '192.168.1.1' '255.255.255.0' spec ' ' 'wifiCreateHotspot' 'wifi create hotspot _ password _' 'str str' 'Network_Name' 'Network_Password' spec 'r' 'getIPAddress' 'IP address' spec 'r' '[net:myMAC]' 'MAC address' to getIPAddress { return ('[net:myIPAddress]') } to wifiConnect ssid password fixedIP gatewayIP subnetIP { if (not ('[net:hasWiFi]')) {return} '[net:stopWiFi]' if ((pushArgCount) < 5) { '[net:startWiFi]' ssid password } else { '[net:startWiFi]' ssid password false fixedIP gatewayIP subnetIP } local 'startMSecs' (millisOp) repeatUntil (('[net:myIPAddress]') != '0.0.0.0') { comment 'Timeout after N seconds' if (((millisOp) - startMSecs) > 10000) { sayIt 'Could not connect' return 0 } comment 'Slow blink while trying to connect' setUserLED true waitMillis 300 setUserLED false waitMillis 300 } repeat 6 { comment 'Quick blinks when connected' setUserLED true waitMillis 50 setUserLED false waitMillis 50 } sayIt 'My IP address is:' ('[net:myIPAddress]') } to wifiCreateHotspot ssid password { if (not ('[net:hasWiFi]')) {return} '[net:startWiFi]' ssid password true if ('Connected' != ('[net:wifiStatus]')) { sayIt 'Could not create hotspot' return 0 } repeat 6 { comment 'Quick blinks when connected' setUserLED true waitMillis 50 setUserLED false waitMillis 50 } sayIt 'My IP address is:' ('[net:myIPAddress]') }