-1

I have a sender that has a bitmap array that represents an image I've downloaded. The sender sends the pixels to the receiver, which the receiver should receive. I need to insert them into another bitmap array and display the picture.

Below is my sender sketch:

static const unsigned char PROGMEM myBitmap[] ={
  0x00, 0x00, 0x00, 0x00, 0x11, 0xff, 0xff, 0xff, 0xff, 0xdf, 0x07, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x21, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xc7, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xc1, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xe3, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x31, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xf2, 0x71, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x11, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x31, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x31, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x71, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x71, 0xff, 0xff, 0xf0, 0x07, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf3, 0xff, 0xff, 0xf0, 0x03, 0xef, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xf0, 0x03, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xe0, 0x00, 0x7f, 0x8f, 0xf8, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0x80, 0x00, 0x7f, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xfe, 0xff, 0x80, 0x00, 0x3c, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0xff, 0xc0, 0x00, 0x10, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0xff, 0xc0, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0xff, 0xc0, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0x7f, 0xe0, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0x7f, 0xff, 0x38, 0x00, 0xdf, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0x7f, 0xff, 0xf8, 0x01, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0x7f, 0xff, 0xf0, 0x07, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfe, 0x3f, 0xff, 0xe0, 0x1c, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0x3f, 0xff, 0xc0, 0x39, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0x3f, 0xff, 0x80, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0x3f, 0xff, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xfe, 0x00, 0x1f, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xf8, 0x70, 0x1f, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xf0, 0xf8, 0x7f, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xf1, 0xfc, 0xff, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xfd, 0xff, 0xe3, 0xdc, 0xff, 0xf0, 0x1f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xe7, 0xdc, 0xff, 0xe0, 0x38, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xcf, 0xdc, 0xff, 0xe0, 0x70, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xff, 0xde, 0x0c, 0x7f, 0xe0, 0xe0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xff, 0xbf, 0x1e, 0x7d, 0xe3, 0xc0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xff, 0xbf, 0x1e, 0xff, 0xc7, 0xc0, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xff, 0x7f, 0xbe, 0xff, 0xc7, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xfe, 0xff, 0xfe, 0xff, 0xcf, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xdf, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xfb, 0xff, 0xf8, 0xff, 0x9f, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xe7, 0xff, 0xff, 0xff, 0x8f, 0x80, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xef, 0xff, 0xfb, 0xff, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xef, 0xff, 0xff, 0xfe, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xef, 0xff, 0xf3, 0xfe, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xff, 0xff, 0xe3, 0xfe, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xff, 0xf3, 0xfe, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xf9, 0xff, 0xff, 0xff, 0xf8, 0x3f, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xb8, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xb9, 0xff, 0xff, 0xff, 0xf8, 0x0f, 0xfe, 0x3f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x99, 0xff, 0xff, 0xff, 0xf8, 0x07, 0xfc, 0x3f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd9, 0xff, 0xff, 0xff, 0xfc, 0x06, 0xfc, 0x3f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd8, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xf8, 0x7f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd8, 0xff, 0xff, 0xff, 0xfc, 0x03, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd8, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd8, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd9, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd9, 0xff, 0xff, 0xff, 0xff, 0x01, 0xf9, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xd8, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xc8, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0xc9, 0xff, 0xff, 0xff, 0xff, 0x80, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00
};

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Sender");
  //set up LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);
  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(915E6)) {
    Serial.println(".");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word assures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

void loop() {
      LoRa.beginPacket();
      Serial.print("Sending packet");
      LoRa.printf("0x%02x\n",myBitmap[i]);
      i=i+1;
      LoRa.endPacket();
      ID=ID+1;

Below is my receiver sketch:

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Receiver");
  //setup LoRa transceiver module
  LoRa.setPins(ss, rst, dio0);
  //replace the LoRa.begin(---E-) argument with your location's frequency 
  //433E6 for Asia
  //866E6 for Europe
  //915E6 for North America
  while (!LoRa.begin(915E6)) {
    Serial.println(".");
    delay(500);
  }
   // Change sync word (0xF3) to match the receiver
  // The sync word insures you don't get LoRa messages from other LoRa transceivers
  // ranges from 0-0xFF
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}

void loop() {
 char buff[4]={0};
 char c;
  // try to parse packet
  static  unsigned char PROGMEM myBitmap [200]={};
  int packetSize = LoRa.parsePacket();
   uint8_t ix = 0;
  if (packetSize) {
    char buff[4] = {0};
    while (LoRa.available()) {
        Serial.print("this is the data");
        buff[ix]=(char)LoRa.read();
    }
 Serial.print("this is the string\n");
    Serial.print(finall);
  // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

The problem that is on the receiver, I am unable to create an array, put the pixels that are successfully sent from the sender in it.

Any help would be appreciated. Thank you.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 2
    First, you are trying to send 1024 bytes of data over LoRa, you need to understands the limitation of [packet size](https://lora-developers.semtech.com/documentation/tech-papers-and-guides/the-book/packet-size-considerations) of LoRa technology. Even you can send 1024 bytes over LoRa, you shouldn't because it violates the airtime utilisation rule, and it also greatly reduce the distance of your LoRa network. – hcheung Jul 13 '22 at 00:24
  • 1
    There is not much right about that code it is hard to see where to start. In the receiver you declare two different `buff` arrays, but use only one. You declare `myBitmap` but don't use it at all. You never increment `ix` and if you did you have nothing to prevent from exceeding the bounds of the improbably small `buff`. What kind of Arduino is this,? The receiver creates its arrays on the stack. Many Arduino devices have tiny memories so tiny stacks - the transmitter array is static and in ROM, does the receiver even have enough RAM to receive it? – Clifford Sep 24 '22 at 07:08

3 Answers3

0

Your message-reading loop doesn't increment the index counter:

buff[ix]=(char)LoRa.read();

should be

buff[ix++]=(char)LoRa.read();

In case there may be more than 255 chars in one read, I'd declare ix at least 16-bit:

uint16_t ix = 0;

Also, you are declaring buff twice.

Also, to be safe, are you sure 4 bytes is enough?

mmixLinus
  • 1,646
  • 2
  • 11
  • 16
  • hello thank you for the help. I fixed the above. whats left is to group the characters coming from the sender into a string and create an array that consists of the grouped characters. lets say the sender sends 0xff. the receiver will receive first the 0, then the x then the f then the f. what im finding hard to figure is to group the characters into one string. in the mentioned example the receiver should group the character to have 0xff as a single string, then insert the string into an array so that lets say arr[0]=0xff, arr[1]=. arr[2]=, any help would be appreciated.thanks – Tony Joe Azar Jul 13 '22 at 18:55
0

There are two issues in your code:

  • In your receiver code, buff[ix]=(char)LoRa.read(); never increments ix.
  • You cannot send 1,024 bytes of data. The LoRa chip's buffer is 256 bytes long.

The second point means that you need to split your array in smaller pieces, and implement a transfer protocol with a few key elements:

  • Frame counter
  • Data
  • Checksum

When the sender is done sending a frame – say 128 bytes – the receiver decodes the frame, saves it, and sends back an ACK of some sort, eg ACK xx yy zz nn where xx is the counter, yy zz the checksum, and nn the number of bytes received. If the sender agrees with that, it moves on the next frame. If not, it either retries, or sends a FAIL signal.

dda
  • 6,030
  • 2
  • 25
  • 34
-1

If you mean you don't know how to create an array, you can first make your program send the size of the array and do dynamic memory allocation.

unsigned char *arr = malloc(size); 

Then you can copy the received buffer to here

  • malloc - arduino - good luck mate – 0___________ Jul 12 '22 at 19:38
  • @0___________ why not? https://forum.arduino.cc/t/dynamic-memory-allocation-malloc-and-free/408391 – Özgür Güzeldereli Jul 12 '22 at 19:54
  • If you don't know the dangerous of using malloc for an Arduino with 2K memory, then you shouldn't use it. By the time you know how to use it, in many cases, you don't need it.... OP already has a buf declared with size of 4 with ` char buff[4]={0};`, why the use of `malloc()` help in this case? – hcheung Jul 13 '22 at 00:44
  • @hcheung the only danger I could think of was fragmentation, and since the buffer is large I assumed that wouldn't cause much trouble. The reason I proposed ```malloc``` was that the receiver may not be aware of the size of the bitmap and OP pointed out they are unable to create an array – Özgür Güzeldereli Jul 13 '22 at 09:34
  • Dears, thank you for you response, the main problem is that the sender is sending the data in characters, lets say the sender sends 0xff. the receiver receives first the 0, then the x , then f, then the f. I dont know how to make the receiver group the 4 characters into one string and create an array that consists of the strings./ in the example mentioned here, i want to have lets say arr[0]=0xff, arr[1]=0x00,..., at the receiver side. after having an array that is a mirror of the sender array.i can output the image in OLED display.Thank you! – Tony Joe Azar Jul 13 '22 at 18:47
  • @TonyJoeAzar Unless you are first turning the numbers into strings you wouldn't get them as 0, x, f, f. That seems incorrect. 0xFF is one byte of information. The 0x part represents that this is the number 255 in hexadecimal notation. Something seems off please refer to here https://stackoverflow.com/questions/2670639/why-are-hexadecimal-numbers-prefixed-with-0x. Edit: oh but I see you are turning them into string. Just do ```LoRa.printf("%c",myBitmap[i]);``` instead of ```LoRa.printf("0x%02x\n",myBitmap[i]);``` and you can receive them and put them directly into the array – Özgür Güzeldereli Jul 13 '22 at 19:23
  • @TonyJoeAzar sounds like an X-Y problem. If your goal is to display on the OLED, then ask _that_ question. It seems likely (short of something fairly capable such as an Arduino Due), that you will not be able to store the image, and will have to write pixels directly to the display as they arrive. – Clifford Sep 24 '22 at 07:15