{"id":858,"date":"2020-11-28T20:00:38","date_gmt":"2020-11-28T13:00:38","guid":{"rendered":"https:\/\/www.makeriot2020.com\/?p=858"},"modified":"2020-11-28T20:00:43","modified_gmt":"2020-11-28T13:00:43","slug":"multiple-lora-device-communication","status":"publish","type":"post","link":"https:\/\/www.makeriot2020.com\/index.php\/2020\/11\/28\/multiple-lora-device-communication\/","title":{"rendered":"Multiple LoRa Device Communication"},"content":{"rendered":"\n<p>In this part of my LoRa Series ( Part 3 ) I will look at some basic code for the Heltec LoRa 32 V2 Module. This code will in particular be focused on Multiple device communication. It can easily adapted from the stock example (as provided below) to implement a custom addressing scheme. <br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LoRa Multiple Communications No Interrupt<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>#include \"heltec.h\"\n\n#define BAND    433E6  \/\/you can set band here directly,e.g. \/\/868E6,915E6\n\n\nString outgoing;              \/\/ outgoing message\n\nbyte localAddress = 0xBB;     \/\/ address of this device\nbyte destination = 0xFD;      \/\/ destination to send to\n\nbyte msgCount = 0;            \/\/ count of outgoing messages\nlong lastSendTime = 0;        \/\/ last send time\nint interval = 2000;          \/\/ interval between sends\n\nvoid setup()\n{\n   \/\/WIFI Kit series V1 not support Vext control\n  Heltec.begin(true \/*DisplayEnable Enable*\/, true \/*Heltec.LoRa Enable*\/, true \/*Serial Enable*\/, true \/*PABOOST Enable*\/, BAND \/*long BAND*\/);\n\n  Serial.println(\"Heltec.LoRa Duplex\");\n\n \n}\n\nvoid loop()\n{\n  if (millis() - lastSendTime > interval)\n  {\n    String message = \"Hello there!\";   \/\/ send a message\n    sendMessage(message);\n    Serial.println(\"Sending \" + message);\n    lastSendTime = millis();            \/\/ timestamp the message\n    interval = random(2000) + 1000;    \/\/ 2-3 seconds\n  }\n\n  \/\/ parse for a packet, and call onReceive with the result:\n  onReceive(LoRa.parsePacket());\n}\n\nvoid sendMessage(String outgoing)\n{\n  LoRa.beginPacket();                   \/\/ start packet\n  LoRa.write(destination);              \/\/ add destination address\n  LoRa.write(localAddress);             \/\/ add sender address\n  LoRa.write(msgCount);                 \/\/ add message ID\n  LoRa.write(outgoing.length());        \/\/ add payload length\n  LoRa.print(outgoing);                 \/\/ add payload\n  LoRa.endPacket();                     \/\/ finish packet and send it\n  msgCount++;                           \/\/ increment message ID\n}\n\nvoid onReceive(int packetSize)\n{\n  if (packetSize == 0) return;          \/\/ if there's no packet, return\n\n  \/\/ read packet header bytes:\n  int recipient = LoRa.read();          \/\/ recipient address\n  byte sender = LoRa.read();            \/\/ sender address\n  byte incomingMsgId = LoRa.read();     \/\/ incoming msg ID\n  byte incomingLength = LoRa.read();    \/\/ incoming msg length\n\n  String incoming = \"\";\n\n  while (LoRa.available())\n  {\n    incoming += (char)LoRa.read();\n  }\n\n  if (incomingLength != incoming.length())\n  {   \/\/ check length for error\n    Serial.println(\"error: message length does not match length\");\n    return;                             \/\/ skip rest of function\n  }\n\n  \/\/ if the recipient isn't this device or broadcast,\n  if (recipient != localAddress &amp;&amp; recipient != 0xFF) {\n    Serial.println(\"This message is not for me.\");\n    return;                             \/\/ skip rest of function\n  }\n\n  \/\/ if message is for this device, or broadcast, print details:\n  Serial.println(\"Received from: 0x\" + String(sender, HEX));\n  Serial.println(\"Sent to: 0x\" + String(recipient, HEX));\n  Serial.println(\"Message ID: \" + String(incomingMsgId));\n  Serial.println(\"Message length: \" + String(incomingLength));\n  Serial.println(\"Message: \" + incoming);\n  Serial.println(\"RSSI: \" + String(LoRa.packetRssi()));\n  Serial.println();\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">LoRa Multiple communication, Interrupt<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"heltec.h\"\n\n#define BAND    433E6  \/\/you can set band here directly,e.g. 868E6,915E6\n\n  \nbyte localAddress = 0xBB;     \/\/ address of this device\nbyte destination = 0xFF;      \/\/ destination to send to\n\nString outgoing;              \/\/ outgoing message\nbyte msgCount = 0;            \/\/ count of outgoing messages\nlong lastSendTime = 0;        \/\/ last send time\nint interval = 2000;          \/\/ interval between sends\n\nvoid setup()\n{\n   \/\/WIFI Kit series V1 not support Vext control\n  Heltec.begin(true \/*DisplayEnable Enable*\/, true \/*Heltec.LoRa Disable*\/, true \/*Serial Enable*\/, true \/*PABOOST Enable*\/, BAND \/*long BAND*\/);\n\n  LoRa.onReceive(onReceive);\n  LoRa.receive();\n  Serial.println(\"Heltec.LoRa init succeeded.\");\n}\n\nvoid loop()\n{\n  if (millis() - lastSendTime > interval)\n  {\n    String message = \"Hello World!\";   \/\/ send a message\n    sendMessage(message);\n    Serial.println(\"Sending \" + message);\n    lastSendTime = millis();            \/\/ timestamp the message\n    interval = random(2000) + 1000;     \/\/ 2-3 seconds\n    LoRa.receive();                     \/\/ go back into receive mode\n  }\n}\n\nvoid sendMessage(String outgoing)\n{\n  LoRa.beginPacket();                   \/\/ start packet\n  LoRa.write(destination);              \/\/ add destination address\n  LoRa.write(localAddress);             \/\/ add sender address\n  LoRa.write(msgCount);                 \/\/ add message ID\n  LoRa.write(outgoing.length());        \/\/ add payload length\n  LoRa.print(outgoing);                 \/\/ add payload\n  LoRa.endPacket();                     \/\/ finish packet and send it\n  msgCount++;                           \/\/ increment message ID\n}\n\nvoid onReceive(int packetSize)\n{\n  if (packetSize == 0) return;          \/\/ if there's no packet, return\n\n  \/\/ read packet header bytes:\n  int recipient = LoRa.read();          \/\/ recipient address\n  byte sender = LoRa.read();            \/\/ sender address\n  byte incomingMsgId = LoRa.read();     \/\/ incoming msg ID\n  byte incomingLength = LoRa.read();    \/\/ incoming msg length\n\n  String incoming = \"\";                 \/\/ payload of packet\n\n  while (LoRa.available())             \/\/ can't use readString() in callback\n  {\n    incoming += (char)LoRa.read();      \/\/ add bytes one by one\n  }\n\n  if (incomingLength != incoming.length())   \/\/ check length for error\n  {\n    Serial.println(\"error: message length does not match length\");\n    return;                             \/\/ skip rest of function\n  }\n\n  \/\/ if the recipient isn't this device or broadcast,\n  if (recipient != localAddress &amp;&amp; recipient != 0xFF)\n  {\n    Serial.println(\"This message is not for me.\");\n    return;                             \/\/ skip rest of function\n  }\n\n  \/\/ if message is for this device, or broadcast, print details:\n  Serial.println(\"Received from: 0x\" + String(sender, HEX));\n  Serial.println(\"Sent to: 0x\" + String(recipient, HEX));\n  Serial.println(\"Message ID: \" + String(incomingMsgId));\n  Serial.println(\"Message length: \" + String(incomingLength));\n  Serial.println(\"Message: \" + incoming);\n  Serial.println(\"RSSI: \" + String(LoRa.packetRssi()));\n  Serial.println();\n}<\/code><\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this part of my LoRa Series ( Part 3 ) I will look at some basic code for the Heltec LoRa 32 V2 Module. This code will in particular be focused on Multiple device communication. It can easily adapted from the stock example (as provided below) to implement a custom addressing scheme. LoRa Multiple &hellip; <a href=\"https:\/\/www.makeriot2020.com\/index.php\/2020\/11\/28\/multiple-lora-device-communication\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Multiple LoRa Device Communication&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":843,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[90],"tags":[42,81,55,91],"class_list":["post-858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lora","tag-esp32","tag-esp8266","tag-iot","tag-lora"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/comments?post=858"}],"version-history":[{"count":0,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/858\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media\/843"}],"wp:attachment":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media?parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/categories?post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/tags?post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}