Tuesday, February 09, 2021

PimgStack Part 2 (again) - The Joy of MQTT

My attempt at using AWS SQS for pushing messages to my Raspberry Pi based digital photo frame (aka PimgStack) was a bust. I needed a lighter-weight solution for pushing messages to the Pi. MQTT provides message passing services and does so while being featherweight, so I decided to give it a try.

The Proof of Concept

First, I headed to an AWS EC2 Linux server and installed and started mosquitto:

# Server IP: 192.168.1.213
$ sudo yum install mosquitto
...
$ mosquitto -v
1612876887: mosquitto version 1.6.10 starting
1612876887: Using default config.
1612876887: Opening ipv4 listen socket on port 1883.
1612876887: Opening ipv6 listen socket on port 1883.
1612876888: New connection from ... on port 1883.
1612876888: New client connected from ... as mosqsub|2145-pimgstack1 (p2, c1, k60).
...

I also had to update the server's security group to allow all traffic for port 1883. (This was a temporary measure, of course. Once my proof-of-concept was done, I turned this off.)

I then connected up to my Raspberry Pi, installed mosquitto-clients and kicked off the mosquitto_sub command.

$  sudo apt install mosquitto-clients
...
$ mosquitto_sub -h 192.168.1.213 -t foo/bar -C 1

-h connects to the server above. -t says to listen on topic foo/bar and -C says to listen for a single message and quit.

Running the above command just hung there. That was perfect.

Finally, I connected to a third Linux box, installed mosquitto there, and invoked mosquito_pub like so:

$ sudo yum install mosquitto
...
$ mosquitto_pub -h 192.168.1.213 -t foo/bar -m "Hello World"

To my shock and amazement, "Hello World" was printed on the Pi's screen and the command exited.

I had just demonstrated the pieces of the puzzle needed to power PimgStack.

The Real Deal

Now that I had a way to receive messages from the cloud, I needed to implement code to handle those messages. That was the easy part. You can see the entire source code for the PimgStack Raspberry Pi client here, however the interesting part is as follows:

while true; do
  message=$(mos_sub pimgstack/1)
  case $message in
    push:*)
      do_push $message
      ;;

    pop)
      do_pop
      ;;

    clear)
      do_clear
      ;;
    *)
      do_error $message
      ;;
  esac
  img_display
done

The above sets up an infinite loop to receive messages. The shell wrapper function mos_sub is invoked to receive messages. When a message arrives, it is trivially parsed. The stack operations 'push', 'pop' and 'clear' are defined, everything else is ignored.

Once the stack is modified, img_display is called and the top most image on the stack is shown. If the stack is empty, a placeholder image is shown.

At this point, I had a very clunky, but functional digital photo frame. The commands below push two images, displaying them each, then pops the top one off, showing the first image that was pushed.

$ mosquitto_pub  -h 192.168.1.213 -t pimgstack/1 -m push:https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPoJ3YPU-CU74S9vR_dS0Qjon4BY8QB0aRgjToWVGEg6d3BM9DjugGBktFAODEGK19cAEtKVpfLs8SkIg7Phln7p8Romo2BPLJGMbjvzafBv21TLMljqsBxkGLATuioU-ZCipM/s4032/20200808_200622.jpg
$ mosquitto_pub  -h 192.168.1.213 -t pimgstack/1 -m push:https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGAEMuswEi-6PRl80WPhyphenhyphenJ_QP7oAxxabWO-4qWTbY9lez-0sd5JfUtDHvzmKLghuaPu8vrTLFXY-c4ZJ5J2X3GflMPAUAqSiwX6yNoUg1SfbuAPT1kVtGgtMDIgPoae6rpZBCk/s4032/20200809_093032.jpg
$ mosquitto_pub  -h 192.168.1.213 -t pimgstack/1 -m pop

The Raspberry Pi side of the photo frame is now finished. Up next, I need to implement sending MQTT messages from my Android device. And then I'll be ready to push and pop images from my phone with ease.

No comments:

Post a Comment