howtophil icon

sendfilemesh.sh

howtophil | PRO | 02/19/24 10:42:19 PM UTC (Edited) | 0 ⭐ | 645 👁️ | Never ⏰ | [BASH, CLI, base64, meshtastic, send files, don't actually do this, probably a bad idea, maybe not terrible]
Bash |

2.87 KB

|

None

|

0 👍

/

0 👎

#!/bin/bash
#------------------------------------------------
# An UNTESTED example of sending a binary file
# as a series of messages over Meshtastic
# after the file is converted to base64.
# The meshtastic commands are echoed to stdout
# instead of actually being issued in this script.
# I want to show what commands would be sent
# rather than sending the commands at this point.
# Try not to bring down your local mesh with too
# many data messages, kids.
# ~HowToPhil 2024/02/19 02:40 EST
#------------------------------------------------
# Added md5 checksums for each previous line
# to make error detection more managed.
# ~HowToPhil 20214/02/19 13:55 EST
#------------------------------------------------
# ./sendmeshfile.sh [filename] '[destNodeID]'
#------------------------------------------------
 
# Prep the line count variable
LINECOUNT=0
 
#PREVLINEMD5=$(echo ""|md5sum)
PREVLINEMD5="FIRSTLINE  -"
 
# Put the arguments into pretty variables
FILENAME=$1
NODEID=$2
 
# Get the md5 of the last line of the base64 encoded file.
# Getting this when the loop ends is rough, so I just
# get it now and save it for later.
LASTLINEMD5=$(base64 "$FILENAME" | tail -n1 |md5sum)
 
# Count how many lines/texts long the base64 encoded file will be
# for adding to the header before sending the file.
# I encode it before sending to get the number of lines
# so my header can include how many messages long the file
# will be and then I send the header message.
TOTALTEXTS=$(base64 "$FILENAME" |wc -l)
echo "meshtastic --dest $NODEID --ack --sendtext \"Sending $FILENAME as base64 in $TOTALTEXTS texts\""
 
# Now begins the bulk of the workload delivering the file as
# a base64 encoded text with checksums included.
base64 "$FILENAME" | (while read line; do
    #-------START OLD UNUSED TEST THOUGHT CODE------#
    # For line numbering
    #LINECOUNT=$[$LINECOUNT +1]
    #
    # This would prepend a line number so missed lines could be requested again
    # The receiver would need to strip the line numbers after putting them in order
    #meshtastic --dest $NODEID --ack --sendtext "$LINECOUNT,$line";
    #-------END OLD UNUSED TEST THOUGHT CODE------#
    
    # I have not tested but waiting for ack might make lines go through better
    # and in order, in which case, line numbers would not be needed...
    echo "meshtastic --dest $NODEID --ack --sendtext \"$PREVLINEMD5 $line\";"
 
    # md5 checksum for "previous" line in next loop
    # This way each line can include error checking
    # to make sure the file is being sent and received
    # properly.
    PREVLINEMD5=$(echo "$line" |md5sum)
done)
 
# Send the md5 of the last line of the base64 encoded file
echo "meshtastic --dest $NODEID --ack --sendtext \"$LASTLINEMD5 LASTLINE\";"
 
# Send footer to be sure the other side knows we think we're done
echo "meshtastic --dest $NODEID --ack --sendtext \"Sent $FILENAME as base64 in $TOTALTEXTS texts.\""
 
 

Comments

  • howtophil icon
    howtophil PRO
    02/19/24 07:05:29 AM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Thoughts: I think I'd have the other side count up the lines and report missing lines... maybe include the line number either way... I'd also do the --ack and have the sending machine pause and "watch for node return" before sending the line again and continuing...
  • howtophil icon
    howtophil PRO
    02/19/24 06:02:31 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Updating the send side of the script. After I decide on a "solid sending format" I can work on the receivefilemesh.sh script.
  • howtophil icon
    howtophil PRO
    02/19/24 06:18:15 PM UTC (Edited)
    Plain Text |

    0 B

    |

    👍

    /

    👎