Updated to v0.3.1

Renamed mqtt_send to mqttSend to better follow my usual naming
convention. Made a few other small changes, including disabling
the debug macro by default. Just uncomment to add it again.

This is the version that will be used with the first release
of Part 3 of the SDK tutorials on hackaday.io.

Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>
This commit is contained in:
A.M. Rowsell 2019-02-27 02:28:00 -05:00
commit 8acff6e880
Signed by: amr
GPG key ID: 0B6E2D8375CF79A9
4 changed files with 32 additions and 21 deletions

View file

@ -38,7 +38,7 @@ PROJECT_NAME = mqtt-esp8266
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = v0.2
PROJECT_NUMBER = v0.3.1
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a

32
main.c
View file

@ -16,7 +16,7 @@ os_timer_t pubTimer;
LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
os_printf("Entered con!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT);
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT);
os_timer_disarm(&pingTimer);
os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
@ -30,7 +30,7 @@ LOCAL void ICACHE_FLASH_ATTR pubuint(void *arg) {
char *dataStr = os_zalloc(20 * sizeof(char));
intToStr(*data, dataStr, 4);
int32_t dataLen = os_strlen(dataStr);
mqtt_send(pSession, (uint8_t *)dataStr, dataLen, MQTT_MSG_TYPE_PUBLISH);
mqttSend(pSession, (uint8_t *)dataStr, dataLen, MQTT_MSG_TYPE_PUBLISH);
os_timer_disarm(&pingTimer);
os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
os_timer_arm(&pingTimer, 1000, 0);
@ -44,16 +44,18 @@ LOCAL void ICACHE_FLASH_ATTR pubfloat(void *arg) {
ftoa(*data, dataStr, 2);
int32_t dataLen = os_strlen(dataStr);
os_printf("Encoded string: %s\tString length: %d\n", dataStr, dataLen);
mqtt_send(pSession, (uint8_t *)dataStr, dataLen, MQTT_MSG_TYPE_PUBLISH);
mqttSend(pSession, (uint8_t *)dataStr, dataLen, MQTT_MSG_TYPE_PUBLISH);
os_timer_disarm(&pingTimer);
os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
os_timer_arm(&pingTimer, 1000, 0);
}
LOCAL void ICACHE_FLASH_ATTR ping(void *arg) {
#ifdef DEBUG
os_printf("Entered ping!\n");
#endif
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
os_timer_disarm(&pingTimer);
os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
os_timer_arm(&pingTimer, 1000, 0);
@ -62,15 +64,16 @@ LOCAL void ICACHE_FLASH_ATTR ping(void *arg) {
LOCAL void ICACHE_FLASH_ATTR sub(void *arg) {
os_printf("Entered sub!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_SUBSCRIBE);
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_SUBSCRIBE);
}
LOCAL void ICACHE_FLASH_ATTR discon(void *arg) {
os_printf("Entered discon!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_DISCONNECT);
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_DISCONNECT);
os_timer_disarm(&pingTimer);
os_timer_disarm(&pubTimer);
}
LOCAL void ICACHE_FLASH_ATTR dataLog(void *arg) {
@ -79,6 +82,7 @@ LOCAL void ICACHE_FLASH_ATTR dataLog(void *arg) {
oneWire_t *pDS18 = &DS18;
uint16_t temp = 0;
sint32 time = 0;
pDS18->temperature = 0;
reset();
transact(pDS18, SKIP_ROM);
@ -94,38 +98,41 @@ LOCAL void ICACHE_FLASH_ATTR dataLog(void *arg) {
pSession->userData = (void *)&pDS18->temperature;
os_printf("\nTemperature is: %d.%02d\n", (int)pDS18->temperature, (int)(pDS18->temperature * 100) % 100);
pubfloat(pSession); // publish the temperature
//discon(pSession);
return;
}
void ICACHE_FLASH_ATTR user_init() {
//uint8_t wifiStatus;
LOCAL mqtt_session_t globalSession;
LOCAL mqtt_session_t *pGlobalSession = &globalSession;
char ssid[32] = "yourwifissid";
char passkey[64] = "yourwifipass";
struct station_config stationConf;
gpio_init(); // init gpio so we can use the LED
uart_div_modify(0, UART_CLK_FREQ / 115200); // set UART to 115200
gpio_init(); // init gpio so we can use the LED & onewire bus
uart_div_modify(0, UART_CLK_FREQ / 921600); // set UART to 921600
wifi_status_led_install(0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); // set GPIO0 as status LED
stationConf.bssid_set = 0;
os_memcpy(&stationConf.ssid, ssid, 32); // copy the ssid and passkey into the station_config struct
os_memcpy(&stationConf.password, passkey, 64);
wifi_set_opmode_current(0x01); //station mode
wifi_station_set_config_current(&stationConf); // tell it about our config, this auto-connects us as well
// prepare the TCP/MQTT connection stuff
// Adafruit IO is at 52.5.238.97
// use strtoarr.py to generate these
static const char ioUser[11] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52 }; // MrAureliusR
static const uint8_t ioUser_len = 11;
static const char ioKey[32] = { }; // use strtoarr.py to generate these
static const uint8_t ioKey_len = 32;
static const char ioTopic[27] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52, 0x2f, 0x66, 0x65, 0x65, 0x64, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x74, 0x6f, 0x70, 0x69, 0x63 }; // MrAureliusR/feeds/testtopic
static const uint8_t ioTopic_len = 27;
/* static const char ioTopic[37] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52, 0x2f, 0x66, 0x65, 0x65, 0x64, 0x73, 0x2f, 0x62, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x2e, 0x62, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x74, 0x65, 0x6d, 0x70 }; */
/* static const uint8_t ioTopic_len = 37; */
static const char clientID[5] = { 0x46, 0x52, 0x5a, 0x4e, 0x30 }; // FRZN0
static const uint8_t clientID_len = 5;
pGlobalSession->port = 1883; // mqtt port
static const char adafruitIO_ip[4] = {52, 5, 238, 97};
// copy all the above info into our mqtt_session_t
os_memcpy(pGlobalSession->ip, adafruitIO_ip, 4);
pGlobalSession->username_len = ioUser_len;
pGlobalSession->username = os_zalloc(sizeof(uint8_t) * pGlobalSession->username_len);
@ -140,6 +147,7 @@ void ICACHE_FLASH_ATTR user_init() {
pGlobalSession->client_id = os_zalloc(sizeof(uint8_t) * pGlobalSession->client_id_len);
os_memcpy(pGlobalSession->client_id, clientID, pGlobalSession->client_id_len);
// set up all the timers to connect and log data
os_timer_setfn(&tcpTimer, (os_timer_func_t *)tcpConnect, pGlobalSession);
os_timer_arm(&tcpTimer, 12000, 0);
os_timer_setfn(&pingTimer, (os_timer_func_t *)con, pGlobalSession);

9
mqtt.c
View file

@ -89,7 +89,10 @@ void ftoa(float n, char *res, int afterpoint)
}
void ICACHE_FLASH_ATTR data_sent_callback(void *arg) {
#ifdef DEBUG
os_printf("Data sent!\n");
#endif
return;
}
void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len) {
@ -266,14 +269,14 @@ uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) {
void ICACHE_FLASH_ATTR pingAlive(void *arg) {
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
}
uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType) {
uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType) {
if(session->validConnection == 1) {
os_timer_disarm(&MQTT_KeepAliveTimer); // disable timer if we are called
#ifdef DEBUG
os_printf("Entering mqtt_send!\n");
os_printf("Entering mqttSend!\n");
#endif
LOCAL mqtt_packet_t packet;
LOCAL mqtt_packet_t *pPacket = &packet;

6
mqtt.h
View file

@ -20,13 +20,13 @@
#include "espconn.h"
#include "os_type.h"
#define DEBUG 1 /**< This define enables or disables serial debug output in most places */
//#define DEBUG 1 /**< This define enables or disables serial debug output in most places */
/**
* @typedef
* This is the enum for message types.
*
* This enum was taken from the NodeMCU mqtt headers. Credit is given to "zeroday" of nodemcu.com. This enum gives us both easily readable names for each message type, but also the correct value for each type. These are used in mqtt_send() to construct the correct packet type.
* This enum was taken from the NodeMCU mqtt headers. Credit is given to "zeroday" of nodemcu.com. This enum gives us both easily readable names for each message type, but also the correct value for each type. These are used in mqttSend() to construct the correct packet type.
*/
typedef enum mqtt_message_enum
{
@ -138,4 +138,4 @@ uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength);
* @param msgType the type of message to be send, one of the mqtt_message_type
* @return -1 in case of error, 0 otherwise
*/
uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType);
uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType);