Skip to content

Mqtt examples #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ boolean swipe_detected() {
int gesture_code;
int status;
boolean ret = false;

sensor_vl53l0x->StartMeasurement();

int top_done = 0;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Loading