{"id":129,"date":"2024-11-07T21:35:55","date_gmt":"2024-11-07T21:35:55","guid":{"rendered":"https:\/\/myjetsonnano.hopto.org\/?p=129"},"modified":"2024-11-07T21:44:53","modified_gmt":"2024-11-07T21:44:53","slug":"uiflow2-atoms3u-espnow","status":"publish","type":"post","link":"https:\/\/myjetsonnano.perseus314.com\/index.php\/2024\/11\/07\/uiflow2-atoms3u-espnow\/","title":{"rendered":"UiFlow2 AtomS3U ESPNow"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A bit of explanation here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Firstly, this example program uses my device MAC addresses. If anyone else wants to run it, they will need to use their own MAC addresses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Secondly, I hate trying to keep track of two programs and what version of which program is in which device. Another worry is that a program is loaded into the wrong device. So to get things working here, I load the same program into each device and it tests the device&#8217;s MAC address in order to act a device 1or device 2. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thinking of a rainbow: red=1, orange=2, &#8230; each device changes color to show its device number, if its the only device turned on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If both devices are active, then each sends random numbers to the other and the number is used to switch to another rainbow colour.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"755\" height=\"754\" src=\"https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-30.png\" alt=\"\" class=\"wp-image-130\" srcset=\"https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-30.png 755w, https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-30-300x300.png 300w, https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-30-150x150.png 150w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"788\" src=\"https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-31.png\" alt=\"\" class=\"wp-image-131\" srcset=\"https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-31.png 740w, https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-31-282x300.png 282w\" sizes=\"auto, (max-width: 740px) 100vw, 740px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"691\" height=\"192\" src=\"https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-32.png\" alt=\"\" class=\"wp-image-132\" srcset=\"https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-32.png 691w, https:\/\/myjetsonnano.perseus314.com\/wp-content\/uploads\/2024\/11\/image-32-300x83.png 300w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>import os, sys, io\nimport M5\nfrom M5 import *\nfrom m5espnow import M5ESPNow\nfrom hardware import *\nimport time\n\n\n\nespnow_0 = None\nrgb = None\n\n\nimport random\n\ndevice_hex = None\nespnow_mac = None\nespnow_data = None\nNewColor = None\nmac1 = None\nmac2 = None\ndevice_mac = None\n\n\ndef espnow_recv_callback(espnow_obj):\n  global espnow_0, rgb, device_hex, espnow_mac, espnow_data, NewColor, mac1, mac2, device_mac\n  espnow_mac, espnow_data = espnow_obj.recv_data()\n  NewColor = espnow_0._bytes_to(espnow_data, 0)\n  if NewColor == 1:\n    rgb.set_color(0, 0xff0000)\n  if NewColor == 2:\n    rgb.set_color(0, 0xff6600)\n  if NewColor == 3:\n    rgb.set_color(0, 0xffff00)\n  if NewColor == 4:\n    rgb.set_color(0, 0x33cc00)\n  if NewColor == 5:\n    rgb.set_color(0, 0x33ffff)\n  if NewColor == 6:\n    rgb.set_color(0, 0x6633ff)\n  if NewColor == 7:\n    rgb.set_color(0, 0xff99ff)\n\n\ndef setup():\n  global espnow_0, rgb, device_hex, espnow_mac, espnow_data, NewColor, mac1, mac2, device_mac\n\n  M5.begin()\n  rgb = RGB()\n  rgb.set_brightness(70)\n  espnow_0 = M5ESPNow(0)\n  espnow_0.set_irq_callback(espnow_recv_callback)\n  mac1 = '70041DD348E4'\n  mac2 = '70041DD34EE0'\n  device_mac = espnow_0.get_mac(0)\n  device_hex = espnow_0._bytes_to_hex_str(device_mac)\n  if device_hex == mac2:\n    rgb.fill_color(0xff6600)\n    espnow_0.set_add_peer(mac1, 1, 0, False)\n  if device_hex == mac1:\n    rgb.fill_color(0xff0000)\n    espnow_0.set_add_peer(mac2, 1, 0, False)\n\n\ndef loop():\n  global espnow_0, rgb, device_hex, espnow_mac, espnow_data, NewColor, mac1, mac2, device_mac\n  M5.update()\n  espnow_0.send_data(1, random.randint(1, 7))\n  time.sleep(1)\n\n\nif __name__ == '__main__':\n  try:\n    setup()\n    while True:\n      loop()\n  except (Exception, KeyboardInterrupt) as e:\n    try:\n      from utility import print_error_msg\n      print_error_msg(e)\n    except ImportError:\n      print(\"please update to latest firmware\")\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A bit of explanation here. Firstly, this example program uses my device MAC addresses. If anyone else wants to run it, they will need to use their own MAC addresses. Secondly, I hate trying to keep track of two programs and what version of which program is in which device. Another worry is that a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-atoms3u","category-uiflow2"],"_links":{"self":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":3,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions\/136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/media\/133"}],"wp:attachment":[{"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myjetsonnano.perseus314.com\/index.php\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}