Deleting Mosquitto MQTT retained messages

I have tried sending a null message to the topics that I want to remove the retained messages for. But that is harder that it sounds, because most programs won’t let you send a null message. The sure fire way that I have found is the delete the persistence file. Deleting the persistence file can be easily done with just a few commands:

  • sudo service mosquitto stop
  • sudo rm /var/lib/mosquitto/mosquitto.db
  • sudo service mosquitto start

after that, all of the retained messages should be removed and mosquitto will begin to build a new persistence file for the current retained MQTT messages.

Advertisements

5″ Touch Screen Setup for Octoprint

Setting up Touch UI to use a 5″ HDMI display with octoprint. The screen I am using connects to the pi via the gpio and HDMI connector. Like this one: http://www.raspberrypiwiki.com/index.php/5_inch_HDMI_GPIO_Touch_Screen

First you need to set up the pi to display on the smaller screen by editing the /boot/config.txt file on the pi. To do this I followed the adafruit guide found here.

1. Sudo nano /boot/config.txt

2. Add these line to the bottom or uncomment and edit them in the default text

hdmi_group=2

hdmi_mode=1

hdmi_mode=87

hdmi_cvt=800 480 60 6 0 0 0

dtoverlay=ads7846,cs=1,penirq=25,penirq_pull=2,speed=50000,keep_vref_on=0,swapxy=0,pmax=255,xohms=150,xmin=200,xmax=3900,ymin=200,ymax=3900


dtoverlay=w1-gpio-pullup,gpiopin=4,extpullup=1

3. Save by CTRL+X, Y, ENTER

Now we need to set up the touch screen input

  • Sudo apt-get update
  • Sudo apt-get install xinput-calibrator

Now when you boot the pi, it will ask you to sign into octopi. But we don’t want to have to log in each time we turn on octoprint.

To get around this we need to have the pi boot to a browser and open the octoprint interface.

To do this I followed the guide by BillyBlaze starting with number 3.

  • Sudo apt-get update
  • sudo apt-get install --no-install-recommends xinit xinput xserver-xorg xserver-xorg-video-fbdev x11-xserver-utils matchbox unclutter chromium-browser
  • sudo nano /usr/share/X11/xorg.conf.d/99-fbdev.conf
  • Paste this into the new file:
  • Section "Device"
      Identifier "touchscreen"
      Driver "fbdev"
      Option "fbdev" "/dev/fb0"
    EndSection

The CTRL+X, Y, ENTER

  • sudo sed -i 's/allowed_users=console/allowed_users=anybody/' /etc/X11/Xwrapper.config

  • git clone https://github.com/BillyBlaze/OctoPrint-TouchUI-autostart.git ~/TouchUI-autostart/
  • sudo cp ~/TouchUI-autostart/touchui.init /etc/init.d/touchui
    sudo chmod +x /etc/init.d/touchui
    sudo cp ~/TouchUI-autostart/touchui.default /etc/default/touchui
    sudo update-rc.d touchui defaults

  • sudo apt-get upgrade
    sudo apt-get install rpi-update

  • sudo rpi-update
  • sudo reboot

By now the pi should be setup to display on a smaller screen, touch screen setup to work, and the pi to autoboot the chromium browser to the octoprint interface.

To make operating the octoprint interface we need to install the “Touch UI” plugin.

And that’s it. Now we need to print a nice case for the entire unit to mount to the printer. That is for the next post.


Set IP address of Raspberry Pi

To set a Static IP address for the Raspberry Pi you need to edit the dhcpcd.conf file 

  •  sudo nano /etc/dhcpcd.conf

Then scroll all the way to the bottom and add the following (be sure to change the numbers to match your requirements)

interface eth0

static ip_address=192.168.0.10/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

interface wlan0

static ip_address=192.168.0.200/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

Then sudo reboot and it should boot with the newly defined IP address

Octoprint setup

Write latest IMG

Edit octopi.txt , and octopi-network.txt in the /boot/ partition

Install MQTT plugin. Edit the ~/.octoprint/config.yaml and add 

plugins:
    mqtt:
        broker:
            # the broker's url, mandatory, if not configured the plugin will do nothing
            url: 127.0.0.1

            # the broker's port
            #port: 1883

            # the username to use to connect with the broker, if not set no user
            # credentials will be sent
            #username: unset

            # the password to use to connect with the broker, only used if a
            # username is supplied too
            #password: unset

            # the keepalive value for the broker connection
            #keepalive: 60

            # tls settings
            #tls:
                # path to the server's certificate file
                #ca_certs: unset

                # paths to the PEM encoded client certificate and private keys
                # respectively, must not be password protected, only necessary
                # if broker requires client certificate authentication
                #certfile: unset
                #keyfile: unset

                # a string specifying which encryption ciphers are allowable for this connection
                #ciphers: unset

            # configure verification of the server hostname in the server certificate.
            #tls_insecure: false

            # configure protocol version to use, valid values: MQTTv31 and MQTTv311
            #protocol: MQTTv31

        publish:
            # base topic under which to publish OctoPrint's messages
            #baseTopic: octoprint/

            # topic for events, appended to the base topic, '{event}' will
            # be substituted with the event name
            #eventTopic: event/{event}

            # topic for print and slicer progress, appended to the base topic, 
            # '{progress}' will be substituted with either 'printing' or 'slicing'
            #progressTopic: progress/{progress}

Install Email Notifier plugin

SSH and change to venv, install pip with pyparsing

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py
  • curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
  •  Sudo python get-pip.py
  • Pip install –upgrade pip

(These command installs everything at once after installing pip: source ~/oprint/bin/activate and then pip install keyrings.alt keyring yagmail  then  ~/oprint/bin/python -c “import yagmail; yagmail.register(‘myusername’, ‘mypassword’)” )
//Install keyring

  • //sudo pip install keyrings.alt

// install yagmail with sudo pip install yagmail[all]

//Set credentials with

  • //cd oprint/bin
  • //python
  • //import yagmai
  • //yagmail.register(“email@gmail.com”, “email_password”)

      Install DetailedProgess

      Add plugin 

      Copy this to the .octoprint/config.yaml inside the plugins:

      detailedprogress:
          # Number of seconds (minimum) to rotate the messages
          time_to_change: 10
          eta_strftime: "%H:%M:%S Day %d"
          etl_format: "{hours:02d}:{minutes:02d}:{seconds:02d}"
          # Messages to display. Placeholders:
          # - completion : The % completed
          # - printTimeLeft : A string in the format "HH:MM:SS" with how long the print still has left
          # - ETA : The date and time formatted in "%H:%M:%S Day %d" that the print is estimated to be completed
          # - filepos: The current position in the file currently being printed
          messages:
            - "{completion:.2f}% complete"
            - "ETL: {printTimeLeft}"
            - "ETA: {ETA}"

      Djdj

      Home Automation

      In an effort to simplify my Home Assistant install in the future. Here is the list of steps that I go through to install it.

      Prerequisites:

      • Download the latest copy of Raspbian image here.
      • Download and install Etcher to use it for writing the Raspbian image to the removable drive.

      Install Raspbian on a USB Flash Drive—

      I have had troubles with using a micro SD card to run Home Assistant on. After about 3 weeks it would just look up and I couldn’t even SSH into the pi to see what happened. I tried rebooting and nothing seemed to fix it unless I re-formatted the SD card and started all over. and then a few weeks later I would have the same issue again. I have read that this could be an issue with the all the writing to the SD card and so I am going to try running HA on a Flash Drive instead.

      To do that I follow this blog post by LazyManJoeto set up my Flash Drive and SD card. Here is the TLDR of that post:

      1. Format the Flash Drive with a FAT32 file system.
      2. Using Etcher, write the Raspbian .img to the Flash Drive.
      3. Open up the flash drive’s root directory and add a text file that is named “ssh” (this is required to enable ssh by default.
      4. Format the SD Card and name the partition “boot”.
      5. Copy all of the contents from the Flash Drvie’s “boot” partition to the SD card.
      6. Edit the “cmdline.txt” file on the SD card by deleting the text that reads root=/dev/mmcblk0p2 and replace it with root=/dev/sda2.
      7. Save the file. This tells the Pi to boot from the USB drive instead of the SD card.
      8. Put both the SD card and the Flash Drive into the pi and apply power.
      9. To expand the partition of the Flash Drive, preform the following steps:
        • SSH into the pi with user “pi” and the default password “raspberry”
        • sudo fdisk /dev/sda
        • press p to see the partitions.
        • note the start position for the sda2 partion. it will be something like 137216.
        • press d then 2 then ENTER (this will delete the 2nd partition).
        • press n then p then 2 then ENTER (this will start creating the new partition).
        • provide the start position that as noted before.
        • leave the end position blank to use the entire drive.
        • press w to commit the changes.
        • you may get an error message about the partition table is busy, but that is ok.
        • sudo reboot
        • once rebooted, sudo resize2fs /dev/sda2 (*This didn’t fork bot me, but df -h showed  it was ok).
        • sudo reboot
        • confirm the changes by checking the allocated space by df -h

      Install Home Assistant —

      To install home assistant I like to follow along with Ben’s BRUH Automation post. On that post he walks through setting put the pi, installing HA, Samba, Duck DNS and Let’s Encrypt. You can follow along his post for more detail. I just like the using his post for setting up the pi, installing HA and Samba. I currently don’t have my pi’s port forwarded to the internet and I just use my HA only when I am on my home wifi network.

      1. passwd to change the pasword for the user pi. You will need to enter the current password “raspberry” and then your new password twice.
      2. sudo apt-get update
      3. sudo apt-get upgrade (press Y when prompted)
      4. sudo raspi-config (no need to expand the file system, so just change the time zone.)
      5. sudo reboot
      6. I use the pi 3’s wifi for an access point for my devices and have it connected to my home network via an Ethernet cable. So I skip the section where Ben describes setting up the wlan0.
      7. Now its time to install HA. Copy the All-In-One (AIO) command found on HA webpage.
      8. Run the AIO command as described on the webpage. *
        • If it fails within the firest few minuets, read a few lines above where it fails and it may tell you a command that needs to run to fix something to allow the install to complete. after running that command, re-run the AIO command.
      9. Enter the password for the use pi when prompted. (about 15- 20 minuets into the install). The install will take a few hours.
      10. sudo reboot

      Install SAMBA —

      SAMBA is how I edit my HA configuration.yaml file from my Windows PC over the network.

      1. sudo apt-get install SAMBA
      2. sudo nano /etc/samba/smb.conf
      3. press and hold control-k to clear initial config, paste the following configuration
      4. [global]
        netbios name = raspberrypi
        server string = The Pi File Center
        workgroup = WORKGROUP
        hosts allow =
        remote announce =
        remote browse sync =
        
        [HOME ASSISTANT]
        path = /home/homeassistant/.homeassistant
        comment = No comment
        browsable = yes
        read only = no
        valid users =
        writable = yes
        guest ok = yes
        public = yes
        create mask = 0777
        directory mask = 0777
        force user = root
        force create mode = 0777
        force directory mode = 0777
        hosts allow =
      5. sudo smbpasswd -a pi
      6. type a new password went prompted
      7. sudo service smbd restart

      Install HostAPD —

      HostAPD is the software that sets up an access point using the pi 3’s on-board wifi adapter. I use this to setup a new wireless network for all my home automation sensors to connect to. I like to keeps these separate from my usual home wifi network to keep down on the number of ip addresses and network traffic on my primary wifi.

      To setup HostAPD, I like to follow along with a post on the lean.adafruit.com website. I have tried to consolidate these steps here.

      1. sudo apt-get install hostapd isc-dhcp-server
      2. sudo apt-get install iptables-persistent
      3. On the two config screens, select yes to both of them.
      4. sudo nano /etc/dhcp/dhcpd.conf
      5. Find the two lines that say
        • option domain-name “example.org”;
          option domain-name-servers ns1.example.org, ns2.example.org;

      6. Comment them both out by adding a # in front of them.
        • #option domain-name “example.org”;
          #option domain-name-servers ns1.example.org, ns2.example.org;

      7. Now find the lines that read
        • # If this DHCP server is the official DHCP server for the local
          # network, the authoritative directive should be uncommented.
          #authoritative;

      8. And un-comment the last line by deleting the #, so it reads
          • # If this DHCP server is the official DHCP server for the local
          • # network, the authoritative directive should be uncommented.
          • authoritative;
      9. Then scroll down to he bottom and add the following lines.
        • subnet 192.168.42.0 netmask 255.255.255.0 {
          range 192.168.42.10 192.168.42.50;
          option broadcast-address 192.168.42.255;
          option routers 192.168.42.1;
          default-lease-time 600;
          max-lease-time 7200;
          option domain-name “local”;
          option domain-name-servers 8.8.8.8, 8.8.4.4;
          }

      10. Save the file by pressing Control-x then y then Enter
      11. sudo nano /etc/default/isc-dhcp-server
      12. Find the test that reads, INTERFACES=”” and change it to INTERFACES=”wlan0″
      13. Save the file by pressing Control-x then y then Enter
      14. sudo ifdown wlan0
      15. sudo nano /etc/network/interfaces
      16. Keep the line that says allow-hotplug wlan0, but comment out any other lines dealing with the wlan0 (we don’t want the lines that start with iface or have the wpa-supplicant.conf file)
      17. Add the lines
        • iface wlan0 inet static
          address 192.168.42.1
          netmask 255.255.255.0

      18. Save the file by pressing Control-x then y then Enter
      19. sudo ifconfig wlan0 192.168.42.1
      20. sudo nano /etc/hostapd/hostapd.conf
      21. Add the following lines. Be sure to change the ssid= and wpa_passphrase= lines to correspond with what you want you network to have.
        • interface=wlan0
          #driver=rtl871xdrv   #driver=nl80211
          ssid=Pi_AP
          country_code=US
          hw_mode=g
          channel=6
          macaddr_acl=0
          auth_algs=1
          ignore_broadcast_ssid=0
          wpa=2
          wpa_passphrase=Raspberry
          wpa_key_mgmt=WPA-PSK
          wpa_pairwise=CCMP
          wpa_group_rekey=86400
          ieee80211n=1
          wme_enabled=1

        • The code above is for using the Raspberry pi 3’s on-board wifi. If using a USB wifi adapter then try using one of the drivers that is listed in the second line by un-commenting the line.
      22. Save the file by pressing Control-x then y then Enter
      23. sudo nano /etc/default/hostapd
      24. Find the line #DAEMON_CONF=”” and change it to DAEMON_CONF=”/etc/hostapd/hostapd.conf”, (don’t forget to remove the # in front)
      25. Save the file by pressing Control-x then y then Enter
      26. sudo nano /etc/init.d/hostapd
      27. Find the line DAEMON_CONF= and change it to DAEMON_CONF=/etc/hostapd/hostapd.conf
      28. Save the file by pressing Control-x then y then Enter
      29. sudo nano /etc/sysctl.conf
      30. Scroll to the bottom and add net.ipv4.ip_forward=1 on a new line.
      31. Save the file by pressing Control-x then y then Enter
      32. sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward
      33. sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
      34. sudo iptables -A FORWARD -i eth0 -o wlan0 -m state –state RELATED,ESTABLISHED -j ACCEPT
      35. sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
      36. sudo sh -c “iptables-save > /etc/iptables/rules.v4”
      37. Time to test it….
      38. sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf to start it manually
      39. If you see any errors or warnings, please check the Adafruit tutorial for help.
      40. Try to connect to the newly created wifi network using the wireless device of your choice.
      41. Once everything is working, run sudo service hostapd start
      42. sudo service isc-dhcp-server start
      43. sudo update-rc.d hostapd enable
      44. sudo update-rc.d isc-dhcp-server enable

      Setup Mosquitto MQTT —

      When the All-In-One command is used to install Home Assistant, it also installs Mosquitto to be used as a MQTT broker. It has a base configuration that is good, but you need to setup a username and password for your IoT devices to use to connect. To do this I like to follow along with Ben’s BRUH Automation video in which he describes the process of setting it up.

      1. sudo nano /etc/mosquitto/mosquitto.conf
      2. Make sure these lines can be found somewhere in the mosquitto.conf file and that they are not commented out.
        •  allow_anonymous false
          password_file /etc/mosquitto/pwfile
          listener 1883

      3. Save the file by pressing Control-x then y then Enter
      4. sudo mosquitto_passwd -c /etc/mosquitto/pwfile <the user name you want> (i.e. sudo mosquitto_passwd -c /etc/mosquitto/pwfile username)
      5. Enter in a new password for that user when prompted.
      6. sudo reboot

      Creating a backup —

      Now that you have spent an entire day getting this set up and it seems to be working great. you should create a new image of the Flash Drive so if anything happens you can just re-write this new image to the Flash Drive and ahead of all these step. To do this I use Win32 Disk Imager.

      1. sudo shutdown -h now
      2. Remove the Flash Drive and insert it into your computer.
      3. Open Win32 Disk Imager and select the flash drive.
      4. Browse to a location on your hard drive and enter in a new file name to save the image as with a .img file extension.
      5. Click the read button and wait for it to complete.

      Now that you have a backup .img, if anything happens to your system, you can recreate it in a matter of minuets instead of hours.

      Future for this blog

      I am an electrical engineer by trade and a maker by heart.

      This blog will be used to share my ideas and projects with the world.

      Project ideas that I have planned:
      DIY Homemade Table Saw with a twist
      DIY Homemade Router Table
      DIY Homemade Dust collector
      Hardware store 3d printer
      And many other little projects

      I like things that are super simple yet very clever and have multiple uses around my shop.

      Stay tuned… This is going to be good!