Haripra
Posts: 2
Joined: Mon Jul 08, 2019 9:23 am

Transfer image over xbee s2c pro using raspberry pi3

Mon Jul 08, 2019 9:55 am

hardware setup:
connected two xbee modules to two raspberrypi, configured one xbee as coordinator(receiver) and the other as router(sender).
we are able to send text file over xbee transparent mode.
problem:
we are unable to send complete image file over this setup. below is the code used for sending and receiving image.
on the receiving side we are able to see the garbage (may be image received),but unable to view it in any image viewer.here we are facing error:fatal error reading jpg image file.
error:interpreting jpeg image file (not a jpeg file:starts with 0x00 0xd6)
can someone help us in doing this as soon as possible.
thanks
in advance.

python code sender side:

Code: Select all

#from twilio.rest import Client
from gpiozero import MotionSensor
from picamera import PiCamera
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import datetime
import smtplib
import time
import serial


#import serial
import RPi.GPIO as GPIO      
import os, time
 
#GPIO.setmode(GPIO.BOARD) 

# Email Addresses
fromAddr = 'abc@gmail.com'
toAddr = 'def@gmail.com'

#Motion Sensor and Camera Initialized
PIRSensor = MotionSensor(14)
camera = PiCamera() 

# Variable to Exit while loop
Tripped = True

#wait five seconds
time.sleep(2)

while Tripped:
    if PIRSensor.motion_detected:       
        print("Motion Detected")
      
        timeCaptured = '/home/pi/Desktop' + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S') + '.jpg'
        camera.rotation = 180
        #camera.start_preview()
        camera.capture(timeCaptured)
        timerecorded = '/home/pi/Desktop' + datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S') + '.h264'
        camera.start_recording(timerecorded)
        time.sleep(5)
        camera.stop_recording()
        #camera.stop_preview()
        print("Image Captured")
        print("video recoded")


####xbee tranfer
        ser = serial.Serial('/dev/ttyUSB0', 9600,timeout=.5)
        fc= '/path/xbee.jpg'
        File = open(fc,'r')
        #while True:
        line = ser.readline()
        a= File.read()
        print(str(a))
        ser.write(str(a))
###xbee
#########email
		#Create the Message
        msg = MIMEMultipart()
        msg[ 'Subject'] = 'Intruder'
        msg['From'] = fromAddr
        msg['To'] = toAddr

		#Attach the file
        File = open(timeCaptured, 'rb')
        img = MIMEImage(File.read())
        File.close()
        msg.attach(img)
	 	
        username = 'abc@gmail.com'
        password = '*****'

        server = smtplib.SMTP('smtp.gmail.com:587')
        server.starttls()
        server.login(username,password)
        server.sendmail(fromAddr, toAddr, msg.as_string())
        #message = client.messages.create(body="HEY SOMEONE ENTERED..PLEASE CHECK YOUR MAIL",from_='+19587463210',to='+919876543210')
        server.quit()

        print("Email Sent")
        #print("sms sent")
       # print("msg sent to:"+message.to)
       # print(message.to)
        #camera.close()

        

[b]code on receiving end:[/b]:

import serial
import os
import datetime
import smtplib
import time
import mysql.connector
db = mysql.connector.connect(host="localhost", user="root", password="2019", db="test")
cursor = db.cursor()

ser = serial.Serial('/dev/ttyUSB0', 9600,timeout=.5)

while True:
   incoming = ser.readline().strip()


   file = open ('images.png','wb')

   print (incoming)
   file.write(incoming)
   file.close()
   file = open ('tezt.txt','r')
   content= file.read()

   sql = """LOAD DATA LOCAL INFILE '/home/pi/images.png' INTO table filetry"""
   print ("data inserted")
   cursor.execute(sql,(content))
   db.commit()
db.commit()
db.close()

ghp
Posts: 1395
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Transfer image over xbee s2c pro using raspberry pi3

Tue Jul 09, 2019 5:01 pm

sending binary data through a serial line needs some work
- use python3. Strict in using binary arrays in the right places.
- do not use readline().strip() with binary data. There might be all sort of bytes in there, so make no assumptions on content.
- build a frame around the binary data. Some start bytes "ABC" or "telegram". Then a length field, simple to use if in ascii, separate with '$'. Then place the binary image data. If you like, add a checksum ? On receiver side, parse incominng data byte by byte and look for your header, parse length and then write binary to a file. Restart if not matching. Possibly pickle can help.

Are there control flow bytes in byte stream for this xbee ? Then you need to escape these.

Haripra
Posts: 2
Joined: Mon Jul 08, 2019 9:23 am

Re: Transfer image over xbee s2c pro using raspberry pi3

Wed Jul 10, 2019 12:29 pm

Thanks for the reply,
We have configured xbee in API mode but unable to communicate the module,not even able to send text files.
Can you please elaborate the solution in detail.

ghp
Posts: 1395
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Transfer image over xbee s2c pro using raspberry pi3

Wed Jul 10, 2019 5:11 pm

There are a number of python libs for xbee around, one example is https://xbplib.readthedocs.io/en/latest/.
Using API is a good thing. Split the binary file into junks. Add a packet number and total number of packets, from this the receiver knows when data are complete.
If you own DIGI devices: there are tools from Digi, think it is 'XCTI' which helps to debug environments.

Return to “Python”