Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:setup:broker [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:setup:broker [2023/03/02 10:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-=====Raspberry as a broker=====+=====MQTT broker=====
  
-On this page we will explain how to turn your Raspberry Pi 3 (also works with 3+) into a broker with Wifi access point. Having the Raspberry Pi working as an access point means that you will not need an additional router (NB. we have not tested this under heavy load). If you have a spare router and do not wish to install our image then you can follow the instructions [[https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/|here]] on how to turn any Raspberry Pi into a broker.+This page will explain how to turn your Raspberry Pi 3 (also works with 3+) into a broker with Wifi access point. Having the Raspberry Pi working as an access point means you will not need an additional router (NB. we have not tested this under heavy load). If you have a spare router and do not wish to install our imageyou can follow the instructions [[https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/|here]] on how to turn any Raspberry Pi into a broker.
  
  
Line 8: Line 8:
 {{:software:raspibroker.zip| Raspberry broker}} {{:software:raspibroker.zip| Raspberry broker}}
  
-And you can find a guide on how to install it [[https://www.raspberrypi.org/documentation/installation/installing-images/|here.]] You will need a 16GB (or larger) micro-SD card since the original image was on such a card.+And you can find a guide on how to install it [[https://www.raspberrypi.org/documentation/installation/installing-images/|here.]] You will need a 16GB (or larger) micro-SD card since the original image was on a 16 GB card.  
 +The easiest way in Linux is to download the zip file, unzip it and by using Startu Disck creator, write the image to a new SD card.
  
-Once you have flashed the image to the SD-card you should insert it to your Raspberry and boot it up. The Wifi-AP service should activate on its own. But to activate the MQTT broker enter <code>mosquitto -d</code> to the terminal window (you can use SSH or monitor and keyboard to do this). You can use the terminal on the Raspbian to confirm if your broker is working. For this open a terminal window and enter <code>mosquitto_sub -t "mytopic"</code> Then open another terminal window and enter <code>mosquitto_pub -t "mytopic" -m "This is my message"</code> 
  
-If this works then the next thing you should try is connecting your laptop (or other) device to the Raspberry and test publishing and subscribing to messages with it. A good program for debugging is [[en:iot:examples:setup:debugging|MQTTBox]]. You can find the default Wifi name and password below.+Once you have flashed the image to the SD card, insert it into your Raspberry and boot it upThe Wifi-AP service should activate on its own. But to start the MQTT broker, enter <code>mosquitto -d</code> to the terminal window (you can use SSH or monitor and keyboard to do this). You can use the terminal on Raspbian to confirm if your broker is workingFor this, open a terminal window and enter <code>mosquitto_sub -t "mytopic"</code> Then open another terminal window and enter <code>mosquitto_pub -t "mytopic" -m "This is my message"</code>
  
-NB!! Try to avoid turning off Raspberry by removing the power cordsince it can cause the SD card to become corrupted. You should always turn the Raspberry off via GUI or typing //sudo shutdown now// to the terminal (using screen and keyboard, or SSH).+If this works, then you should next try connecting your laptop (or other) device to Raspberry and test publishing and subscribing to messages with it. A good program for debugging is [[en:iot:examples:setup:debugging|MQTTBox]]. You can find the default Wifi name and password below. 
 + 
 +NB!! Avoid turning off Raspberry by removing the power cord since it can cause the SD card to become corrupted. You should always turn the Raspberry off via GUI or typing //sudo shutdown now// to the terminal (using screen and keyboard, or SSH)
 +Upgrading Rasbian may stop the initial system from working. Consider it when upgrading and keep a copy of important configuration files or the original system.
  
 =====Passwords and other data for the raspberry broker===== =====Passwords and other data for the raspberry broker=====
Line 20: Line 23:
 **IP address of Raspberry Pi (for broker and SSH):** **IP address of Raspberry Pi (for broker and SSH):**
  
-192.168.4.1:22+192.168.4.1
  
 Port for MQTT broker (without password): 1883 Port for MQTT broker (without password): 1883
Line 36: Line 39:
 password: piIoT123 password: piIoT123
  
 +===== Testing =====
 +
 +To test that everything works, use the IoT Basic kit.
 +
 +Connect one Controller module with the Button module and upload the following test code to this module:
 +
 +Libraries in platformio.ini
 +<code>
 +lib_deps = ITTIoT, blackketter/Switch
 +</code>
 +
 +Source code
 +<code c>
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Switch.h>
 +
 +#define MODULE_TOPIC "rgb"
 +const byte buttonPin = D3;
 +
 +Switch button = Switch(buttonPin);
 +
 +void iot_connected()
 +{
 +  iot.log("Button online!");
 +}
 + 
 +void setup()
 +{
 +  iot.setConfig("wname", "raspberryIoT");
 +  iot.setConfig("wpass", "piIoT123");
 +  iot.setConfig("msrv", "192.168.4.1");
 +  iot.setConfig("mport", "1883");
 +  iot.setup(); 
 +}
 + 
 +void loop()
 +{
 +  iot.handle(); 
 +  button.poll();
 + 
 +  if (button.released()) {
 +    iot.log("Released");
 +    iot.publishMsgTo(MODULE_TOPIC,"255;0;0",0);
 +  }
 +  if (button.pushed()) {
 +    iot.log("ButtonPushed");
 +    iot.publishMsgTo(MODULE_TOPIC,"0;255;0",0);
 +  }
 +
 +  delay(3);
 +}
 +</code>
 +
 +then connect the second Controller module with RGB module and upload the following code:
 +
 +Libraries in platformio.ini
 +<code>
 +lib_deps = ITTIoT, Adafruit NeoPixel
 +</code>
 +
 +Source code
 +<code c>
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Adafruit_NeoPixel.h>
 + 
 +#define MODULE_TOPIC "rgb"
 +#define PIN            D2
 +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
 + 
 +String getValue(String data, char separator, int index)
 +{
 +  int found = 0;
 +  int strIndex[] = {0, -1};
 +  int maxIndex = data.length()-1;
 + 
 +  for(int i=0; i<=maxIndex && found<=index; i++)
 +  {
 +    if(data.charAt(i)==separator || i==maxIndex)
 +    {
 +        found++;
 +        strIndex[0] = strIndex[1]+1;
 +        strIndex[1] = (i == maxIndex) ? i+1 : i;
 +    }
 +  }
 +  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
 +}
 + 
 +void iot_received(String topic, String msg)
 +{
 +  if(topic == MODULE_TOPIC)
 +  {
 +    String r = getValue(msg,';',0);
 +    String g = getValue(msg,';',1);
 +    String b = getValue(msg,';',2);
 +    pixels.setPixelColor(0, r.toInt(), g.toInt(), b.toInt()); 
 +    pixels.show(); 
 +  }
 +}
 + 
 +void iot_connected()
 +{
 +  iot.subscribe(MODULE_TOPIC);
 +  iot.log("IoT NeoPixel example!");
 +}
 + 
 +void setup()
 +{
 +  iot.setConfig("wname", "raspberryIoT");
 +  iot.setConfig("wpass", "piIoT123");
 +  iot.setConfig("msrv", "192.168.4.1");
 +
 +  iot.setup();
 + 
 +  pixels.begin(); 
 +  pixels.setPixelColor(0, 255, 255, 255);
 +  pixels.show(); 
 +}
 + 
 +void loop()
 +{
 +  iot.handle(); 
 +  delay(200); 
 +}
 +</code>
  
 +Power up Raspberry Pi and both modules. Wait until RGB lights up, and then try to press the button on the Button module. RGB module colour should turn green. If this happens, everything is set up correctly. Raspberry Pi serves wifi and MQTT broker service in the given parameters.
  
 +{{:en:iot:examples:setup:broker.jpg?400|}}
  
en/iot/examples/setup/broker.1595244384.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0