0

community

I've been working on image transmission using xbee s2b pro modules and Arduino Mega. Main task is to transmit an jpg image taken by a jpeg serial camera at the transmitter and send it to a microSD memory at the receiver, but I've been dealing with one unique trouble, I need a delay of 2 seconds to send and receive succesfully 1 byte, if I set less seconds I lose information and receipt image get corrupted. Here you will see my codes:

Transmitter code:

byte ZERO = 0x00;
byte incomingbyte;
long int a=0x0000,j=0,k=0,count=0,i=0;
uint8_t MH,ML;
boolean EndFlag=0;

void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{   
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
} 
Serial1.begin(38400);
}

void loop()
{
byte a[32];
int ii;
SendResetCmd();
delay(4000);                            
SendTakePhotoCmd();
delay(1000);     
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
}
while(!EndFlag)
{
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(20); 
while(Serial1.available()>0)
{
incomingbyte=Serial1.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag))
{
a[j]=incomingbyte;
if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++)
{
Serial.write(a[j]);
delay(2000);// observe the image through serial port
}
i++;
} 
while(1);
}

void SendResetCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x26);
Serial1.write(ZERO);
}

void SetImageSizeCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x31);
Serial1.write(0x05);
Serial1.write(0x04);
Serial1.write(0x01);
Serial1.write(ZERO);
Serial1.write(0x19);
Serial1.write(0x11);
}

void SetBaudRateCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x24);
Serial1.write(0x03);
Serial1.write(0x01);
Serial1.write(0x2A);
Serial1.write(0xC8);

}

void SendTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(ZERO);
}

void SendReadDataCmd()
{
MH=a/0x100;
ML=a%0x100;
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x32);
Serial1.write(0x0c);
Serial1.write(ZERO);
Serial1.write(0x0a);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(MH);
Serial1.write(ML);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(ZERO);
Serial1.write(0x20);
Serial1.write(ZERO);
Serial1.write(0x0a);
a+=0x20;
}

void StopTakePhotoCmd()
{
Serial1.write(0x56);
Serial1.write(ZERO);
Serial1.write(0x36);
Serial1.write(0x01);
Serial1.write(0x03);
}

Receiver code:

#include <SD.h>
#include <SPI.h>

File myFile;
int pinCS = 53; // Pin 10 on Arduino Uno

void setup() 
{
  Serial.begin(9600);
  pinMode(pinCS, OUTPUT);
   if (!SD.begin()) {
    return;
  }
}

void loop() {
  byte buf[5000];
  if (Serial.available()>0)
  {
      myFile = SD.open("imgrx.jpg", FILE_WRITE);
      Serial.readBytes(buf,sizeof(buf));
      Serial.println((byte)*buf);
      myFile.write((byte)*buf);
      while(Serial.available()>0) Serial.read();
   }
  else{
    myFile.close();
  }
}

I've tried a MicroSD -> XBEE -> MicroSD also and that trouble continued there, maybe I've passed through something and I'd like to tell me where. Let me add these methods give me images identical to the ones I send, so even slowly, it's very effective. I changed baudrates without any success. Xbee adresses are connected as unicast. I also tried using only Serial.readBytes and I got Bytes faster but as decimal values and is unreadable.

I would really appreciate any information or experience approach to solve this problem. Ask me anything you don't understand about my question. Thanks

M. Ríos
  • 1
  • 2
  • Welcome to Stack Overflow! That looks like more code than strictly needed. Can you reduce the code until you have a [mcve] that demonstrates your problem? This will increase your chances of getting good answers. – Neuron Apr 26 '18 at 16:30
  • There is also an Arduino Stack Exchange site that you might want to research first. –  Apr 26 '18 at 16:33
  • Thanks for those appreciations. – M. Ríos Apr 26 '18 at 16:50

1 Answers1

0

Some starting recommendations:

  • Increase your baud rate to 115200 to increase throughput to/from the XBee.
  • Add hardware flow control to your setup so you know when the XBee module is "full" of queued data and not receiving.
  • Switch to API mode on your XBee modules, and generate API frames using the maximum payload size.
  • Wait for the Transmit Status response on your outbound Transmit frames before sending your next packet.

XBee 802.15.4 modules (including Zigbee and DigiMesh) weren't designed for high throughput. They're low-power, long range devices where you'll be lucky to get 10kbytes/second.

tomlogic
  • 11,489
  • 3
  • 33
  • 59