Join Mqtt and Docker branch
Some checks reported errors
continuous-integration/drone/push Build was killed
Some checks reported errors
continuous-integration/drone/push Build was killed
This commit is contained in:
parent
97339527ee
commit
f9838748b2
4 changed files with 163 additions and 6 deletions
|
@ -5,7 +5,7 @@ EXTRA_CFLAGS=-O3
|
||||||
#Enable debugging
|
#Enable debugging
|
||||||
#EXTRA_CFLAGS+=-DLWIP_DEBUG=1 -DHTTPD_DEBUG=LWIP_DBG_ON
|
#EXTRA_CFLAGS+=-DLWIP_DEBUG=1 -DHTTPD_DEBUG=LWIP_DBG_ON
|
||||||
|
|
||||||
EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/mbedtls extras/httpd extras/sntp extras/cpp_support
|
EXTRA_COMPONENTS=extras/paho_mqtt_c extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/mbedtls extras/httpd extras/sntp extras/cpp_support
|
||||||
|
|
||||||
LIBS = hal m
|
LIBS = hal m
|
||||||
|
|
||||||
|
|
|
@ -301,7 +301,6 @@ void user_init(void) {
|
||||||
int8_t wifi_ap_channel = 6;
|
int8_t wifi_ap_channel = 6;
|
||||||
sysparam_get_int8("wifi_ap_channel", &wifi_ap_channel);
|
sysparam_get_int8("wifi_ap_channel", &wifi_ap_channel);
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
/* AU does not allow channels above 13, although 14 works. */
|
/* AU does not allow channels above 13, although 14 works. */
|
||||||
if(wifi_ap_channel > 13) {
|
if(wifi_ap_channel > 13) {
|
||||||
|
@ -408,7 +407,10 @@ void user_init(void) {
|
||||||
/* initialize tasks */
|
/* initialize tasks */
|
||||||
xTaskCreate(&httpd_task, "httpd_task", 2048, NULL, 3, NULL);
|
xTaskCreate(&httpd_task, "httpd_task", 2048, NULL, 3, NULL);
|
||||||
|
|
||||||
xTaskCreate(&sntp_task, "SNTP", 512, NULL, 2, NULL);
|
xTaskCreate(&sntp_task, "SNTP", 512, NULL, 1, NULL);
|
||||||
|
|
||||||
|
// xTaskCreate(&beat_task, "beat_task", 256, NULL, 3, NULL);
|
||||||
|
xTaskCreate(&mqtt_task, "mqtt_task", 512, NULL, 4, NULL);
|
||||||
}
|
}
|
||||||
>>>>>>> 4b8d354 (basic webconf)
|
>>>>>>> 4b8d354 (basic webconf)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,3 +3,158 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <espressif/esp_common.h>
|
||||||
|
#include <espressif/user_interface.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
#include <paho_mqtt_c/MQTTESP8266.h>
|
||||||
|
#include <paho_mqtt_c/MQTTClient.h>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <semphr.h>
|
||||||
|
|
||||||
|
|
||||||
|
/* You can use http://test.mosquitto.org/ to test mqtt_client instead
|
||||||
|
* of setting up your own MQTT server */
|
||||||
|
#define MQTT_HOST ("172.16.1.53")
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
|
||||||
|
#define MQTT_USER NULL
|
||||||
|
#define MQTT_PASS NULL
|
||||||
|
|
||||||
|
QueueHandle_t publish_queue;
|
||||||
|
#define PUB_MSG_LEN 16
|
||||||
|
|
||||||
|
extern "C" void beat_task(void *pvParameters) {
|
||||||
|
TickType_t xLastWakeTime = xTaskGetTickCount();
|
||||||
|
char msg[PUB_MSG_LEN];
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
vTaskDelayUntil(&xLastWakeTime, 10000 / portTICK_PERIOD_MS);
|
||||||
|
printf("beat\r\n");
|
||||||
|
snprintf(msg, PUB_MSG_LEN, "Beat %d\r\n", count++);
|
||||||
|
if(xQueueSend(publish_queue, (void *) msg, 0) == pdFALSE) {
|
||||||
|
printf("Publish queue overflow.\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void topic_received(mqtt_message_data_t *md) {
|
||||||
|
int i;
|
||||||
|
mqtt_message_t *message = md->message;
|
||||||
|
printf("Received: ");
|
||||||
|
for (i = 0; i < md->topic->lenstring.len; ++i)
|
||||||
|
printf("%c", md->topic->lenstring.data[i]);
|
||||||
|
|
||||||
|
printf(" = ");
|
||||||
|
for (i = 0; i < (int) message->payloadlen; ++i)
|
||||||
|
printf("%c", ((char *) (message->payload))[i]);
|
||||||
|
|
||||||
|
printf("\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *get_my_id(void) {
|
||||||
|
// Use MAC address for Station as unique ID
|
||||||
|
static char my_id[13];
|
||||||
|
static bool my_id_done = false;
|
||||||
|
int8_t i;
|
||||||
|
uint8_t x;
|
||||||
|
if(my_id_done)
|
||||||
|
return my_id;
|
||||||
|
if(!sdk_wifi_get_macaddr(STATION_IF, (uint8_t *) my_id))
|
||||||
|
return NULL;
|
||||||
|
for (i = 5; i >= 0; --i) {
|
||||||
|
x = my_id[i] & 0x0F;
|
||||||
|
if(x > 9) x += 7;
|
||||||
|
my_id[i * 2 + 1] = x + '0';
|
||||||
|
x = my_id[i] >> 4;
|
||||||
|
if(x > 9) x += 7;
|
||||||
|
my_id[i * 2] = x + '0';
|
||||||
|
}
|
||||||
|
my_id[12] = '\0';
|
||||||
|
my_id_done = true;
|
||||||
|
return my_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" void mqtt_task(void *pvParameters) {
|
||||||
|
int ret = 0;
|
||||||
|
struct mqtt_network network;
|
||||||
|
mqtt_client_t client = mqtt_client_default;
|
||||||
|
char mqtt_client_id[20];
|
||||||
|
uint8_t mqtt_buf[100];
|
||||||
|
uint8_t mqtt_readbuf[100];
|
||||||
|
mqtt_packet_connect_data_t data = mqtt_packet_connect_data_initializer;
|
||||||
|
|
||||||
|
mqtt_network_new(&network);
|
||||||
|
memset(mqtt_client_id, 0, sizeof(mqtt_client_id));
|
||||||
|
strcpy(mqtt_client_id, "ESP-");
|
||||||
|
strcat(mqtt_client_id, get_my_id());
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
printf("%s: started\n\r", __func__);
|
||||||
|
printf("%s: (Re)connecting to MQTT server %s ... ", __func__,
|
||||||
|
MQTT_HOST);
|
||||||
|
ret = mqtt_network_connect(&network, MQTT_HOST, MQTT_PORT);
|
||||||
|
if(ret) {
|
||||||
|
printf("error: %d\n\r", ret);
|
||||||
|
taskYIELD();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
printf("done\n\r");
|
||||||
|
mqtt_client_new(&client, &network, 5000, mqtt_buf, 100,
|
||||||
|
mqtt_readbuf, 100);
|
||||||
|
|
||||||
|
data.willFlag = 0;
|
||||||
|
data.MQTTVersion = 3;
|
||||||
|
data.clientID.cstring = mqtt_client_id;
|
||||||
|
data.username.cstring = MQTT_USER;
|
||||||
|
data.password.cstring = MQTT_PASS;
|
||||||
|
data.keepAliveInterval = 10;
|
||||||
|
data.cleansession = 0;
|
||||||
|
printf("Send MQTT connect ... ");
|
||||||
|
ret = mqtt_connect(&client, &data);
|
||||||
|
if(ret) {
|
||||||
|
printf("error: %d\n\r", ret);
|
||||||
|
mqtt_network_disconnect(&network);
|
||||||
|
taskYIELD();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
printf("done\r\n");
|
||||||
|
mqtt_subscribe(&client, "/esptopic", MQTT_QOS1, topic_received);
|
||||||
|
xQueueReset(publish_queue);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
char msg[PUB_MSG_LEN - 1] = "\0";
|
||||||
|
while (xQueueReceive(publish_queue, (void *) msg, 0) ==
|
||||||
|
pdTRUE) {
|
||||||
|
printf("got message to publish\r\n");
|
||||||
|
mqtt_message_t message;
|
||||||
|
message.payload = msg;
|
||||||
|
message.payloadlen = PUB_MSG_LEN;
|
||||||
|
message.dup = 0;
|
||||||
|
message.qos = MQTT_QOS1;
|
||||||
|
message.retained = 0;
|
||||||
|
ret = mqtt_publish(&client, "/beat", &message);
|
||||||
|
if(ret != MQTT_SUCCESS) {
|
||||||
|
printf("error while publishing message: %d\n", ret);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = mqtt_yield(&client, 1000);
|
||||||
|
if(ret == MQTT_DISCONNECTED)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
printf("Connection dropped, request restart\n\r");
|
||||||
|
mqtt_network_disconnect(&network);
|
||||||
|
taskYIELD();
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,15 +5,15 @@
|
||||||
#ifndef FIRMWARE_MQTT_H
|
#ifndef FIRMWARE_MQTT_H
|
||||||
#define FIRMWARE_MQTT_H
|
#define FIRMWARE_MQTT_H
|
||||||
|
|
||||||
<<<<<<< HEAD
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void mqtt_task(void *pvParameters);
|
||||||
|
void beat_task(void *pvParameters);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
=======
|
|
||||||
>>>>>>> 7d1fb53 (refactoring code)
|
|
||||||
#endif //FIRMWARE_MQTT_H
|
#endif //FIRMWARE_MQTT_H
|
||||||
|
|
Loading…
Reference in a new issue