Skip to content

Instantly share code, notes, and snippets.

@shantanoo-desai
Last active June 29, 2020 13:21
Show Gist options
  • Save shantanoo-desai/8aff9e0b18b9563ebc3ff10dd563b654 to your computer and use it in GitHub Desktop.
Save shantanoo-desai/8aff9e0b18b9563ebc3ff10dd563b654 to your computer and use it in GitHub Desktop.
Arduino Sketch for Sending Secure Data (TLSv1.2 Certificate) and Username/Password over MQTT Broker with ESP32 and LSM9DS0
/**
* Make sure to add `\n` at the end of each line of the certificate and the `\` to keep the certificate as one single string.
*/
const char* ca_cert = \
"-----BEGIN CERTIFICATE-----\n"\
"CERTIFICATE CONTENT HERE \n" \
"-----END CERTIFICATE-----\n";
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS0.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
// CERTIFICATE Header File
#include "cert.h"
//WifiClient and MQTT Client
WiFiClientSecure espClient;
PubSubClient client(espClient);
// USE I2C bus for ESP32 with ID 1000
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();
/**
* SETTINGS HERE
*/
String SENSOR_ID = WiFi.macAddress();
/*WLAN Configuration Settings*/
const char* ssid = "<WLAN_SSID>";
const char* password = "<WLAN_PASSPHRASE>";
/*MQTT Broker Configuration Settings*/
const char* mqtt_broker_address = "<SECURE_MQTT_BROKER_ADDRESS>";
const uint16_t mqtt_port = 8883;
const char* mqtt_username = "<MQTT_USERNAME>";
const char* mqtt_password = "<MQTT_PASSWORD>";
const char* client_id = "ESP32_TESTSec1";
/*MQTT TOPICS*/
String acc_topic = "acme/factory/US/boston/" + SENSOR_ID + "/acc";
String mag_topic = "acme/factory/US/boston/" + SENSOR_ID + "/mag";
String gyro_topic = "acme/factory/US/boston/" + SENSOR_ID + "/gyro";
String temp_topic = "acme/factory/US/boston/" + SENSOR_ID + "/temp";
/*Reconnect to MQTT Broker*/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("MQTT:reconnect: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(client_id, mqtt_username, mqtt_password)) {
Serial.println("MQTT:reconnect: Connected");
} else {
Serial.print("MQTT:reconnect: Failed, rc=");
Serial.print(client.state());
Serial.println("MQTT:reconnect: Trying Again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
void setupSensor() {
// 1.) Set the accelerometer range
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
// 2.) Set the magnetometer sensitivity
lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
// 3.) Setup the gyroscope
lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("LSM MQTT Demo");
if (!lsm.begin()) {
Serial.println("Sensor Not Initialized");
while(1);
}
Serial.println("Found Sensor, Setting up");
setupSensor();
delay(1000);
Serial.println("Connecting to WLAN Access-Point");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("main:setup: WiFi Connected");
Serial.println("main:setup: IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("main:setup: MAC Address: ");
Serial.println(WiFi.macAddress());
Serial.println("main:setup: Setting up MQTT Configuration");
// Set CA Certificate here
espClient.setCACert(ca_cert);
client.setServer(mqtt_broker_address, mqtt_port);
}
String lineprotocol_temp(int temp_value) {
String _result = "environment temp=" + String(temp_value);
return _result;
}
String lineprotocol_acc(float acc_x, float acc_y, float acc_z) {
String _result = "acceleration accX=" + String(acc_x) + ",accY=" + String(acc_y) + ",accZ=" + String(acc_z);
return _result;
}
String lineprotocol_mag(float mag_x, float mag_y, float mag_z) {
String _result = "magnetometer magX=" + String(mag_x) + ",magY=" + String(mag_y) + ",magZ=" + String(mag_z);
return _result;
}
String lineprotocol_gyro(float gyro_x, float gyro_y, float gyro_z) {
String _result = "gyroscope gyroX=" + String(gyro_x) + ",gyroY=" + String(gyro_y) + ",gyroZ=" + String(gyro_z);
return _result;
}
void loop() {
if (!client.connected()){
reconnect();
}
client.loop();
/* Get a new sensor event */
sensors_event_t accel, mag, gyro, temp;
lsm.getEvent(&accel, &mag, &gyro, &temp);
client.publish(acc_topic.c_str() , lineprotocol_acc(accel.acceleration.x, accel.acceleration.y, accel.acceleration.z).c_str());
client.publish(mag_topic.c_str(), lineprotocol_mag(mag.magnetic.x, mag.magnetic.y, mag.magnetic.z).c_str());
client.publish(gyro_topic.c_str(), lineprotocol_gyro(gyro.gyro.x, gyro.gyro.y, gyro.gyro.z).c_str());
client.publish(temp_topic.c_str(), lineprotocol_temp(temp.temperature).c_str());
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment