This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:setup:broker [2020/07/20 11:26] – external edit 127.0.0.1 | en: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:// | + | This page will explain how to turn your Raspberry Pi 3 (also works with 3+) into a broker with a 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 image, you can follow the instructions [[https:// |
Line 8: | Line 8: | ||
{{: | {{: | ||
- | And you can find a guide on how to install it [[https:// | + | And you can find a guide on how to install it [[https:// |
+ | 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 < | ||
- | If this works then the next thing you should try is connecting your laptop (or other) device | + | Once you have flashed the image to the SD card, insert it into your Raspberry and boot it up. The Wifi-AP service should activate on its own. But to start the MQTT broker, enter < |
- | NB!! Try to 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). | + | 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: |
+ | |||
+ | 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 | ||
+ | < | ||
+ | lib_deps = ITTIoT, blackketter/ | ||
+ | </ | ||
+ | |||
+ | Source code | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define MODULE_TOPIC " | ||
+ | const byte buttonPin = D3; | ||
+ | |||
+ | Switch button = Switch(buttonPin); | ||
+ | |||
+ | void iot_connected() | ||
+ | { | ||
+ | iot.log(" | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | iot.setConfig(" | ||
+ | iot.setConfig(" | ||
+ | iot.setConfig(" | ||
+ | iot.setConfig(" | ||
+ | iot.setup(); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | iot.handle(); | ||
+ | button.poll(); | ||
+ | |||
+ | if (button.released()) { | ||
+ | iot.log(" | ||
+ | iot.publishMsgTo(MODULE_TOPIC," | ||
+ | } | ||
+ | if (button.pushed()) { | ||
+ | iot.log(" | ||
+ | iot.publishMsgTo(MODULE_TOPIC," | ||
+ | } | ||
+ | |||
+ | delay(3); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | then connect the second Controller module with RGB module and upload the following code: | ||
+ | |||
+ | Libraries in platformio.ini | ||
+ | < | ||
+ | lib_deps = ITTIoT, Adafruit NeoPixel | ||
+ | </ | ||
+ | |||
+ | Source code | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define MODULE_TOPIC " | ||
+ | #define PIN D2 | ||
+ | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, | ||
+ | |||
+ | 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< | ||
+ | { | ||
+ | if(data.charAt(i)==separator || i==maxIndex) | ||
+ | { | ||
+ | found++; | ||
+ | strIndex[0] = strIndex[1]+1; | ||
+ | strIndex[1] = (i == maxIndex) ? i+1 : i; | ||
+ | } | ||
+ | } | ||
+ | return found> | ||
+ | } | ||
+ | |||
+ | void iot_received(String topic, String msg) | ||
+ | { | ||
+ | if(topic == MODULE_TOPIC) | ||
+ | { | ||
+ | String r = getValue(msg,';', | ||
+ | String g = getValue(msg,';', | ||
+ | String b = getValue(msg,';', | ||
+ | pixels.setPixelColor(0, | ||
+ | pixels.show(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void iot_connected() | ||
+ | { | ||
+ | iot.subscribe(MODULE_TOPIC); | ||
+ | iot.log(" | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | iot.setConfig(" | ||
+ | iot.setConfig(" | ||
+ | iot.setConfig(" | ||
+ | |||
+ | iot.setup(); | ||
+ | |||
+ | pixels.begin(); | ||
+ | pixels.setPixelColor(0, | ||
+ | pixels.show(); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | iot.handle(); | ||
+ | delay(200); | ||
+ | } | ||
+ | </ | ||
+ | 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. | ||
+ | {{: | ||