The host Raspberry Pi requires some software setup to be able to access all features of the Machinon board, such as the SPI-UART serial ports and the RTC, and operate as a headless embedded system.
The steps below are based on starting with a clean install of the official Raspian Stretch Lite 2018-04-18 SD card image.
- Write standard Raspian Lite image to SD card.
- With SD card still in PC card reader, create new empty file
ssh
in root directory (/boot partition) to enable SSH login. - Edit
/boot/config.txt
and add the following lines:dtoverlay=sc16is752-spi1,24 # add support for SC16IS752 SPI-UART expander chip dtoverlay=i2c-rtc,mcp7941x # add support for RTC chip dtoverlay=pi3-act-led, gpio=26 # move ACT LED function to GPIO26 dtoverlay=pi3-miniuart-bt # swap "miniuart" and PL011 UART for better machinon serial comms (for Pi3 only) dtoverlay=pi3-disable-wifi # optional to disable wifi dtoverlay=pi3-disable-bt # optional to disable bluetooth enable_uart=1 # enable UART for Pi3 and Jessie or later
- Edit
/boot/cmdline.txt
and remove the textconsole=serial0,115200
to allow applications to use serial port. See https://www.raspberrypi.org/documentation/configuration/uart.md for more info. - Connect the Pi to the Machinon board with the GPIO ribbon cable and power up. Wait for the Pi to boot, then find its IP and log in via SSH (use PuTTY or similar SSH client)
- Run raspi-config and:
- Enable SPI
- Enable I2C
- Enable Serial (but disable login shell over serial) (should already be done by the previous edits to config.txt and cmdline.txt)
- Expand filesystem to fill card
- Change GPU memory to 16 MB (not essential)
- Edit
/etc/modules
and add a new line withrtc-mcp7941x
- Edit
/lib/udev/hwclock-set
and comment out (add # to start of lines) the lines:if [ -e /run/systemd/system ] ; then exit 0 fi
- Reboot and check that the Pi has correct time from network. Then optionally manually set HW clock with
sudo hwclock -w
to write system time to HW clock. The Pi will automatically load the time/date from the HW clock at boot. This can can be forced manually withsudo hwclock -r
to set the system clock from the HW clock. The Pi does an NTP update of system clock at boot, and then every 1024 secs (17 mins) thereafter, and sets the RTC from this. - Optionally set static IP:
- Edit /etc/dhcpcd.conf and uncomment or add these lines (change to suit):
interface eth0 static ip_address=192.168.1.15/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1
- Alternatively, edit the "fall back to static IP" section to make the Pi fall back to that static IP if DHCP fails.
- Reboot for network changes to take effect
- Edit /etc/dhcpcd.conf and uncomment or add these lines (change to suit):
- Add permanent aliases for the SPI UARTs (Domoticz does not show port names like "ttySC1"):
- create a new udev rules file
/etc/udev/rules.d/98-minibms.rules
with:KERNEL=="ttySC0" SYMLINK="serial2" KERNEL=="ttySC1" SYMLINK="serial3"
- Save file and reboot
- Check for the aliases serial2 and serial3:
ls -l /dev
(serial0 and serial1 are the Pi internal ports)
- create a new udev rules file
- Change the default password, or optionally add a new user to run everything as instead of "pi". See https://mattwilcox.net/web-development/setting-up-a-secure-home-web-server-with-raspberry-pi for ideas.
- Install any other desired updates, packages, etc. For Domoticz suggestions see https://www.domoticz.com/wiki/Raspberry_Pi#Raspberry_Pi_additional_software_installations
The Machinon software support includes a set of PHP web forms to make the I/O configuration simple. This requires a web server that supports PHP, such as NGINX.
See https://www.raspberrypi.org/documentation/remote-access/web-server/nginx.md and https://howtoraspberrypi.com/install-nginx-raspbian-and-accelerate-your-raspberry-web-server/ for NGINX install details.
- Install NGINX (and php-fpm if required. On Raspian Stretch this will install PHP7.0)
sudo apt install nginx php-fpm
- Create a new NGINX config file based on the default config:
cd /etc/nginx/sites-available sudo cp default /etc/nginx/conf.d/nginx-machinon.conf
- Update the new nginx-machinon.conf file with the content below:
# pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; # With php-cgi (or other tcp sockets):` #fastcgi_pass 127.0.0.1:9000; }
- Disable the default config by deleting the "sites-enabled" symbolic link:
sudo rm /etc/nginx/sites-enabled/default
- Reload nginx config:
sudo nginx -s reload
- Copy the config forms PHP files and resources from https://github.com/EdddieN/machinon/tree/master/files/config-forms to the NGINX web pages directory, under a "config" subdirectory (typically
/var/www/html/config/
), and set file/directory permissions to allow NGINXwww-data
user to access the files and execute the .sh script:sudo chown -R www-data /var/www/html sudo chmod -R 770 /var/www/html/ sudo usermod -a -G dialout www-data sudo usermod -a -G www-data pi
- Test the config forms by browsing to
https://<IP_of_machinon>/config
which should show the AIN channels configuration form with the existing inputs configuration. Make some changes and click Submit. The form should reload and show the new configuration.
NGINX can also proxy the web pages served by Domoticz or other automation servers, allowing HTTPS access on the same port as the config pages if required. Set Domoticz to use a non-standard port such as 8080 (for HTTP) or 4443 (for HTTPS), and update the nginx-machinon.conf (created above) with the following content. Then reload the config as above.
# Optionally Redirect all HTTP requests to HTTPS
#server {
# listen 80;
# #listen [::]:80;
# server_name localhost;
# return 302 https://$host$request_uri;
#}
# Default server configuration
server {
# Optionally listen on HTTP port 80 (comment out the 80 -> 443 redirect above)
listen 80 default_server;
# SSL configuration
listen 443 ssl default_server;
#listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
# Replace with your own certs if you have them.
include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location = / {
# Redirect requests for "/" to the Domoticz directory
# Change to match your actual installation directory
rewrite ^ $scheme://$host/domoticz/ ;
}
location / {
# Redirect requests for files in "/" to the Domoticz directory
#rewrite ^(/)$ $scheme://$host/domoticz/ ;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7 fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# With php5 fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
location /domoticz/{
# Optionally add authentication, unless the host software has login authentication
#auth_basic "Restricted";
#auth_basic_user_file /etc/nginx/.htpasswd;
# NB: set Domoticz to use port 4443 for SSL (or port 0 to disable SSL), so that the default 443 can be used by NGINX
# Pass requests to Domoticz HTTPS port
#proxy_pass https://localhost:4443/;
# OR Pass requests to Domoticz HTTP port
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
}
# deny access to .htaccess files, if Apache's document root concurs with nginx's one
location ~ /\.ht {
deny all;
}
# deny access to .sh files
location ~\.sh$ {
deny all;
# fake a "not found" response
return 404;
}
}
There are two front-panel status LEDs directly connected to the 40-way GPIO header on the Machinon board. These can be controlled by Raspberry pi GPIO pins.
To indicate Raspberry Pi SD card access on the Machinon front panel red "ACT/disk" LED D4, the green "ACT" LED on the Pi3 can be redirected to the GPIO26 pin using the pi3-act-led
overlay. See the system installation section above for details.
The "ACT" LED can also be controlled directly (NB this disables the normal "SD activity" function!):
ON: echo 1 | sudo tee /sys/class/leds/led0/brightness
OFF: echo 0 | sudo tee /sys/class/leds/led0/brightness
To restore the SD ACT function for LED0:
echo mmc0 | sudo tee /sys/class/leds/led0/trigger
The Machinon front panel "user" green LED D5 is connected to Pi GPIO13 and can be controlled manually or from a user script:
# Enable GPIO 13 and set to output
echo "13" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio13/direction
# Write a 1/high to GPIO13 to turn LED ON
echo "1" > /sys/class/gpio/gpio13/value
# Write a 0/low to GPIO13 to turn LED OFF
echo "0" > /sys/class/gpio/gpio13/value
# Clean up (disables GPIO pin)
echo "13" > /sys/class/gpio/unexport
The Machinon board uses two Atmel XMEGA microcontrollers (main and slave) to handle its I/O and internal functions. Each microcontroller has a bootloader that allows firmware updates using the AVR109/AVR1605 serial protocol supported by programming software such as AVRDUDE.
The main XMEGA bootloader is triggered by hardware reset from the host Raspberry Pi (MySensors command message or GPIO pulse). The slave XMEGA bootload is handled through the main XMEGA and is triggered with a MySensors command message. The Machinon support software includes shell scripts to handle the firmware update using AVRDUDE.
The bootloaders operate at 115200 bits/sec on the configuration port (same serial settings as for configuration messages). This is normally ttySC1 on the Raspberry Pi.
The Raspberry Pi repository includes AVRDUDE 6.3 as at June 2018. Install with:
sudo apt-get install avrdude
And check that it works (this should print the version number):
avrdude -v
Copy the scripts from https://github.com/EdddieN/machinon/tree/master/files/scripts to a suitable directory on the Pi (eg home folder /home/pi/
), and make them executable with:
sudo chmod ug+x *.sh
To update the main XMEGA firmware using the fwupdate.sh
support script:
- Copy the new firmware HEX file to the directory that contains the script and rename it to
machinon_main.hex
. - Run the updater script with the
-m
parameter to perform the bootload:
./fwupdate.sh -m
To update the main XMEGA firmware manually:
- Send the "run main bootloader" command (a MySensors format message) on the configuration port:
OR
IFS=$'\n' stty -F /dev/ttySC1 raw ispeed 115200 ospeed 115200 cs8 -crtscts echo "0;1;1;0;25;1" > /dev/ttySC1
Reset the XMEGA directly. Run the Machinon support scriptreset-machinon.sh -m
or manually generate a >10ms reset pulse on GPIO23 with the steps below:echo "23" > /sys/class/gpio/export sleep 0.5 # allow time for filesystem changes echo "out" > /sys/class/gpio/gpio23/direction echo "1" > /sys/class/gpio/gpio23/value sleep 0.05 # wait for 50 ms to ensure that AVR resets echo "0" > /sys/class/gpio/gpio23/value echo "23" > /sys/class/gpio/unexport
- Run AVRDude within 5 seconds to do the bootload:
avrdude -v -p atxmega256a3u -c avr109 -P /dev/ttySC1 -b 115200 -U flash:w:machinon_main.hex:i -e
To update the slave XMEGA firmware using the support script:
- Copy the new firmware HEX file to the directory that contains the script and rename it to
machinon_slave.hex
. - Run the updater script with the
-s
parameter to perform the bootload:./fwupdate.sh -s
To update the slave XMEGA firmware manually:
- Send the "run slave bootloader" command (a MySensors format message) on the configuration port:
IFS=$'\n' stty -F /dev/ttySC1 raw ispeed 115200 ospeed 115200 cs8 -crtscts echo "0;1;1;0;25;2" > /dev/ttySC1
- Run AVRDude within 5 seconds to do the bootload:
avrdude -v -p atxmega64a3u -c avr109 -P /dev/ttySC1 -b 115200 -U flash:w:machinon_slave.hex:i -e