Thursday, August 23, 2018

Installing Ubuntu 18.04 32bits on Raspberry Pi 3

Ubuntu 18.04 32bits
Raspberry Pi 3
Python 2.7.15 / 3.6.5
NodeJS 8.10.0
MySQL 5.7.23


Goal:
  • Install Ubuntu on Raspberry Pi 3 with Python, Mosquitto, MySQL and NodeJS.

Status:
  • 2018.09.15 - Devices don't work for more than 5 min. It dosen't responds to ping. As these devices (Temp/Hum and Door sensors) only read sensors and publish to Broker, maybe the problem is with this version of Broker or an incompatibility with Ubuntu 18.04 for Raspberry Pi 3, or a hardware issue. When we pointed out all devices to other version of Broker in another computer, there's no problem anymore. Action: Mount another machine, with Ubuntu Mate 16.04.2 running on Raspberry Pi 2 to be an IoT Server.
  • 2018.08.23 - Ubuntu, Python, MySQL, Mosquitto and NodeJS are working.
Installing Ubuntu:
  • Download Ubuntu Image
  • Install DDRescue and unzip:
  • sudo apt-get install gddrescue xz-utils
  • unxz ubuntu-18.04-preinstalled-server-armhf+raspi3.img.xz
  • Identify MicroSD device name (/dev/sdX) and write to it:
  • lsblk
  • sudo ddrescue -D --force ubuntu-18.04-preinstalled-server-armhf+raspi3.img /dev/sdX
  • First Boot with the SD card: (User: ubuntu  Password: ubuntu)
  • Connect with WiFi:
    • nano /etc/netplan/*.yaml  (e.g. 50-cloud-init.yaml)
      • For wireless dhcp addressing:


network:
  version: 2
  renderer: networkd
  wifis:
    wlp3s0:
      dhcp4: yes
      access-points:
        "network_ssid_name":
          password: "**********"


Finishing the Wifi configuration:
  • sudo netplan --debug generate      # make config files
  • sudo netplan apply       # apply new configuration
  • reboot.      # must reboot

  • Once connected to Internet, installing optional PPAs:
  • sudo add-apt-repository ppa:ubuntu-raspi2/ppa
  • sudo apt update
  • Upgrade new packages (optional):
    • sudo apot upgrade
  • Change server hostname (optional):
    • sudo hostnamectl set-hostname <HOSTNAME>
    • sudo nano /etc/cloud/cloud.cfg
    • Change  preserve_hostname: false  to:
    • preserve_hostname: true
    • sudo nano /etc/hosts
    • Add a new line after  "127.0.0.1 localhost"
    • 127.0.1.1 <HOSTNAME>
  • Installing Python:
    • sudo apt install python python3 python-minimal python-pip python-mysqldb
    • Install Paho MQTT Python Client:
    • pip install paho-mqtt
    • Install SQLAlchemy:
    • pip install SQLAlchemy==1.2.11
  • Install Mosquitto MQTT Server:
    • sudo apt update
    • sudo apt-get install mosquitto mosquitto-clients
    • Start Mosquitto Server:
      • mosquitto -d
  • Installing MySQL:
    • sudo apt install mysql-server
    • sudo mysql_secure_installation
    • To configure the root account to authenticate with a password, run the following "alter user" command. Then, run "flush privileges" which tells the server to reload the grant tables and put your new changes into effect:
    • sudo mysql
    • alter user 'root'@'localhost' identified with mysql_native_password by 'password';
    • flush privileges;
    • Creating a new user (iotdbuser) and granting privileges:
    • create user 'iotdbuser'@'%' IDENTIFIED BY 'user_password';
    • grant all privileges on iotdb.* to 'iotdbuser'@'localhost';
    • To connect to MySQL server remotely using MySQL client, edit the /etc/mysql/mysql.conf.d/mysqld.cnf configuration:
    • sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    • And change the "bind-address" from  127.0.0.1  to:
    • bind-address = 0.0.0.0
    • And grant access to root user from network:
    • grant all on *.* to root@'%' identified by 'root-password';
    • Once ready, reboot your MySQL server:
    • sudo service mysql restart
  • Installing NodeJS:
    • sudo apt install nodejs npm
    • Create a directory for the NodeJS projects:
    • mkdir ~/iot/wsk_cp
    • cd ~/iot/wsk_cp
    • npm init –y
    • sudo npm install mysql
    • sudo npm install express -g
    • sudo npm install request -g
    • sudo npm install ws -g

Testing:
  • Testing Mosquitto:
  • mosquitto_sub -t "outTopic"
  • mosquitto_pub -t "outTopic" -m "hello world!"
  • Testing MySQL:
  • mysql -V
  • sudo mysql
  • select user,authentication_string,plugin,host FROM mysql.user;
  • Connect to MySQL server over network:
  • mysql -u root -p '' -h <MYSQL_HOST_IP>
Troubleshooting:
    • .
    References:
    If you like this content, feel free to