Mqtt Comunication(arduino - Raspberry)
by ΒαγγέληςΑ1 in Circuits > Arduino
597 Views, 0 Favorites, 0 Comments
Mqtt Comunication(arduino - Raspberry)
Ι Will try to make a simple home automation using MQTT protocol to communicate between Arduino and Raspberry.
The MQTT broker is install on a raspberry Pi Zero. Pi Zero using to make samething like a server which use same topics that we use to communicate with Arduino.
Architecture of Smart Home.
I use a router with Wi-Fi but I don not connect it in the internet. I make a local LAN network. No one from the outside can have access in the smart home, me nether. I make this project to handle if I am in the house, from my computer or my phone or tablet..
Schema of home is in images
We use in every scenario two LEDs to simulate the open close light or relay or everything want someone control in the room.
Supplies
I use one raspberry zero witch run the mqtt server
I use an Arduino uno with ethernet shield and a wifi wemos D1
I use a Wi-Fi Arduino
I use a modem-router
I use same LEDs and sensor to simulate home rooms.
Prepeare SD Card
First, insert the microSD card into your computer.
Now you need to install the right Raspberry Pi Imager for windows operating system. You can do this with the following link:
Once this is done, start the Imager and open the “CHOOSE OS” menu.
Scroll down the menu click “Other general-purpose OS”.
Here you you can select Ubuntu and see a list of download options.
For this project we use you select the Ubuntu Server 21.04. As indicated in the imager this will work for the Raspberry Pi 2,3, 3+ and any of the 4’s.
Open the “SD Card” menu. Select the microSD card you have inserted.
Finally, click “WRITE” and wait for the magic to happen… (This magic might take a few minutes)
If everything done write you se this window
Do not remove the sd card yet
Set up Wifi conf and Static IP
Getting setup with Wi-Fi
With the SD card still inserted in your laptop, open a file manager and locate the “system-boot” partition on the card. It contains initial configuration files that load during the first boot process.
Edit the network-config file to add your Wi-Fi credentials. An example is already included in the file, you can simply adapt it.
To do so, uncomment (remove the “#” at the beginning) and edit the following lines:
wifis:
wlan0:
dhcp4: true
optional: true
access-points:
<wifi network name>:
password: "<wifi password>"
For example:
wifis:
wlan0:
dhcp4: true
optional: true
access-points:
"home network":
password: "123456789"
Note: network name must be enclosed in quotation marks.
Save the file and extract the card from your laptop.
Note ⓘ: During the first boot, your Raspberry Pi will try to connect to this network. It will fail the first time around. Simply reboot sudo reboot and it will work.
Set a static IP
To set a static IP you need to replace the
dhcp4: true
line in the network-config file with lines that specify the intended IP address as well as its default gateway and DNS server. You can do this for either the eth0 or wlan0 interface (or both). It is important that you get the indenting right for this work correctly
For example, if you were planning to give the pi the address 192.100.1.23 in the 192.100.1.0/24 subnet with a default gateway of 192.100.1.1 and a DNS server of 192.100.1.53 then the following text would work. The same structure works for both the eth0 or wlan0 sections in the file:
ethernets:
eth0:
addresses:
- 192.100.101.23/24
gateway4: 192.100.1.1
nameservers:
addresses: [192.100.1.53]
optional: true
To enable SSH
Simple make a file SSH with out extension and add it in sd card.
Install Config MQTT
Connect the rasbery in power
And type in your windows terminal
arp -a | findstr b8-27-eb
Know use a client ssh I use MobaXterm
The password is ubuntu but after the first connection ask you to change the password
After change it is reboot automatically now give the new password to log in
Install mqtt
First we update the ubuntu
1. sudo apt-get update
2. sudo apt-get install
Here are the steps I used on Ubuntu.
- sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
- sudo apt-get update
- sudo apt-get install mosquitto
- sudo apt-get install mosquitto-clients
- sudo apt clean
Test open to terminal in the first one type
in the second send the message
in first terminal know you can see the message
Configuration topic and Password and acl file
Let’s configure Mosquitto to use passwords. First go to /etc/mosquitto/passwd. There an example of password file the pwfile you can open to see. The file contain key value rows key is the name and value is the hash md5 from passwords.
Mosquitto includes a utility to generate a special password file called mosquitto_passwd. This command will prompt you to enter a password for the specified username, and place the results in /etc/mosquitto/passwd
sudo mosquitto_passwd -c /etc/mosquitto/passwd homeautomation
Ask for a password
Now require to activate logins for all connections:
sudo nano /etc/mosquitto/conf.d/default.conf
Now need to make the default.conf
sudo nano default.conf
Paste in the following:
per_listener_settings true
allow_anonymous false
password_file /etc/mosquitto/passwd
allow_anonymous false will disable all non-authenticated connections, and the password_file line tells Mosquitto where to look for user and password information. Save and exit the file.
Now we need to restart Mosquitto and test our changes.
sudo systemctl restart mosquitto
Try to publish a message without a password:
mosquitto_pub -h localhost -t home -m "hello world"
The message should be rejected:
Before we try again with the password, switch to your second terminal window again, and subscribe to the ‘test’ topic, using the username and password this time:
mosquitto_sub -h localhost -t test -u "homeautomation" -P "home"
Now publish a message with your other terminal, again using the username and password:
mosquitto_pub -h localhost -t home -m "hello world" -u "homeautomation" -P "home"
Topic restrictions are configured on the Broker, and there is nothing to configure on the client.
Topic restriction is done in an access control list ( ACL ) file.
To enable topic restrictions you need to edit the mosquitto.conf file to use it.
Note: You should create a copy of the default file as a backup before you edit it.
You will find the setting in the Default authentication and topic access control section:
acl_file /etc/mosquito
A default file ( aclfile.example ) is provided with the installation.
Make a copy of file as acl
Open and make the changes
Because we need to have topic for every room we make two deferent topic one for bedroom and other for kitchen and add a new user allow publish message is the bedroom
Follow images
I forget the password of homeautomation in first after I make test and user but the password is not authenticated and in the end I make a new one the bedroom tha can use for authentication for all topics tha I use in home
Arduino
Final Test
Final result you can see in video. Sorry for the video.
I have give the best i could video editing but is the first time i try to synchronize two videos in one.