add mesh example

This commit is contained in:
Siwat Sirichai 2019-08-21 19:19:51 +07:00
parent 62cab61fbb
commit 331825732e
5 changed files with 315 additions and 0 deletions

View File

@ -0,0 +1,98 @@
#include "credentials.h"
#include <ESP8266MQTTMesh.h>
#include <FS.h>
#ifndef LED_PIN
#define LED_PIN LED_BUILTIN
#endif
#define FIRMWARE_ID 0x1337
#define FIRMWARE_VER "0.1"
wifi_conn networks[] = NETWORK_LIST;
const char* mesh_password = MESH_PASSWORD;
const char* mqtt_server = MQTT_SERVER;
const int mqtt_port = MQTT_PORT;
#if ASYNC_TCP_SSL_ENABLED
const uint8_t *mqtt_fingerprint = MQTT_FINGERPRINT;
bool mqtt_secure = MQTT_SECURE;
#if MESH_SECURE
#include "ssl_cert.h"
#endif
#endif
#ifdef ESP32
String ID = String((unsigned long)ESP.getEfuseMac());
#else
String ID = String(ESP.getChipId());
#endif
unsigned long previousMillis = 0;
const long interval = 5000;
int cnt = 0;
// Note: All of the '.set' options below are optional. The default values can be
// found in ESP8266MQTTMeshBuilder.h
ESP8266MQTTMesh mesh = ESP8266MQTTMesh::Builder(networks, mqtt_server, mqtt_port)
.setVersion(FIRMWARE_VER, FIRMWARE_ID)
.setMeshPassword(mesh_password)
#if ASYNC_TCP_SSL_ENABLED
.setMqttSSL(mqtt_secure, mqtt_fingerprint)
#if MESH_SECURE
.setMeshSSL(ssl_cert, ssl_cert_len, ssl_key, ssl_key_len, ssl_fingerprint)
#endif //MESH_SECURE
#endif //ASYNC_TCP_SSL_ENABLED
.build();
void callback(const char *topic, const char *msg);
void setup() {
Serial.begin(115200);
delay(1000); //This is only here to make it easier to catch the startup messages. It isn't required
mesh.setCallback(callback);
mesh.begin();
pinMode(LED_PIN, OUTPUT);
}
void loop() {
if (! mesh.connected())
return;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
String cntStr = String(cnt);
String msg = "hello from " + ID + " cnt: " + cntStr;
mesh.publish(ID.c_str(), msg.c_str());
previousMillis = currentMillis;
cnt++;
}
}
void callback(const char *topic, const char *msg) {
if (0 == strcmp(topic, (const char*) ID.c_str())) {
if(String(msg) == "0") {
digitalWrite(LED_PIN, HIGH);
}else{
digitalWrite(LED_PIN, LOW);
}
}
}

View File

@ -0,0 +1,16 @@
#define NETWORK_PASSWORD "network password"
#define NETWORK_LIST { \
WIFI_CONN("ssid 1", NETWORK_PASSWORD, NULL, 0), \
WIFI_CONN("ssid 2", NETWORK_PASSWORD, NULL, 0), \
NULL, \
}
#define MESH_PASSWORD "esp8266_sensor_mesh"
#define MQTT_SERVER "MQTT Server IP Address"
#define MQTT_PORT 1883
/* Only used if SSL is enabled */
#define MESH_SECURE true
#define MQTT_SECURE false
#define MQTT_FINGERPRINT NULL
//const uint8_t MQTT_FINGERPRINT[] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33};

View File

@ -0,0 +1,87 @@
const uint8_t ssl_key[] =
"\x30\x82\x02\x5D\x02\x01\x00\x02\x81\x81\x00\xA6\xE5\xE8\x1A\xFB"
"\xF8\x1A\xB7\x34\xCF\x98\x90\x10\x7A\x80\x1D\x1C\x3F\xA7\x9A\xC0"
"\x97\x5D\xC2\xCF\xD0\xD6\xCE\x65\x11\x57\xB5\x4A\x21\x81\x5E\x66"
"\xBE\x05\x70\xDC\xF3\x5C\x8D\x40\x12\x03\x22\xAC\x75\x26\x92\x04"
"\x1F\xC1\x9D\x6F\x68\x4E\x70\xE9\x43\xF1\xD0\xF2\x2F\x68\xDE\xA5"
"\x9A\xBA\xF6\xCA\x53\xA4\x89\x49\xD8\x9C\x65\x7C\x02\x7B\xBF\xFC"
"\x66\x56\xA6\x55\x45\x71\x8B\x31\x3F\xEC\xA0\x65\x0B\x76\x6F\xE5"
"\x08\x67\xDF\x8A\x9E\x8B\x46\x29\x63\x71\x84\xED\x57\xC2\xBC\x37"
"\x79\xA2\x3F\xFA\x39\xD3\xEA\xBF\x7A\xCB\x83\x02\x03\x01\x00\x01"
"\x02\x81\x80\x21\xCB\x2F\xA2\x37\x1E\xD7\x99\xFD\x11\x83\xDC\xB7"
"\xD9\x76\x13\x6E\xE2\xDC\xB7\x13\x04\x13\x32\x1D\x0E\x36\x50\x78"
"\x5A\x78\x9D\xF6\xB2\xAE\x15\x45\x4C\x78\xA1\x8F\xBB\x9F\x23\xE2"
"\xB3\x42\xFB\x44\x5C\x3C\x41\x18\xA0\xAD\x7D\x89\x4F\x5F\x82\xB1"
"\x58\xD6\x9F\x9A\x8F\x3A\x2C\x5C\xE4\xED\xEC\x5E\x95\xDB\x59\x66"
"\xEE\xF3\xAB\x95\x3E\x3D\xF6\xC0\xFD\x13\x38\x78\x1D\xC8\xE2\x08"
"\xAC\x3B\xA5\x1F\xC8\x64\xD2\xD2\xC4\x3E\xD1\xC0\x4B\xC2\xC8\x56"
"\x94\xC1\xE5\xA1\x5C\x57\x7E\x56\x5E\xC1\x33\x80\x7F\x07\x33\xD8"
"\x0B\xD5\xC1\x02\x41\x00\xD3\x95\x14\xE1\xC1\xAB\xEF\x19\x66\x6C"
"\xFE\xDB\xB3\x75\xD0\xEA\x1C\x2C\x17\x2A\x3F\x6D\x79\xCA\xA9\x82"
"\x7F\x19\x98\xC0\x1F\x96\x0A\x6B\x86\x01\xAE\x4E\xEA\x99\x48\x65"
"\x61\xC8\xB1\xC1\xDC\x20\xFF\xEE\x3A\xB3\x10\xCE\x4B\xEF\x6C\xD0"
"\x60\x44\x68\x33\x45\x13\x02\x41\x00\xC9\xEF\x63\xDA\x7D\xF8\x8A"
"\xC9\xD1\x96\xBE\x13\xB6\x3F\xB6\x04\xF1\xD7\x06\x89\xCC\x2D\x5E"
"\xDA\x9A\x78\xA7\xAC\x40\x37\x2E\x5C\x1F\x7C\x64\x6D\x4B\xB1\x78"
"\xD6\x62\x5F\x60\x95\xC9\x7B\xD7\x03\x54\x46\x98\xD8\x4E\xA8\xB8"
"\xC6\x32\xD8\xC5\x32\xAE\xCA\xDD\xD1\x02\x40\x57\x37\x61\xF7\x39"
"\x85\x6D\x37\x14\x30\xA3\xD1\xDE\xA5\x17\x2C\x19\xD6\xD6\xE9\xB4"
"\x61\xA5\x4D\xB4\x18\x35\xDA\x50\x4C\x09\xF9\x28\x6C\x70\x3D\xEB"
"\x23\x5E\xB3\x36\xD3\x8B\xBE\x55\xFF\xEA\x84\xB3\xDA\xF8\xD9\x6D"
"\x79\x0C\x76\x32\x6D\xA6\xF1\x2B\xDE\xCE\x7F\x02\x41\x00\x89\xD6"
"\x0B\xB4\x92\x13\xDA\xB8\x53\x85\xAF\x8C\xC8\xF3\xC8\x0C\xAB\xFE"
"\xF8\x09\x8B\x02\xD5\x22\x26\x1A\x81\x69\x04\x14\x26\x62\xDF\x63"
"\x0B\x31\xC7\x5F\x06\x7A\x5F\x7F\x76\xF0\x07\x2D\xAE\xE0\x28\xE0"
"\x5F\x68\x16\x98\xF8\x36\xE1\x72\x31\x78\x9C\xF3\x00\x61\x02\x41"
"\x00\xB5\x07\xCF\xC3\x62\x65\xD3\xD4\x97\x2B\xCA\x6B\x66\x75\x5C"
"\xE1\x38\xFD\xAF\xC2\xA9\xCE\x21\x3F\x09\xDC\xEA\xEE\xD2\x09\xF8"
"\xCF\xF6\x8D\xEC\x95\xF2\x12\xAC\xE7\x11\x30\xE3\xC6\xDB\x26\x17"
"\x64\xB1\xC2\x78\xF0\x47\x75\x11\xA9\x69\x52\x23\xC3\x2B\x37\x4B"
"\xBB"
;
const uint32_t ssl_key_len = 609;
const uint8_t ssl_cert[] =
"\x30\x82\x02\x47\x30\x82\x01\x2F\x02\x09\x00\xD2\x15\x44\x99\x56"
"\xC9\xF0\xD1\x30\x0D\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05"
"\x05\x00\x30\x1C\x31\x1A\x30\x18\x06\x03\x55\x04\x0A\x0C\x11\x45"
"\x73\x70\x72\x65\x73\x73\x69\x66\x20\x53\x79\x73\x74\x65\x6D\x73"
"\x30\x1E\x17\x0D\x31\x37\x30\x37\x31\x31\x30\x33\x30\x33\x31\x32"
"\x5A\x17\x0D\x33\x31\x30\x33\x32\x30\x30\x33\x30\x33\x31\x32\x5A"
"\x30\x33\x31\x19\x30\x17\x06\x03\x55\x04\x0A\x0C\x10\x61\x78\x54"
"\x4C\x53\x20\x6F\x6E\x20\x45\x53\x50\x38\x32\x36\x36\x31\x16\x30"
"\x14\x06\x03\x55\x04\x03\x0C\x0D\x65\x73\x70\x38\x32\x36\x36\x2E"
"\x6C\x6F\x63\x61\x6C\x30\x81\x9F\x30\x0D\x06\x09\x2A\x86\x48\x86"
"\xF7\x0D\x01\x01\x01\x05\x00\x03\x81\x8D\x00\x30\x81\x89\x02\x81"
"\x81\x00\xA6\xE5\xE8\x1A\xFB\xF8\x1A\xB7\x34\xCF\x98\x90\x10\x7A"
"\x80\x1D\x1C\x3F\xA7\x9A\xC0\x97\x5D\xC2\xCF\xD0\xD6\xCE\x65\x11"
"\x57\xB5\x4A\x21\x81\x5E\x66\xBE\x05\x70\xDC\xF3\x5C\x8D\x40\x12"
"\x03\x22\xAC\x75\x26\x92\x04\x1F\xC1\x9D\x6F\x68\x4E\x70\xE9\x43"
"\xF1\xD0\xF2\x2F\x68\xDE\xA5\x9A\xBA\xF6\xCA\x53\xA4\x89\x49\xD8"
"\x9C\x65\x7C\x02\x7B\xBF\xFC\x66\x56\xA6\x55\x45\x71\x8B\x31\x3F"
"\xEC\xA0\x65\x0B\x76\x6F\xE5\x08\x67\xDF\x8A\x9E\x8B\x46\x29\x63"
"\x71\x84\xED\x57\xC2\xBC\x37\x79\xA2\x3F\xFA\x39\xD3\xEA\xBF\x7A"
"\xCB\x83\x02\x03\x01\x00\x01\x30\x0D\x06\x09\x2A\x86\x48\x86\xF7"
"\x0D\x01\x01\x05\x05\x00\x03\x82\x01\x01\x00\x5F\x4F\xA6\x53\x1D"
"\xC2\x7C\x2C\x0D\xDE\x54\x05\xAD\x61\xDE\x39\xE2\x8B\xA9\x47\x0D"
"\x57\xC3\xC0\x15\x18\x0C\xBF\x71\x32\x5A\x8E\x68\x64\x7A\xA2\xE3"
"\x27\x76\xC4\x22\x16\x31\x7A\x14\xD7\x2B\x16\x1E\x94\x2F\x69\x51"
"\xFF\xE0\xBC\x84\xD7\x6B\x51\xC1\xA7\x45\x1E\x6E\xB4\x86\x4F\xDD"
"\x07\x15\x72\x4B\x12\x79\xEF\x31\xDE\xE4\x9E\xC6\x6F\xDA\x36\xC8"
"\x59\x1B\x6A\x3E\xF0\xDE\xD7\x84\xCE\xE7\xAE\x59\xBE\xB6\x2D\xB0"
"\xD3\x84\x8F\xD6\xA2\x6F\x2F\x9E\x22\x5B\x1D\x61\xCF\x96\xB3\x62"
"\x16\x63\xC7\x8A\xA8\xC8\x7C\xE0\xB5\x98\xAE\x12\xB1\xAE\xE5\xFF"
"\x1F\x1A\x0D\x15\x7C\x26\xF0\x19\xBF\x12\x70\xB3\x29\xF1\xA0\x14"
"\x64\x52\x08\x56\x88\xBF\x54\xCF\x5C\x44\x50\x25\xB9\xF4\xC3\x29"
"\x3B\xB2\x98\xC8\xE2\xB9\x76\x78\x0E\xDB\x8E\x52\x8B\x4D\x7B\x95"
"\xDB\xCB\xE8\x68\x67\x3B\x6F\x99\x0E\xDC\x4D\x81\x93\xB2\xA3\x99"
"\xD2\xBC\x1E\xB4\x4F\x9D\xC0\x11\xEF\x10\xD4\xA8\x32\xEC\xE3\xFE"
"\xA8\x52\x57\x4F\x04\x3D\x17\x9B\xF1\x3C\x19\xAB\x17\x03\x66\x69"
"\x90\x8A\x7B\x09\xDF\xF9\x78\xE6\xC6\xED\x04\x19\x2B\x88\xD4\xE1"
"\x80\x88\x1A\x98\x6F\x9C\x6D\x37\x18\x55\x36"
;
const uint32_t ssl_cert_len = 587;
const uint8_t ssl_fingerprint[] =
"\xC8\x03\xFF\xF8\xD0\x8D\xB8\xE7\x11\xF0\x87\x2C\x57\x4E\xDD\xD3"
"\x48\x2C\x35\x64"
;

View File

@ -0,0 +1,98 @@
#include "credentials.h"
#include <ESP8266MQTTMesh.h>
#include <FS.h>
#ifndef LED_PIN
#define LED_PIN LED_BUILTIN
#endif
#define FIRMWARE_ID 0x1337
#define FIRMWARE_VER "0.1"
wifi_conn networks[] = NETWORK_LIST;
const char* mesh_password = MESH_PASSWORD;
const char* mqtt_server = MQTT_SERVER;
const int mqtt_port = MQTT_PORT;
#if ASYNC_TCP_SSL_ENABLED
const uint8_t *mqtt_fingerprint = MQTT_FINGERPRINT;
bool mqtt_secure = MQTT_SECURE;
#if MESH_SECURE
#include "ssl_cert.h"
#endif
#endif
#ifdef ESP32
String ID = String((unsigned long)ESP.getEfuseMac());
#else
String ID = String(ESP.getChipId());
#endif
unsigned long previousMillis = 0;
const long interval = 5000;
int cnt = 0;
// Note: All of the '.set' options below are optional. The default values can be
// found in ESP8266MQTTMeshBuilder.h
ESP8266MQTTMesh mesh = ESP8266MQTTMesh::Builder(networks, mqtt_server, mqtt_port)
.setVersion(FIRMWARE_VER, FIRMWARE_ID)
.setMeshPassword(mesh_password)
#if ASYNC_TCP_SSL_ENABLED
.setMqttSSL(mqtt_secure, mqtt_fingerprint)
#if MESH_SECURE
.setMeshSSL(ssl_cert, ssl_cert_len, ssl_key, ssl_key_len, ssl_fingerprint)
#endif //MESH_SECURE
#endif //ASYNC_TCP_SSL_ENABLED
.build();
void callback(const char *topic, const char *msg);
void setup() {
Serial.begin(115200);
delay(1000); //This is only here to make it easier to catch the startup messages. It isn't required
mesh.setCallback(callback);
mesh.begin();
pinMode(LED_PIN, OUTPUT);
}
void loop() {
if (! mesh.connected())
return;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
String cntStr = String(cnt);
String msg = "hello from " + ID + " cnt: " + cntStr;
mesh.publish(ID.c_str(), msg.c_str());
previousMillis = currentMillis;
cnt++;
}
}
void callback(const char *topic, const char *msg) {
if (0 == strcmp(topic, (const char*) ID.c_str())) {
if(String(msg) == "0") {
digitalWrite(LED_PIN, HIGH);
}else{
digitalWrite(LED_PIN, LOW);
}
}
}

View File

@ -0,0 +1,16 @@
#define NETWORK_PASSWORD "network password"
#define NETWORK_LIST { \
WIFI_CONN("ssid 1", NETWORK_PASSWORD, NULL, 0), \
WIFI_CONN("ssid 2", NETWORK_PASSWORD, NULL, 0), \
NULL, \
}
#define MESH_PASSWORD "esp8266_sensor_mesh"
#define MQTT_SERVER "MQTT Server IP Address"
#define MQTT_PORT 1883
/* Only used if SSL is enabled */
#define MESH_SECURE true
#define MQTT_SECURE false
#define MQTT_FINGERPRINT NULL
//const uint8_t MQTT_FINGERPRINT[] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33};