Last active
March 2, 2022 14:26
-
-
Save BrianRossmajer/fe30f5739dfc3cfedb84f0cb6b4bff15 to your computer and use it in GitHub Desktop.
TinyCore Raspberry Pi minimal USB RFID reader or USB barcode reader to MQTT gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Have an old Pi that would corrupt its SD cards with normal Pi OS's, so used TinyCore linux for pi instead | |
# which only writes to SD card when you run `filetool.sh -b` ... basically it's become more like a | |
# embedded microcontroller that posts to MQTT when a tag or barcode is scanned. | |
# after following the tinycore pi setup instructions and getting its networking going, I | |
tce-load -wi ntp | |
tce-load -wi perl5 | |
tce-load -wo cpanm | |
echo /usr/local/bin/ntpdate time.chu.nrc.ca >> /opt/bootlocal.sh | |
echo /usr/local/bin/ntpd >> /opt/bootlocal.sh | |
cpanm Net::MQTT::Simple | |
# install stuff to run "instead of" the normal console. Use flock so only one copy runs | |
echo >> ~tc/.ashrc 'flock -n /dev/shm/gateway perl -Iperl5/lib/perl5 gateway.pl' | |
# this currently relies on my mqtt broker publishing the time every second, and updates | |
# a watchdog entry every ten seconds. Without that, might be better to just do a loop around tick() | |
# instead of using run() | |
cat > gateway.pl <<EOF | |
use Net::MQTT::Simple; | |
use IO::Select; | |
$b=Net::MQTT::Simple->new("mqtt-broker.example.com"); | |
$b->last_will("rfids/pi","down",1); | |
$s = IO::Select->new(); | |
$s->add(\*STDIN); | |
$b->run("time/seconds"=>sub{ | |
my $t=time(); | |
if ($s->can_read(.5)) { | |
chomp($tag = <STDIN>); | |
$b->publish("rfids/$tag",$t); | |
} elsif ( substr($t,-1) == '0' ) { | |
$b->retain("rfids/pi",$t); | |
} | |
}); | |
EOF | |
sudo mount /mnt/mmcblk0p1 | |
# add consoleblank=0 to /mnt/mmcblk0p1/cmdline.txt | |
# otherwise the screen saver will kick in and block keystrokes | |
filetool.sh -b | |
sudo reboot | |
# you should see the gateway watchdog update in MQTT and entries for each thing scanned |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment