Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X
This is the second part of a ThingBerry related blog post series.
ThingBerry is ThingWorx installed on a RaspBerry Pi, which can be used for portable demonstrations without the need of utilizing e.g. customer networks. Instead the ThingBerry provides its own custom WIFI hotspot and allows Things to connect and send / receive demo data on a small scale.
In this particual blog post we'll discuss on how to setup the ThingBerry as a WIFI hotspot to directly connect with mobile devices or other Raspberry Pis.
As the ThingBerry is a highly unsupported environment for ThingWorx, please see this blog post for all related warnings.
In case this guide looks familiar, it's probably because it's based on https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/
As the ThingBerry is currently connected via ethernet we can utilize the Raspberry Pi's WIFI connection to create a private network where all the wireless devices can connect to, e.g. another Raspberry Pi or a ESP8266
First we need to install dnsmasq and hostapd. Those will help setting up the access point and create a private DNS server to dynamically assign IP-addresses to connecting devices.
sudo apt-get install dnsmasq hostapd
We will need to configure the wlan0 interface with a static IP. For this the dhcpcd needs to ignore the wlan0 interface.
sudo nano /etc/dhcpcd.conf
Paste the following content to the end of the file. This must be ABOVE any interface lines you may have added earlier!
denyinterfaces wlan0
Save and exit.
Let's now configure the static IP.
sudo nano /etc/network/interfaces
Comment out ALL lines for the wlan* configurations (e.g. wlan0, wlan1). By default there are three lines which need to be commented out by adding a # at the beginning of the line. After this the wlan0 can be pasted in:
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
Save and exit.
Now restart the dhcpcd service and reload the wlan0 configuration with
sudo service dhcpcd restart
sudo ifdown wlan0
sudo ifup wlan0
Hostapd is used to configure the actual WIFI hot spot, e.g. the SSID and the WIFI password (wpa_passphrase) that's required to connect to this network.
sudo nano /etc/hostapd/hostapd.conf
Paste the following content:
interface=wlan0
driver=nl80211
ssid=thingberry
hw_mode=g
channel=6
wmm_enabled=1
ieee80211n=1
country_code=DE
macaddr_acl=0
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=changeme
rsn_pairwise=CCMP
If you prefer another SSID or a more secure password, please ensure updating above configuration!
Save and exit.
Check if the configuration is working via
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
It should return correctly, without any errors and finally show "wlan0: AP-ENABLED". With this you can now connect to the "thingberry" SSID.
However there's no IP assigned automatically - so that sucks can be improved...
Stop hostapd with CTRL+C and let's start it on boot.
sudo nano /etc/default/hostapd
At the end of the file, paste the following content:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Save and exit.
Dnsmasq allows to assign dynamic IP addresses. Let's backup the original configuration file and create a new one.
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
Paste the following content:
interface=wlan0
listen-address=192.168.0.1
bind-interfaces
dhcp-range=192.168.0.100,192.168.0.199,255.255.255.0,12h
Save and exit.
This will make the DNS service listen on 192.168.0.1 and assign IP addresses between 192.168.0.100 and 192.168.0.199 with a 12 hour lease.
Next step is to setup the IPV4 forwarding for the wlan0 interface.
sudo nano /etc/sysctl.conf
Uncomment the following line:
You can search in nano with CTRL+W
net.ipv4.ip_forward=1
Save and exit.
To be able to call the ThingBerry with its actual hostname, the hostname needs to be mapped in the host configuration.
sudo nano /etc/hosts
Search the line with your hostname and update it to the local IP address (as configured in the listen-address above), e.g.
192.168.0.1 thingberry
Save and exit.
Please note that this is the hostname and not the SSID of the network!
Run the following command to enable the fowarding and reboot.
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo reboot
The ThingBerry is independent of any internet traffic.
However if your connected devices or the ThingBerry itself need to contact the internet, the WIFI connection needs to route those packages to and from the (plugged-in) ethernet device.
This can done through iptables
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
All traffic will be internet related traffic will be forwarded between eth0 and wlan0 and vice versa.
To load this configuration every time the ThingBerry is booted, it needs to be saved via
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Run this file by editing
sudo nano /etc/rc.local
Above the line exit 0 add the following line:
iptables-restore < /etc/iptables.ipv4.nat
Save and exit.
Connect to the new WIFI hotspot via the SSID and the password configured earlier through any WIFI capable device.
When connecting to your new access point check your device's IP settings (in iOS, Android or a laptop / desktop device). It should show a 192.168.0.x IP address and should be pingable from the ThingBerry console.
Connect to http://192.168.0.1/Thingworx or http://<servername>/Thingworx to open the Composer.