mqtt client (experimental)
This commit is contained in:
		
							parent
							
								
									9c0b504841
								
							
						
					
					
						commit
						05e5d31a53
					
				@ -63,7 +63,8 @@ Depending on board hardware following features are supported:
 | 
				
			|||||||
- IF482 (serial) and DCF77 (gpio) time telegram generator
 | 
					- IF482 (serial) and DCF77 (gpio) time telegram generator
 | 
				
			||||||
- Switch external power / battery
 | 
					- Switch external power / battery
 | 
				
			||||||
- LED Matrix display (similar to [this 64x16 model](https://www.instructables.com/id/64x16-RED-LED-Marquee/), can be ordered on [Aliexpress](https://www.aliexpress.com/item/P3-75-dot-matrix-led-module-3-75mm-high-clear-top1-for-text-display-304-60mm/32616683948.html))
 | 
					- LED Matrix display (similar to [this 64x16 model](https://www.instructables.com/id/64x16-RED-LED-Marquee/), can be ordered on [Aliexpress](https://www.aliexpress.com/item/P3-75-dot-matrix-led-module-3-75mm-high-clear-top1-for-text-display-304-60mm/32616683948.html))
 | 
				
			||||||
- SD-card (see section SD-card here)
 | 
					- SD-card (see section SD-card here) for logging pax data
 | 
				
			||||||
 | 
					- Ethernet interface for MQTT communication via TCP/IP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Target platform must be selected in [platformio.ini](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/platformio.ini).<br>
 | 
					Target platform must be selected in [platformio.ini](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/platformio.ini).<br>
 | 
				
			||||||
Hardware dependent settings (pinout etc.) are stored in board files in /hal directory. If you want to use a ESP32 board which is not yet supported, use hal file generic.h and tailor pin mappings to your needs. Pull requests for new boards welcome.<br>
 | 
					Hardware dependent settings (pinout etc.) are stored in board files in /hal directory. If you want to use a ESP32 board which is not yet supported, use hal file generic.h and tailor pin mappings to your needs. Pull requests for new boards welcome.<br>
 | 
				
			||||||
@ -211,7 +212,7 @@ Data can be stored on an SD-card if one is availabe. Simply choose the file in s
 | 
				
			|||||||
	OR
 | 
						OR
 | 
				
			||||||
	#define HAS_SDCARD 2     // SD-card-reader/writer, using SDMMC interface
 | 
						#define HAS_SDCARD 2     // SD-card-reader/writer, using SDMMC interface
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Pins for SD-card
 | 
					    // Pins for SPI interface
 | 
				
			||||||
    #define SDCARD_CS   (13) // fill in the correct numbers for your board
 | 
					    #define SDCARD_CS   (13) // fill in the correct numbers for your board
 | 
				
			||||||
    #define SDCARD_MOSI (15)
 | 
					    #define SDCARD_MOSI (15)
 | 
				
			||||||
    #define SDCARD_MISO (2)
 | 
					    #define SDCARD_MISO (2)
 | 
				
			||||||
 | 
				
			|||||||
@ -5,6 +5,7 @@
 | 
				
			|||||||
#include "senddata.h"
 | 
					#include "senddata.h"
 | 
				
			||||||
#include "rcommand.h"
 | 
					#include "rcommand.h"
 | 
				
			||||||
#include "spislave.h"
 | 
					#include "spislave.h"
 | 
				
			||||||
 | 
					#include "mqttclient.h"
 | 
				
			||||||
#include "bmesensor.h"
 | 
					#include "bmesensor.h"
 | 
				
			||||||
#include "display.h"
 | 
					#include "display.h"
 | 
				
			||||||
#include "sds011read.h"
 | 
					#include "sds011read.h"
 | 
				
			||||||
 | 
				
			|||||||
@ -6,13 +6,13 @@
 | 
				
			|||||||
#include <ETH.h>
 | 
					#include <ETH.h>
 | 
				
			||||||
#include <PubSubClient.h>
 | 
					#include <PubSubClient.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define MQTT_CLIENT "paxcounter"
 | 
					#define MQTT_INTOPIC "paxcounter_in/"
 | 
				
			||||||
#define MQTT_INTOPIC "pax_IN"
 | 
					#define MQTT_OUTTOPIC "paxcounter_out/"
 | 
				
			||||||
#define MQTT_OUTTOPIC "pax_OUT"
 | 
					 | 
				
			||||||
#define MQTT_PORT 1883
 | 
					#define MQTT_PORT 1883
 | 
				
			||||||
#define MQTT_SERVER "broker.hivemq.com"
 | 
					#define MQTT_SERVER "broker.hivemq.com"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern TaskHandle_t mqttTask;
 | 
					extern TaskHandle_t mqttTask;
 | 
				
			||||||
 | 
					extern PubSubClient mqttClient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void mqtt_enqueuedata(MessageBuffer_t *message);
 | 
					void mqtt_enqueuedata(MessageBuffer_t *message);
 | 
				
			||||||
void mqtt_queuereset(void);
 | 
					void mqtt_queuereset(void);
 | 
				
			||||||
 | 
				
			|||||||
@ -38,5 +38,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
bool sdcard_init(void);
 | 
					bool sdcard_init(void);
 | 
				
			||||||
void sdcardWriteData(uint16_t, uint16_t);
 | 
					void sdcardWriteData(uint16_t, uint16_t);
 | 
				
			||||||
 | 
					static void createFile(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
				
			|||||||
@ -52,6 +52,11 @@ void doHousekeeping() {
 | 
				
			|||||||
  ESP_LOGD(TAG, "spiloop %d bytes left | Taskstate = %d",
 | 
					  ESP_LOGD(TAG, "spiloop %d bytes left | Taskstate = %d",
 | 
				
			||||||
           uxTaskGetStackHighWaterMark(spiTask), eTaskGetState(spiTask));
 | 
					           uxTaskGetStackHighWaterMark(spiTask), eTaskGetState(spiTask));
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					#ifdef HAS_MQTT
 | 
				
			||||||
 | 
					  ESP_LOGD(TAG, "MQTTloop %d bytes left | Taskstate = %d",
 | 
				
			||||||
 | 
					           uxTaskGetStackHighWaterMark(mqttTask), eTaskGetState(mqttTask));
 | 
				
			||||||
 | 
					  mqttClient.loop();
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if (defined HAS_DCF77 || defined HAS_IF482)
 | 
					#if (defined HAS_DCF77 || defined HAS_IF482)
 | 
				
			||||||
  ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d",
 | 
					  ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d",
 | 
				
			||||||
 | 
				
			|||||||
@ -8,13 +8,11 @@
 | 
				
			|||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// enable only if you want to store a local paxcount table on the device
 | 
					// enable only if you want to store a local paxcount table on the device
 | 
				
			||||||
//#define HAS_SDCARD  2      // this board has an SD-card-reader/writer
 | 
					//#define HAS_SDCARD 2 // this board has a SDMMC card-reader/writer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// enable only if you want to send paxcount via ethernet port to mqtt server
 | 
					// enable only if you want to send paxcount via ethernet port to mqtt server
 | 
				
			||||||
#define HAS_MQTT 1  // use MQTT on ethernet interface
 | 
					#define HAS_MQTT 1  // use MQTT on ethernet interface
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//#define BAT_MEASURE_ADC ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC1_CHANNEL_7
 | 
					//#define BAT_MEASURE_ADC ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC1_CHANNEL_7
 | 
				
			||||||
//#define BAT_VOLTAGE_DIVIDER 2 // voltage divider 470k/470k on board
 | 
					//#define BAT_VOLTAGE_DIVIDER 2 // voltage divider 470k/470k on board
 | 
				
			||||||
#define BAT_MEASURE_ADC ADC1_GPIO39_CHANNEL // external power probe GPIO pin
 | 
					#define BAT_MEASURE_ADC ADC1_GPIO39_CHANNEL // external power probe GPIO pin
 | 
				
			||||||
 | 
				
			|||||||
@ -14,7 +14,7 @@ void NetworkEvent(WiFiEvent_t event) {
 | 
				
			|||||||
  switch (event) {
 | 
					  switch (event) {
 | 
				
			||||||
  case SYSTEM_EVENT_ETH_START:
 | 
					  case SYSTEM_EVENT_ETH_START:
 | 
				
			||||||
    ESP_LOGI(TAG, "Ethernet link layer started");
 | 
					    ESP_LOGI(TAG, "Ethernet link layer started");
 | 
				
			||||||
    ETH.setHostname(MQTT_CLIENT);
 | 
					    ETH.setHostname(ETH.macAddress().c_str());
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case SYSTEM_EVENT_ETH_CONNECTED:
 | 
					  case SYSTEM_EVENT_ETH_CONNECTED:
 | 
				
			||||||
    ESP_LOGI(TAG, "Network link connected");
 | 
					    ESP_LOGI(TAG, "Network link connected");
 | 
				
			||||||
@ -40,7 +40,7 @@ void NetworkEvent(WiFiEvent_t event) {
 | 
				
			|||||||
int mqtt_connect(const char *my_host, const uint16_t my_port) {
 | 
					int mqtt_connect(const char *my_host, const uint16_t my_port) {
 | 
				
			||||||
  IPAddress mqtt_server_ip;
 | 
					  IPAddress mqtt_server_ip;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static String clientId = "paxcounter-" + String(random(0xffff), HEX);
 | 
					  static String clientId = "paxcounter-" + ETH.macAddress();
 | 
				
			||||||
  ESP_LOGI(TAG, "MQTT name is %s", clientId.c_str());
 | 
					  ESP_LOGI(TAG, "MQTT name is %s", clientId.c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // resolve server
 | 
					  // resolve server
 | 
				
			||||||
@ -53,16 +53,15 @@ int mqtt_connect(const char *my_host, const uint16_t my_port) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // attempt to connect to MQTT server
 | 
					  // attempt to connect to MQTT server
 | 
				
			||||||
  if (EthClient.connect(mqtt_server_ip, my_port)) {
 | 
					  if (EthClient.connect(mqtt_server_ip, my_port, HOMECYCLE * 2 * 1000)) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    mqttClient.setServer(mqtt_server_ip, my_port);
 | 
					    mqttClient.setServer(mqtt_server_ip, my_port);
 | 
				
			||||||
 | 
					    mqttClient.setKeepAlive(HOMECYCLE * 2);
 | 
				
			||||||
    mqttClient.setCallback(mqtt_callback);
 | 
					    mqttClient.setCallback(mqtt_callback);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (mqttClient.connect(clientId.c_str())) {
 | 
					    if (mqttClient.connect(clientId.c_str())) {
 | 
				
			||||||
      ESP_LOGI(TAG, "MQTT server connected, subscribing...");
 | 
					      ESP_LOGI(TAG, "MQTT server connected, subscribing...");
 | 
				
			||||||
      mqttClient.publish(MQTT_OUTTOPIC, "hello world");
 | 
					      mqttClient.publish(MQTT_OUTTOPIC, clientId.c_str());
 | 
				
			||||||
      mqttClient.subscribe(MQTT_INTOPIC);
 | 
					      mqttClient.subscribe(MQTT_INTOPIC);
 | 
				
			||||||
      mqttClient.loop();
 | 
					 | 
				
			||||||
      ESP_LOGI(TAG, "MQTT topic subscribed");
 | 
					      ESP_LOGI(TAG, "MQTT topic subscribed");
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      ESP_LOGW(TAG, "MQTT server not responding, retrying later");
 | 
					      ESP_LOGW(TAG, "MQTT server not responding, retrying later");
 | 
				
			||||||
@ -77,7 +76,6 @@ int mqtt_connect(const char *my_host, const uint16_t my_port) {
 | 
				
			|||||||
void mqtt_client_task(void *param) {
 | 
					void mqtt_client_task(void *param) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  MessageBuffer_t msg;
 | 
					  MessageBuffer_t msg;
 | 
				
			||||||
  char cPort[4], cMsg[PAYLOAD_BUFFER_SIZE + 1];
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  while (1) {
 | 
					  while (1) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,16 +85,23 @@ void mqtt_client_task(void *param) {
 | 
				
			|||||||
      continue;
 | 
					      continue;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // send data to mqtt server
 | 
					    // send data to mqtt server, if we are connected
 | 
				
			||||||
    if (mqttClient.connected()) {
 | 
					    if (mqttClient.connected()) {
 | 
				
			||||||
      snprintf(cPort, sizeof(cPort), "%d", msg.MessagePort);
 | 
					      mqttClient.beginPublish(MQTT_OUTTOPIC, msg.MessageSize + 2, false);
 | 
				
			||||||
      snprintf(cMsg, sizeof(cMsg), "%0X", msg.Message);
 | 
					      mqttClient.write(msg.MessagePort);
 | 
				
			||||||
      ESP_LOGI(TAG, "Topic=%s | Message=%s", cPort, cMsg);
 | 
					      mqttClient.write('/');
 | 
				
			||||||
      mqttClient.publish(cPort, cMsg);
 | 
					      mqttClient.write(msg.Message, msg.MessageSize);
 | 
				
			||||||
      mqttClient.loop();
 | 
					      if (mqttClient.endPublish()) {
 | 
				
			||||||
      ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize);
 | 
					        ESP_LOGI(TAG, "%d byte(s) sent to MQTT", msg.MessageSize + 2);
 | 
				
			||||||
    } else {
 | 
					        continue; // while(1)
 | 
				
			||||||
      mqtt_enqueuedata(&msg); // re-enqueue the undelivered message
 | 
					      } else
 | 
				
			||||||
 | 
					        goto reconnect;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    } else { // not connected, thus re-enqueue the undelivered message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    reconnect:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      mqtt_enqueuedata(&msg);
 | 
				
			||||||
      delay(10000);
 | 
					      delay(10000);
 | 
				
			||||||
      // attempt to reconnect to MQTT server
 | 
					      // attempt to reconnect to MQTT server
 | 
				
			||||||
      mqtt_connect(MQTT_SERVER, MQTT_PORT);
 | 
					      mqtt_connect(MQTT_SERVER, MQTT_PORT);
 | 
				
			||||||
@ -149,7 +154,7 @@ void mqtt_enqueuedata(MessageBuffer_t *message) {
 | 
				
			|||||||
void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
 | 
					void mqtt_queuereset(void) { xQueueReset(MQTTSendQueue); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void mqtt_callback(char *topic, byte *payload, unsigned int length) {
 | 
					void mqtt_callback(char *topic, byte *payload, unsigned int length) {
 | 
				
			||||||
  if ((length >= 1) && (topic == MQTT_INTOPIC))
 | 
					  ESP_LOGD(TAG, "MQTT %d byte(s) received", length);
 | 
				
			||||||
    rcommand(payload, length);
 | 
					    rcommand(payload, length);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -9,12 +9,11 @@ static const char TAG[] = __FILE__;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static bool useSDCard;
 | 
					static bool useSDCard;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void createFile(void);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
File fileSDCard;
 | 
					File fileSDCard;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool sdcard_init() {
 | 
					bool sdcard_init() {
 | 
				
			||||||
  ESP_LOGI(TAG, "looking for SD-card...");
 | 
					  ESP_LOGI(TAG, "looking for SD-card...");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if HAS_SDCARD == 1
 | 
					#if HAS_SDCARD == 1
 | 
				
			||||||
  pinMode(SDCARD_CS, OUTPUT);
 | 
					  pinMode(SDCARD_CS, OUTPUT);
 | 
				
			||||||
  useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
 | 
					  useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
 | 
				
			||||||
@ -70,7 +69,7 @@ void createFile(void) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  for (int i = 0; i < 100; i++) {
 | 
					  for (int i = 0; i < 100; i++) {
 | 
				
			||||||
    sprintf(bufferFilename, SDCARD_FILE_NAME, i);
 | 
					    sprintf(bufferFilename, SDCARD_FILE_NAME, i);
 | 
				
			||||||
    ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename);
 | 
					    // ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if HAS_SDCARD == 1
 | 
					#if HAS_SDCARD == 1
 | 
				
			||||||
    bool fileExists = SD.exists(bufferFilename);
 | 
					    bool fileExists = SD.exists(bufferFilename);
 | 
				
			||||||
@ -79,7 +78,7 @@ void createFile(void) {
 | 
				
			|||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!fileExists) {
 | 
					    if (!fileExists) {
 | 
				
			||||||
      ESP_LOGD(TAG, "SD: file does not exist: opening");
 | 
					      // ESP_LOGD(TAG, "SD: file does not exist: opening");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if HAS_SDCARD == 1
 | 
					#if HAS_SDCARD == 1
 | 
				
			||||||
      fileSDCard = SD.open(bufferFilename, FILE_WRITE);
 | 
					      fileSDCard = SD.open(bufferFilename, FILE_WRITE);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user