Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Created February 5, 2020 11:54
Show Gist options
  • Save ma2shita/1784800f5bf4bf376b6e86fed7b7beae to your computer and use it in GitHub Desktop.
Save ma2shita/1784800f5bf4bf376b6e86fed7b7beae to your computer and use it in GitHub Desktop.
MQTT Pub/Sub with SORACOM Beam for M5Stack
/* MQTT Pub/Sub with SORACOM Beam for M5Stack */
/*
* Copyright (c) 2020 Kohei "Max" MATSUSHITA
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/
#include <string.h>
#include <stdio.h>
#include <M5Stack.h>
#define CONSOLE Serial
#define MODEM Serial2
#define TINY_GSM_MODEM_UBLOX
#include <TinyGsmClient.h>
TinyGsm modem(MODEM);
TinyGsmClient ctx(modem);
#include <PubSubClient.h>
PubSubClient MqttClient(ctx);
const char *THING_NAME = "m5stack";
const char *PUB_TOPIC = "/devices/m5stack/events";
const char *SUB_TOPIC = "/devices/m5stack/commands/#";
#define __VERSION__ "1.0.0"
#define LOOP_INTERVAL (6000)
void callback(char* topic, byte* payload, unsigned int length) {
String buf_t = String(topic);
payload[length] = '\0'; /* https://hawksnowlog.blogspot.com/2017/06/convert-byte-array-to-string.html */
String buf_p = String((char*) payload);
/* Or;
char buf_p[length];
for (int i = 0; i < length; i++) buf_p[i] = (char) payload[i];
*/
/* Iplement HERE */
CONSOLE.print("Incoming topic: ");
CONSOLE.println(buf_t);
CONSOLE.print("Payload>");
CONSOLE.println(buf_p);
M5.Lcd.clear(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0,0);
M5.Lcd.println(buf_t);
M5.Lcd.println(buf_p);
}
void cellular_disconnect() {
MqttClient.disconnect();
modem.restart();
}
void cellular_connect() {
M5.Lcd.clear(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0,0);
CONSOLE.print(F("modem.restart()"));
M5.Lcd.print(F("modem.restart()"));
modem.restart();
CONSOLE.println(F("Ok"));
M5.Lcd.println(F("Ok"));
CONSOLE.print(F("waitForNetwork()"));
M5.Lcd.print(F("waitForNetwork()"));
while (!modem.waitForNetwork()) CONSOLE.print(".");
CONSOLE.println(F("Ok"));
M5.Lcd.println(F("Ok"));
CONSOLE.print(F("gprsConnect(soracom.io)"));
M5.Lcd.print(F("gprsConnect(soracom.io)"));
modem.gprsConnect("soracom.io", "sora", "sora");
CONSOLE.println(F("Ok"));
M5.Lcd.println(F("Ok"));
CONSOLE.print(F("isNetworkConnected()"));
M5.Lcd.print(F("isNetworkConnected()"));
while (!modem.isNetworkConnected()) CONSOLE.print(".");
CONSOLE.println(F("Ok"));
M5.Lcd.println(F("Ok"));
CONSOLE.print(F("My IP addr: "));
M5.Lcd.print(F("My IP addr: "));
IPAddress ipaddr = modem.localIP();
CONSOLE.println(ipaddr);
M5.Lcd.println(ipaddr);
}
void mqtt_disconnect() {
CONSOLE.println(MqttClient.state());
MqttClient.disconnect();
}
void mqtt_connect() {
CONSOLE.print("ThingName(mqtt_id): ");
CONSOLE.println(THING_NAME);
MqttClient.setServer("beam.soracom.io", 1883);
MqttClient.setCallback(callback);
if (!MqttClient.connect(THING_NAME)) {
CONSOLE.println(MqttClient.state());
}
MqttClient.subscribe(SUB_TOPIC);
}
void connection_check_and_reconnect() {
if (!MqttClient.connected()) {
mqtt_disconnect();
cellular_disconnect();
cellular_connect();
mqtt_connect();
}
}
void setup() {
delay(500);
Serial.begin(115200);
M5.begin();
M5.Lcd.clear(BLACK);
M5.Lcd.setTextColor(WHITE);
CONSOLE.println();
CONSOLE.println(__VERSION__);
M5.Lcd.println(__VERSION__);
MODEM.begin(115200, SERIAL_8N1, 16, 17); // 3G MODULE
cellular_connect();
mqtt_connect();
}
void loop() {
M5.update();
connection_check_and_reconnect();
/* Implement for Loop */
CONSOLE.println("loop");
char payload[512];
sprintf(payload, "{\"uptime\":%lu}", millis()/1000); /* for example */
MqttClient.publish(PUB_TOPIC, payload);
unsigned long next = millis();
while (millis() < next + LOOP_INTERVAL) {
MqttClient.loop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment