diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/examples/Boards/STM32F746NG-DISCOVERY/Ethernet_MQTT_Adafruit.io/Ethernet_MQTT_Adafruit.io.ino b/examples/Boards/STM32F746NG-DISCOVERY/Ethernet_MQTT_Adafruit.io/Ethernet_MQTT_Adafruit.io.ino new file mode 100644 index 0000000..c3030e3 --- /dev/null +++ b/examples/Boards/STM32F746NG-DISCOVERY/Ethernet_MQTT_Adafruit.io/Ethernet_MQTT_Adafruit.io.ino @@ -0,0 +1,160 @@ +/* + STM32F746G-DISCOVERY MQTT example to publish on https://io.adafruit.com broker + + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview + + This sketch demonstrates the capabilities of the pubsub library in combination + with the STM32F746G-DISCOVERY board. + + It will use several components of the board and you need to install corresponding libraries : + - Ethernet (STM32Ethernet) : https://github.com/stm32duino/STM32Ethernet + - Temperature and humidity sensor (HTS221) : https://github.com/stm32duino/HTS221 + - Arduino Client for MQTT: https://github.com/knolleary/pubsubclient + + To get temperature and humidity you will need the X-NUCLEO-IKS01A1, a motion MEMS and + environmental sensor expansion board for the STM32 board. + + You can find more information on the board and exapansion board here : + http://www.st.com/en/evaluation-tools/32f746gdiscovery.html + http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-sense-hw/x-nucleo-iks01a1.html) + + You will need to create some feeds in your Adafruit IO Dashboard. + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io + to know how register and create your Adafruit IO dashboard and feeds: + - hello: Text block + - onoff: Toggle block + - temp: Stream block + - hum: Gauge block + A screenshot of the dashboard is available in the img/ directory: dashboard_adafruit.png + + It connects to the Adafruit IO's MQTT server (a.k.a broker) server then: + - publishes announcement "Hi, I'm STM32 user!" to the topic 'AIO_USERNAME"/feeds/hello"' + - subscribes to the topic AIO_USERNAME"/feeds/onoff"', switching LED_BUILTIN state + - publishes temperature and humidity from the HTS221 sensor to the topic + 'AIO_USERNAME"/feeds/temp"' and 'AIO_USERNAME"/feeds/hum"' every 10 seconds + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. + +*/ +#include <LwIP.h> +#include <STM32Ethernet.h> +#include <PubSubClient.h> +#include <HTS221Sensor.h> + +// Adafruit.io Setup +#define AIO_SERVER "io.adafruit.com" +#define AIO_SERVERPORT 1883 +#define AIO_USERNAME "......" +#define AIO_KEY "........" + +// Etherrnet setup +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +// Set the static IP address to use if the DHCP fails to assign +IPAddress ip(192, 168, 0, 177); +EthernetClient STClient; + +// i2c sensors +HTS221Sensor *HumTemp; + +PubSubClient client(STClient); +long lastMsg = 0; +char msg[8]; + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output + Serial.begin(115200); + + client.setServer(AIO_SERVER, AIO_SERVERPORT); + client.setCallback(callback); + + // Start the Ethernet connection: + if (Ethernet.begin(mac) == 0) { + Serial.println("Failed to configure Ethernet using DHCP"); + // Try to congifure using IP address instead of DHCP: + Ethernet.begin(mac, ip); + } + // Allow the hardware to sort itself out + delay(1500); + + // Initialize I2C bus. + Wire.begin(); + + // Initlialize components. + HumTemp = new HTS221Sensor (&Wire); + HumTemp->Enable(); +} + +void callback(char* topic, byte* payload, unsigned int length) { + +#if 0 + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (unsigned int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); +#else + UNUSED(topic); +#endif + if (length > 1) { + // Switch on/off the LED (payload messages can be 'ON' or 'OFF') + if ((char)payload[1] == 'N') { + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on + } else { + digitalWrite(LED_BUILTIN, LOW); // Turn the LED off + } + } +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + // Note - the default maximum packet size is 128 bytes. If the + // combined length of clientId, username and password exceed this, + // you will need to increase the value of MQTT_MAX_PACKET_SIZE in + // PubSubClient.h + if (client.connect("STM32Client", AIO_USERNAME, AIO_KEY)) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish(AIO_USERNAME"/feeds/hello", "Hi, I'm STM32 user!"); + // ... and resubscribe + client.subscribe(AIO_USERNAME"/feeds/onoff"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +void loop() { + float temperature, humidity; + if (!client.connected()) { + reconnect(); + } + client.loop(); + + long now = millis(); + if (now - lastMsg > 10000) { + lastMsg = now; + HumTemp->GetTemperature(&temperature); + dtostrf(temperature, 2, 2, msg); + Serial.print("Publish temperature "); + Serial.println(msg); + client.publish(AIO_USERNAME"/feeds/temp", msg); + + HumTemp->GetHumidity(&humidity); + snprintf (msg, 8, "%u", (unsigned int)humidity); + Serial.print("Publish humidity "); + Serial.println(msg); + client.publish(AIO_USERNAME"/feeds/hum", msg); + } +} diff --git a/examples/Boards/STM32F746NG-DISCOVERY/Ethernet_MQTT_Adafruit.io/README.md b/examples/Boards/STM32F746NG-DISCOVERY/Ethernet_MQTT_Adafruit.io/README.md new file mode 100644 index 0000000..a4381ba --- /dev/null +++ b/examples/Boards/STM32F746NG-DISCOVERY/Ethernet_MQTT_Adafruit.io/README.md @@ -0,0 +1,39 @@ +# Ethernet_MQTT_Adafruit.io + + This example demonstrates the capabilities of the [PubSubClient](https://github.com/knolleary/pubsubclient) library in combination + with the [STM32F746G-DISCOVERY](http://www.st.com/en/evaluation-tools/32f746gdiscovery.html) board to publish on https://io.adafruit.com broker + + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview + +# Dependencies + +Install the following libraries using the Arduino IDE: **_Sketch -> Include Library -> Manage Libraries_** and search: + + * [PubSubClient](https://github.com/knolleary/pubsubclient): Arduino Client for MQTT. + * [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet): Ethernet library for STM32 based board with on-board Ethernet connector. + * [STM32duino HTS221](https://github.com/stm32duino/HTS221): Ethernet library for STM32 based board with on-board Ethernet connector. + +# Hardware + +To get temperature and humidity you will need the [X-NUCLEO-IKS01A1](http://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32-nucleo-expansion-boards/stm32-ode-sense-hw/x-nucleo-iks01a1.html), a motion MEMS and environmental sensor expansion board for the STM32 board. + +# Adafruit IO Dashboard. + + You will need to create some feeds in your Adafruit IO Dashboard. + + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io + + to know how register and create your Adafruit IO dashboard and feeds: + * hello: Text block + * onoff: Toggle block + * temp: Stream block + * hum: Gauge block + + A screenshot of the dashboard: + [[/img/dashboard_adafruit.png|alt="Dashboard Adafruit"]] + + It connects to the Adafruit IO's MQTT server (a.k.a broker) server then: + * publishes announcement "_Hi, I'm STM32 user!_" to the topic `AIO_USERNAME"/feeds/hello"` + * subscribes to the topic `AIO_USERNAME"/feeds/onoff"`, switching `LED_BUILTIN` state + * publishes temperature and humidity from the HTS221 sensor to the topic + `AIO_USERNAME"/feeds/temp"` and `AIO_USERNAME"/feeds/hum"` every 10 seconds diff --git a/examples/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino b/examples/Boards/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino similarity index 99% rename from examples/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino rename to examples/Boards/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino index 11ab5ee..721abe1 100644 --- a/examples/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino +++ b/examples/Boards/STM32L475VG-DISCOVERY-IOT/BTLE_sensors_TimeOfFlight_demo/BTLE_sensors_TimeOfFlight_demo.ino @@ -122,7 +122,7 @@ boolean swipe_detected() { int gesture_code; int status; boolean ret = false; - + sensor_vl53l0x->StartMeasurement(); int top_done = 0; @@ -339,7 +339,7 @@ void setup() { /* Configure the User Button in GPIO Mode */ pinMode(USER_BTN, INPUT); - + // Initialize I2C bus. dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL); dev_i2c->begin(); diff --git a/examples/Boards/STM32L475VG-DISCOVERY-IOT/WiFi_MQTT_Adafruit.io/README.md b/examples/Boards/STM32L475VG-DISCOVERY-IOT/WiFi_MQTT_Adafruit.io/README.md new file mode 100644 index 0000000..9923b2d --- /dev/null +++ b/examples/Boards/STM32L475VG-DISCOVERY-IOT/WiFi_MQTT_Adafruit.io/README.md @@ -0,0 +1,35 @@ +# WiFi_MQTT_Adafruit.io + + This example demonstrates the capabilities of the [PubSubClient](https://github.com/knolleary/pubsubclient) library in combination + with the [B-L475E-IOT01A](http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html) board to publish on https://io.adafruit.com broker + + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview + +# Dependencies + +Install the following libraries using the Arduino IDE: **_Sketch -> Include Library -> Manage Libraries_** and search: + + * [PubSubClient](https://github.com/knolleary/pubsubclient): Arduino Client for MQTT. + * [STM32duino ISM43362-M3G-L44](https://github.com/stm32duino/WiFi-ISM43362-M3G-L44): WiFi library for the [B-L475E-IOT01A](http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html) board. + * [STM32duino HTS221](https://github.com/stm32duino/HTS221): Ethernet library for STM32 based board with on-board Ethernet connector. + +# Adafruit IO Dashboard. + + You will need to create some feeds in your Adafruit IO Dashboard. + + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io + + to know how register and create your Adafruit IO dashboard and feeds: + * hello: Text block + * onoff: Toggle block + * temp: Stream block + * hum: Gauge block + + A screenshot of the dashboard: + [[/img/dashboard_adafruit.png|alt="Dashboard Adafruit"]] + + It connects to the Adafruit IO's MQTT server (a.k.a broker) server then: + * publishes announcement "_Hi, I'm STM32 user!_" to the topic `AIO_USERNAME"/feeds/hello"` + * subscribes to the topic `AIO_USERNAME"/feeds/onoff"`, switching `LED_BUILTIN` state + * publishes temperature and humidity from the HTS221 sensor to the topic + `AIO_USERNAME"/feeds/temp"` and `AIO_USERNAME"/feeds/hum"` every 10 seconds diff --git a/examples/Boards/STM32L475VG-DISCOVERY-IOT/WiFi_MQTT_Adafruit.io/WiFi_MQTT_Adafruit.io.ino b/examples/Boards/STM32L475VG-DISCOVERY-IOT/WiFi_MQTT_Adafruit.io/WiFi_MQTT_Adafruit.io.ino new file mode 100644 index 0000000..0c42ccf --- /dev/null +++ b/examples/Boards/STM32L475VG-DISCOVERY-IOT/WiFi_MQTT_Adafruit.io/WiFi_MQTT_Adafruit.io.ino @@ -0,0 +1,193 @@ +/* + B-L475E-IOT01A MQTT example to publish on https://io.adafruit.com broker + + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/overview + + This sketch demonstrates the capabilities of the pubsub library in combination + with the B-L475E-IOT01A board. + + It will use several components of the board and you need to install corresponding libraries : + - WiFi (ISM43362-M3G-L44) : https://github.com/stm32duino/WiFi-ISM43362-M3G-L44 + - Temperature and humidity sensor (HTS221) : https://github.com/stm32duino/HTS221 + - Arduino Client for MQTT: https://github.com/knolleary/pubsubclient + + You can find more information on this board here : + http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html + + You will need to create some feeds in your Adafruit IO Dashboard. + See https://learn.adafruit.com/mqtt-adafruit-io-and-you/getting-started-on-adafruit-io + to know how register and create your Adafruit IO dashboard and feeds: + - hello: Text block + - onoff: Toggle block + - temp: Stream block + - hum: Gauge block + A screenshot of the dashboard is available in the sketch directory: dashboard_adafruit.png + + It connects to the Adafruit IO's MQTT server (a.k.a broker) server then: + - publishes announcement "Hi, I'm STM32 user!" to the topic 'AIO_USERNAME"/feeds/hello"' + - subscribes to the topic AIO_USERNAME"/feeds/onoff"', switching LED_BUILTIN state + - publishes temperature and humidity from the HTS221 sensor to the topic + 'AIO_USERNAME"/feeds/temp"' and 'AIO_USERNAME"/feeds/hum"' every 10 seconds + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. + +*/ +#include <SPI.h> +#include <WiFiST.h> +#include <PubSubClient.h> +#include <HTS221Sensor.h> + +// Update these with values suitable for your network. +char ssid[] = "......."; +const char* password = "........"; + +// Adafruit.io Setup +#define AIO_SERVER "io.adafruit.com" +#define AIO_SERVERPORT 1883 +#define AIO_USERNAME "......" +#define AIO_KEY "........" + +// WiFi module setup +SPIClass SPI_3(PC12, PC11, PC10); +WiFiClass WiFi(&SPI_3, PE0, PE1, PE8, PB13); +WiFiClient STClient; +int status = WL_IDLE_STATUS; // the Wifi radio's status + +// i2c sensors +HTS221Sensor *HumTemp; + +TwoWire *dev_i2c; +#define I2C2_SCL PB10 +#define I2C2_SDA PB11 + + +PubSubClient client(STClient); +long lastMsg = 0; +char msg[8]; + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output + Serial.begin(115200); + setup_wifi(); + client.setServer(AIO_SERVER, AIO_SERVERPORT); + client.setCallback(callback); + + // Initialize I2C bus. + dev_i2c = new TwoWire(I2C2_SDA, I2C2_SCL); + dev_i2c->begin(); + + // Initlialize components. + HumTemp = new HTS221Sensor (dev_i2c); + HumTemp->Enable(); +} + +void setup_wifi() { + + delay(10); + + // Initialize the WiFi module: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi module not detected"); + // don't continue: + while (true); + } + + // Print firmware version: + String fv = WiFi.firmwareVersion(); + Serial.print("Firmware version: "); + Serial.println(fv); + + if (fv != "C3.5.2.3.BETA9") { + Serial.println("Please upgrade the firmware"); + } + + // Attempt to connect to Wifi network: + Serial.print("Attempting to connect to network: "); + Serial.println(ssid); + while (status != WL_CONNECTED) { + Serial.print("."); + // Connect to network: + status = WiFi.begin(ssid, password); + // Wait 10 seconds for connection: + delay(10000); + } + + Serial.println(); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + +#if 0 + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (unsigned int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); +#else + UNUSED(topic); +#endif + if (length > 1) { + // Switch on/off the LED (payload messages can be 'ON' or 'OFF') + if ((char)payload[1] == 'N') { + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on + } else { + digitalWrite(LED_BUILTIN, LOW); // Turn the LED off + } + } +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + // Note - the default maximum packet size is 128 bytes. If the + // combined length of clientId, username and password exceed this, + // you will need to increase the value of MQTT_MAX_PACKET_SIZE in + // PubSubClient.h + if (client.connect("STM32Client", AIO_USERNAME, AIO_KEY)) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish(AIO_USERNAME"/feeds/hello", "Hi, I'm STM32 user!"); + // ... and resubscribe + client.subscribe(AIO_USERNAME"/feeds/onoff"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +void loop() { + float temperature, humidity; + if (!client.connected()) { + reconnect(); + } + client.loop(); + + long now = millis(); + if (now - lastMsg > 10000) { + lastMsg = now; + HumTemp->GetTemperature(&temperature); + dtostrf(temperature, 2, 2, msg); + Serial.print("Publish temperature "); + Serial.println(msg); + client.publish(AIO_USERNAME"/feeds/temp", msg); + + HumTemp->GetHumidity(&humidity); + snprintf (msg, 8, "%u", (unsigned int)humidity); + Serial.print("Publish humidity "); + Serial.println(msg); + client.publish(AIO_USERNAME"/feeds/hum", msg); + } +} diff --git a/examples/Communication/MQTT/MQTTClient/Hello/Hello.ino b/examples/Communication/MQTT/MQTTClient/Hello/Hello.ino new file mode 100644 index 0000000..1aa3a70 --- /dev/null +++ b/examples/Communication/MQTT/MQTTClient/Hello/Hello.ino @@ -0,0 +1,158 @@ +/******************************************************************************* + * Copyright (c) 2014, 2015 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial contribution + * Benjamin Cabe - adapt to IPStack, and add Yun instructions + * Ian Craggs - remove sprintfs to reduce sketch size + * + * Modified by Frederic Pillon to use the STM32 Ethernet Library + * Only includes have changed: + * #include <SPI.h> + * #include <Ethernet.h> + * Replaced by: + * #include <LwIP.h> + * #include <STM32Ethernet.h> + *******************************************************************************/ + +#define WARN Serial.println + +#define MQTTCLIENT_QOS2 1 + + +#include <LwIP.h> +#include <STM32Ethernet.h> +#include <IPStack.h> +#include <Countdown.h> +#include <MQTTClient.h> + +int arrivedcount = 0; + +void messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + + Serial.print("Message "); + Serial.print(++arrivedcount); + Serial.print(" arrived: qos "); + Serial.print(message.qos); + Serial.print(", retained "); + Serial.print(message.retained); + Serial.print(", dup "); + Serial.print(message.dup); + Serial.print(", packetid "); + Serial.println(message.id); + Serial.print("Payload "); + Serial.println((char*)message.payload); +} + + +EthernetClient c; // replace by a YunClient if running on a Yun +IPStack ipstack(c); +MQTT::Client<IPStack, Countdown, 50, 1> client = MQTT::Client<IPStack, Countdown, 50, 1>(ipstack); + +byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; // replace with your device's MAC +const char* topic = "arduino-sample"; + +void connect() +{ + char hostname[] = "iot.eclipse.org"; + int port = 1883; + + Serial.print("Connecting to "); + Serial.print(hostname); + Serial.print(":"); + Serial.println(port); + + int rc = ipstack.connect(hostname, port); + if (rc != 1) + { + Serial.print("rc from TCP connect is "); + Serial.println(rc); + } + + Serial.println("MQTT connecting"); + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = (char*)"arduino-sample"; + rc = client.connect(data); + if (rc != 0) + { + Serial.print("rc from MQTT connect is "); + Serial.println(rc); + } + Serial.println("MQTT connected"); + + rc = client.subscribe(topic, MQTT::QOS2, messageArrived); + if (rc != 0) + { + Serial.print("rc from MQTT subscribe is "); + Serial.println(rc); + } + Serial.println("MQTT subscribed"); +} + +void setup() +{ + Serial.begin(9600); + Ethernet.begin(mac); + Serial.println("MQTT Hello example"); + connect(); +} + +MQTT::Message message; + +void loop() +{ + if (!client.isConnected()) + connect(); + + arrivedcount = 0; + + // Send and receive QoS 0 message + char buf[100]; + strcpy(buf, "Hello World! QoS 0 message"); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + int rc = client.publish(topic, message); + while (arrivedcount == 0) + { + Serial.println("Waiting for QoS 0 message"); + client.yield(1000); + } + + // Send and receive QoS 1 message + strcpy(buf, "Hello World! QoS 1 message"); + message.qos = MQTT::QOS1; + message.payloadlen = strlen(buf)+1; + rc = client.publish(topic, message); + while (arrivedcount == 1) + { + Serial.println("Waiting for QoS 1 message"); + client.yield(1000); + } + + // Send and receive QoS 2 message + strcpy(buf, "Hello World! QoS 2 message"); + message.qos = MQTT::QOS2; + message.payloadlen = strlen(buf)+1; + rc = client.publish(topic, message); + while (arrivedcount == 2) + { + Serial.println("Waiting for QoS 2 message"); + client.yield(1000); + } + delay(2000); +} diff --git a/examples/Communication/MQTT/MQTTClient/README.md b/examples/Communication/MQTT/MQTTClient/README.md new file mode 100644 index 0000000..ad844b2 --- /dev/null +++ b/examples/Communication/MQTT/MQTTClient/README.md @@ -0,0 +1,25 @@ +# Paho MQTT C client + +Required to install prebuilt Arduino port of MQTTClient. + +See http://www.eclipse.org/paho/clients/c/embedded/ + +Download the prebuilt Arduino port of MQTTClient and in the Arduino IDE use +**_Sketch -> Include Library -> Add .ZIP Library..._** with the downloaded client zip file. + +# Examples + +## Hello +This is the basic example provided with the MQTTClient library. +It has been modified to use the [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet) library. + +Only includes have changed: +``` +#include <SPI.h> +#include <Ethernet.h> +``` +Replaced by: +``` +#include <LwIP.h> +#include <STM32Ethernet.h> +``` \ No newline at end of file diff --git a/examples/Communication/MQTT/PubSubClient/README.md b/examples/Communication/MQTT/PubSubClient/README.md new file mode 100644 index 0000000..177d2fb --- /dev/null +++ b/examples/Communication/MQTT/PubSubClient/README.md @@ -0,0 +1,24 @@ +# PubSubClient + +This library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT. + +https://github.com/knolleary/pubsubclient + +# Dependencies + +Install the following libraries using the Arduino IDE: **_Sketch -> Include Library -> Manage Libraries_** and search: + + * [PubSubClient](https://github.com/knolleary/pubsubclient): Arduino Client for MQTT. + * [STM32duino ISM43362-M3G-L44](https://github.com/stm32duino/WiFi-ISM43362-M3G-L44): WiFi library for the B-L475E-IOT01A board. + * [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet): Ethernet library for STM32 based board with on-board Ethernet connector. + +# Examples + +## mqtt_B-L475E-IOT01A +This example is based on the mqtt_esp8266 provided with PubSubClient library. +It has been modified to use the [STM32duino ISM43362-M3G-L44](https://github.com/stm32duino/WiFi-ISM43362-M3G-L44) WiFi library with [B-L475E-IOT01A](http://www.st.com/en/evaluation-tools/b-l475e-iot01a.html) board + +## mqtt_STM32Ethernet +This example is based on the mqtt_basic provided with PubSubClient library. +It has been modified to use the [STM32 Ethernet](https://github.com/stm32duino/STM32Ethernet) Ethernet library with a STM32 based board +with on-board Ethernet connector. diff --git a/examples/Communication/MQTT/PubSubClient/mqtt_B-L475E-IOT01A/mqtt_B-L475E-IOT01A.ino b/examples/Communication/MQTT/PubSubClient/mqtt_B-L475E-IOT01A/mqtt_B-L475E-IOT01A.ino new file mode 100644 index 0000000..4d15345 --- /dev/null +++ b/examples/Communication/MQTT/PubSubClient/mqtt_B-L475E-IOT01A/mqtt_B-L475E-IOT01A.ino @@ -0,0 +1,141 @@ +/* + Basic B-L475E-IOT01A MQTT example + + This example is based on the mqtt_esp8266 provided with PubSubClient library. + + This sketch demonstrates the capabilities of the pubsub library in combination + with the B-L475E-IOT01A board. + + It connects to an MQTT server then: + - publishes "hello world" to the topic "outTopic" every two seconds + - subscribes to the topic "inTopic", printing out any messages + it receives. NB - it assumes the received payloads are strings not binary + - If the first character of the topic "inTopic" is an 1, switch ON the LED_BUILTIN Led, + else switch it off + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. +*/ +#include <SPI.h> +#include <WiFiST.h> +#include <PubSubClient.h> + +// Update these with values suitable for your network. +char ssid[] = "......."; +const char* password = "......."; +const char* mqtt_server = "broker.mqtt-dashboard.com"; + +SPIClass SPI_3(PC12, PC11, PC10); +WiFiClass WiFi(&SPI_3, PE0, PE1, PE8, PB13); +WiFiClient STClient; +int status = WL_IDLE_STATUS; // the Wifi radio's status + +PubSubClient client(STClient); +long lastMsg = 0; +char msg[50]; +long value = 0; + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output + Serial.begin(115200); + setup_wifi(); + client.setServer(mqtt_server, 1883); + client.setCallback(callback); +} + +void setup_wifi() { + + delay(10); + + // initialize the WiFi module: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi module not detected"); + // don't continue: + while (true); + } + + // print firmware version: + String fv = WiFi.firmwareVersion(); + Serial.print("Firmware version: "); + Serial.println(fv); + + if (fv != "C3.5.2.3.BETA9") { + Serial.println("Please upgrade the firmware"); + } + + // attempt to connect to Wifi network: + Serial.print("Attempting to connect to network: "); + Serial.println(ssid); + while (status != WL_CONNECTED) { + Serial.print("."); + // Connect to WPA2 network: + status = WiFi.begin(ssid, password); + if (status != WL_CONNECTED) { + // Connect to WPA (TKIP) network: + status = WiFi.begin(ssid, password, ES_WIFI_SEC_WPA); + } + // wait 10 seconds for connection: + delay(10000); + } + + Serial.println(); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (unsigned int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + } + Serial.println(); + + // Switch on the LED if an 1 was received as first character + if ((char)payload[0] == '1') { + digitalWrite(LED_BUILTIN, HIGH); // Turn the LED on + } else { + digitalWrite(LED_BUILTIN, LOW); // Turn the LED off + } +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("B-L475E-IOT01AClient")) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish("outTopic", "hello world"); + // ... and resubscribe + client.subscribe("inTopic"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} +void loop() { + + if (!client.connected()) { + reconnect(); + } + client.loop(); + + long now = millis(); + if (now - lastMsg > 2000) { + lastMsg = now; + ++value; + snprintf (msg, 50, "hello world #%ld", value); + Serial.print("Publish message: "); + Serial.println(msg); + client.publish("outTopic", msg); + } +} diff --git a/examples/Communication/MQTT/PubSubClient/mqtt_STM32Ethernet/mqtt_STM32Ethernet.ino b/examples/Communication/MQTT/PubSubClient/mqtt_STM32Ethernet/mqtt_STM32Ethernet.ino new file mode 100644 index 0000000..7f77884 --- /dev/null +++ b/examples/Communication/MQTT/PubSubClient/mqtt_STM32Ethernet/mqtt_STM32Ethernet.ino @@ -0,0 +1,100 @@ +/* + Basic STM32 Ethernet MQTT example + + This sketch demonstrates the basic capabilities of the library. + It connects to an MQTT server then: + - publishes "hello world" to the topic "outTopic" every two seconds + - subscribes to the topic "inTopic", printing out any messages + it receives. NB - it assumes the received payloads are strings not binary + + It will reconnect to the server if the connection is lost using a blocking + reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to + achieve the same result without blocking the main loop. + +*/ + +#include <LwIP.h> +#include <STM32Ethernet.h> +#include <PubSubClient.h> + +// Update these with values suitable for your network. +byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; +// Set the static IP address to use if the DHCP fails to assign +IPAddress ip(192, 168, 0, 177); +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) +char server[] = "broker.mqtt-dashboard.com"; // name address for mqtt broker (using DNS) +long lastMsg = 0; +char msg[50]; +long value = 0; + +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (unsigned int i=0;i<length;i++) { + Serial.print((char)payload[i]); + } + Serial.println(); +} + +EthernetClient ethClient; +PubSubClient client(ethClient); + +void reconnect() { + + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("arduinoClient72")) { + Serial.println("connected"); + // Once connected, publish an announcement... + client.publish("outTopic","hello world"); + // ... and resubscribe + client.subscribe("inTopic"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +void setup() +{ + Serial.begin(115200); + + client.setServer(server, 1883); + client.setCallback(callback); + + // Start the Ethernet connection: + if (Ethernet.begin(mac) == 0) { + Serial.println("Failed to configure Ethernet using DHCP"); + // Try to congifure using IP address instead of DHCP: + Ethernet.begin(mac, ip); + } + // Allow the hardware to sort itself out + delay(1500); +} + +void loop() +{ + if (!client.connected()) { + reconnect(); + } + client.loop(); + + long now = millis(); + if (now - lastMsg > 2000) { + lastMsg = now; + ++value; + snprintf (msg, 50, "hello world #%ld", value); + Serial.print("Publish message: "); + Serial.println(msg); + client.publish("outTopic", msg); + } +} diff --git a/img/dashboard_adafruit.png b/img/dashboard_adafruit.png new file mode 100644 index 0000000..821f8cc Binary files /dev/null and b/img/dashboard_adafruit.png differ