Sending Ascii Images

From packets2photons
Jump to navigation Jump to search

Process Control

until ./rx_2019_09_04.py; do
    echo "Server './rx_2019_09_04.py' crashed with exit code $?.  Respawning.." >&2
    sleep 1
done

Process Output

#!/bin/sh

echo "$@"

sort -V -o list.sorted$@ $@
uniq list.sorted$@ > unique$@
sed '/,==End/d' unique$@ > del_removed$@
awk -F "," '{print $1}' del_removed$@ > seq_nums$@
awk '{for(i=p+1; i<$1; i++) print i} {p=$1}' seq_nums$@ > missing$@
if [ -s "missing" ] 
then
        echo "missing has some data."
else
        echo "File check successful"
        awk '{sub(/[^,]*/,"");sub(/,/,"")} 1' del_removed$@ > cols_removed$@ 
        tr -d ' \t\n\r\f' < cols_removed$@ > base64$@
        cat base64$@ | base64 -d > done$@
fi

433 MHz Send

 """
Example for using the RFM69HCW Radio with Raspberry Pi.

Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
Author: Brent Rubell for Adafruit Industries
"""
# Import Python System Libraries
import time
# Import Blinka Libraries
import busio
from digitalio import DigitalInOut, Direction, Pull
import board
# Import the SSD1306 module.
import adafruit_ssd1306
# Import the RFM69 radio module.
import adafruit_rfm69
import base64

import os
import glob
import base64

while True:
    
    filename = int(time.time())
    filename = str(filename)
    filename = (filename[4:]+".jpg")
    print(filename)

    print ("Directory cleanup")
    #os.system('rm /home/pi/camera/*')
    #os.system('rm /home/pi/cam.jpg')
    #print ("Taking photo (copying image)")
    string = ("raspistill -o " + filename)
    os.system(string)
    #os.system('cp /home/pi/camera/cam.jpg /home/pi/cam.jpg')
    print ("Downscaling image")
    string = ("convert -resize 20% " + filename + " " + filename)
    os.system(string)
    string = ("convert " + filename  + " -rotate 180 " + filename)
    os.system(string)
    string = ("guetzli --quality 85 " + filename + " " + filename)
    os.system(string)
    #print ("removing cam.jpg")
    #os.system('rm /home/pi/camera/cam.jpg')
    print("we have taken photo " + filename)
    time.sleep(5)

    def split_by_n(seq, n):
    #    A generator to divide a sequence into chunks of n units.
        while seq:
            yield seq[:n]
            seq = seq[n:]

    # Create the I2C interface.
    i2c = busio.I2C(board.SCL, board.SDA)

    # 128x32 OLED Display
    display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)
    # Clear the display.
    display.fill(0)
    display.show()
    width = display.width
    height = display.height

    # Configure Packet Radio
    CS = DigitalInOut(board.CE1)
    RESET = DigitalInOut(board.D25)
    spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
    rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 433.0)
    prev_packet = None
    # Optionally set an encryption key (16 byte AES key). MUST match both
    # on the transmitter and receiver (or be set to None to disable/the default).
    rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08'

    txchars = 40
    line_no=0
    end_del = ("==End of Base 64 image")
    tx_success =("==Transfer Success")

    with open(filename, "rb") as imageFile:
        segment_retran=['0']
        base64str = base64.b64encode(imageFile.read())
        length = (len(base64str))
        #start_del = ("Beginning of Base64 image, estimated " +str (length) + " chars ==\n")
        print (base64str)
        print (length)
        num_lines = length/txchars
        print (str(length) + "chars so " + str(num_lines) + " lines to send")
        
        #send the Start Del
        info = list(split_by_n(base64str, txchars)) 
        #print ("Sending Start Del: " + start_del)
        #send_file = bytes(str(start_del), "utf-8")
        #rfm69.send(send_file)
        #print ("Start Del sent:")
        #print(infoa)

        packet = None

        while (line_no < num_lines):

            packet = rfm69.receive()
            time.sleep(1)

            if packet is not None:
                print("We recieved packet with contents:" )
                prev_packet = packet
                try:
                    (prev_packet).decode('utf-8')
                    packet_text = str(prev_packet, "utf-8")
                    print(packet_text)
                    packet = None
                    string = packet_text.split(",")
                except UnicodeError:
                    print("string is not UTF-8")
                if (string[0] == filename):
                    line_no = int(string[1])
                    print("Received loss notification, going back to: " + str(line_no))
                    #send_line = ((filename)+","+str(line)+","+str(info[line]))
                    #send_file = bytes(str(send_line), "utf-8")
                    #rfm69.send(send_file)
                    #print("Resent: "+ (send_line))

            info[line_no] = info[line_no].decode('utf-8')
            send_line = ((filename)+","+str(line_no)+","+str(info[line_no]))
            #print (x)
            #print ((x), end = '')
            print (send_line)
            send_file = bytes(str(send_line), "utf-8")
            rfm69.send(send_file)
            line_no+=1

        #finished sending
        #send end del
        send_line = end_del
        send_line = ((filename)+","+str(line_no)+","+ end_del)
        print (send_line)
        send_file = bytes(str(send_line), "utf-8")
        rfm69.send(send_file)
        time.sleep(2)
        rfm69.send(send_file)
        time.sleep(2)
        rfm69.send(send_file)

        packet_text = ("unfinished")
        
        count=200 
        while (packet_text != tx_success):
            print ("Awaiting file completion verification")
            if (count < 0): #then exit
                packet_text = tx_success
            else:
                count-=1
                packet = rfm69.receive()
                if packet is not None:
                    print("We recieved packet with contents:" )
                    prev_packet = packet
                    try:
                        (prev_packet).decode('utf-8')
                        packet_text = str(prev_packet, "utf-8")
                        print(packet_text)
                        string = packet_text.split(",")
                        print(string[1])
                    except UnicodeError:
                        print("string is not UTF-8")
                    if (string[1] == tx_success):
                        packet_text = string[1]
                        print("successful transfer exiting")
                    else:
                        line_no = int(string[1])
                        print("received request for seq num" + str(line_no) + str(info[line_no]))
                        send_line = ((filename)+","+str(line_no)+","+str(info[line_no]))
                        print (send_line)
                        send_file = bytes(str(send_line), "utf-8")
                        rfm69.send(send_file)

433 MHz Receive

#!/usr/bin/env python3

"""
Example for using the RFM69HCW Radio with Raspberry Pi.

Learn Guide: https://learn.adafruit.com/lora-and-lorawan-for-raspberry-pi
Author: Brent Rubell for Adafruit Industries
"""
# Import Python System Libraries
import time
# Import Blinka Libraries
import busio
from digitalio import DigitalInOut, Direction, Pull
import board
# Import the SSD1306 module.
import adafruit_ssd1306
# Import the RFM69 radio module.
import adafruit_rfm69
import os

# Create the I2C interface.
i2c = busio.I2C(board.SCL, board.SDA)

# Configure Packet Radio
CS = DigitalInOut(board.CE1)
RESET = DigitalInOut(board.D25)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
rfm69 = adafruit_rfm69.RFM69(spi, CS, RESET, 433.0)
prev_packet = None
# Optionally set an encryption key (16 byte AES key). MUST match both
# on the transmitter and receiver (or be set to None to disable/the default).
rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08'

#start_del = ("Beginning of Base64 image==")
end_del = ("==End of Base 64 image")
tx_success = ("==Transfer Success")

count=1
line=1
missing = []

curr_seq=0

while True:
    packet = None

    # check for packet rx
    packet = rfm69.receive()
    if packet is not None:
        prev_packet = packet
        try:
            (prev_packet).decode('utf-8')
            packet_text = str(prev_packet, "utf-8")
            rec = packet_text.split(",")
            filename=rec[0]
        except UnicodeError:
            print("string is not UTF-8")
        
        if (rec[2] == end_del):
            print("End Delimiter Recieved lets check file validity")
            time.sleep(10)
            while (packet_text != tx_success):
                packet = None
                packet = rfm69.receive()
                if packet is not None:
                    print("We are recieving a missing frame")
                    #run process output 
                    prev_packet = packet
                    try:
                        (prev_packet).decode('utf-8')
                        packet_text = str(prev_packet, "utf-8")
                        print (packet_text)
                        rec = packet_text.split(",")
                        f=open(rec[0],"a+")
                        f.write(rec[1]+","+rec[2]+"\n")
                    except UnicodeError:
                        print("string is not UTF-8")
                    
                else:
                    print("We will send a list of missing frames")
                    process_string = str("/home/pi/process_output " + str(filename))
                    print (process_string)
                    os.system(process_string)
                    #print the contents of missing
                    send_line = (str(rec[0]))
                    missing_filename="missing"+str(filename) 
                    if (os.stat(missing_filename).st_size > 0):
                        m=open(missing_filename,"r")
                        m1=m.readlines()
                        for l in m1:
                            send_line = ((str(rec[0])) + "," + (str(l)))
                            print("The request line:" + send_line)
                            send_file = bytes(str(send_line), "utf-8")
                            rfm69.send(send_file)
                            packet = rfm69.receive()
                            if packet is not None:
                                prev_packet = packet
                                try:
                                    (prev_packet).decode('utf-8')
                                    packet_text = str(prev_packet, "utf-8")
                                    rec = packet_text.split(",")
                                    f=open(rec[0],"a+")
                                    f.write(rec[1]+","+rec[2]+"\n")
                                    print (packet_text)
                                except UnicodeError:
                                    print("string is not UTF-8")
                    else:
                        print("There are no remaining missing seq nums")
                        packet_text = tx_success

        else: #this is not the end frame delimiter
            f=open(rec[0],"a+")
            f.write(rec[1]+","+rec[2]+"\n")
            print (packet_text)

Interpreting redundant data

#!/bin/sh

sort -V -o list.sorted beer.jpg
uniq list.sorted > unique
awk '{sub(/[^,]*/,"");sub(/,/,"")} 1' unique > cols_removed 
tr -d ' \t\n\r\f' < cols_removed > base64
cat base64 | base64 -d > image2.jpg