Updated formatting with astyle -A14 -S
Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>
This commit is contained in:
parent
3de8cc9ed7
commit
7c5dfae57b
7 changed files with 651 additions and 638 deletions
|
@ -8,3 +8,5 @@ While working on a series of tutorials for Hackaday, I realized MQTT would be th
|
|||
At this point, it's a bit messy. It still needs a lot of work. This documentation will help me keep everything organized and easy-to-use.
|
||||
|
||||
The documentation has now been turned into Doxygen documentation. You can view the latest documentation [here](https://mraureliusr.gitlab.io/mqtt-esp8266/index.html)
|
||||
|
||||
Make sure to check out the corresponding hackaday.io project!
|
||||
|
|
18
main.c
18
main.c
|
@ -14,7 +14,9 @@ os_timer_t pingTimer;
|
|||
os_timer_t pubTimer;
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
|
||||
#ifdef DEBUG
|
||||
os_printf("Entered con!\n");
|
||||
#endif
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT);
|
||||
|
||||
|
@ -24,7 +26,9 @@ LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
|
|||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR pubuint(void *arg) {
|
||||
#ifdef DEBUG
|
||||
os_printf("Entered pubuint!\n");
|
||||
#endif
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
uint8_t *data = (uint8_t *)(pSession->userData);
|
||||
char *dataStr = os_zalloc(20 * sizeof(char));
|
||||
|
@ -37,13 +41,17 @@ LOCAL void ICACHE_FLASH_ATTR pubuint(void *arg) {
|
|||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR pubfloat(void *arg) {
|
||||
#ifdef DEBUG
|
||||
os_printf("Entered pubfloat!\n");
|
||||
#endif
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
float *data = (float *)(pSession->userData);
|
||||
char *dataStr = os_zalloc(20 * sizeof(char));
|
||||
ftoa(*data, dataStr, 2);
|
||||
int32_t dataLen = os_strlen(dataStr);
|
||||
#ifdef DEBUG
|
||||
os_printf("Encoded string: %s\tString length: %d\n", dataStr, dataLen);
|
||||
#endif
|
||||
mqttSend(pSession, (uint8_t *)dataStr, dataLen, MQTT_MSG_TYPE_PUBLISH);
|
||||
os_timer_disarm(&pingTimer);
|
||||
os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
|
||||
|
@ -62,14 +70,18 @@ LOCAL void ICACHE_FLASH_ATTR ping(void *arg) {
|
|||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR sub(void *arg) {
|
||||
#ifdef DEBUG
|
||||
os_printf("Entered sub!\n");
|
||||
#endif
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_SUBSCRIBE);
|
||||
|
||||
}
|
||||
|
||||
LOCAL void ICACHE_FLASH_ATTR discon(void *arg) {
|
||||
#ifdef DEBUG
|
||||
os_printf("Entered discon!\n");
|
||||
#endif
|
||||
mqtt_session_t *pSession = (mqtt_session_t *)arg;
|
||||
mqttSend(pSession, NULL, 0, MQTT_MSG_TYPE_DISCONNECT);
|
||||
os_timer_disarm(&pingTimer);
|
||||
|
@ -92,11 +104,15 @@ LOCAL void ICACHE_FLASH_ATTR dataLog(void *arg) {
|
|||
reset();
|
||||
transact(pDS18, SKIP_ROM);
|
||||
transact(pDS18, SCRATCH_READ);
|
||||
//os_printf("\nReceived onewire data: %x %x\n", pDS18->scratchpad[0], pDS18->scratchpad[1]);
|
||||
#ifdef DEBUG
|
||||
os_printf("\nReceived onewire data: %x %x\n", pDS18->scratchpad[0], pDS18->scratchpad[1]);
|
||||
#endif
|
||||
temp = ((uint16_t)pDS18->scratchpad[1] << 8) | pDS18->scratchpad[0];
|
||||
pDS18->temperature += (temp >> 4) + ((temp & 0x0F) * 0.0625);
|
||||
pSession->userData = (void *)&pDS18->temperature;
|
||||
#ifdef DEBUG
|
||||
os_printf("\nTemperature is: %d.%02d\n", (int)pDS18->temperature, (int)(pDS18->temperature * 100) % 100);
|
||||
#endif
|
||||
pubfloat(pSession); // publish the temperature
|
||||
//discon(pSession);
|
||||
return;
|
||||
|
|
30
mqtt.c
30
mqtt.c
|
@ -28,26 +28,23 @@ static os_timer_t MQTT_KeepAliveTimer;
|
|||
static os_timer_t waitForWifiTimer;
|
||||
|
||||
// reverses a string 'str' of length 'len'
|
||||
void reverse(char *str, int len)
|
||||
{
|
||||
void reverse(char *str, int len) {
|
||||
int i=0, j=len-1, temp;
|
||||
while (i<j)
|
||||
{
|
||||
while (i<j) {
|
||||
temp = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = temp;
|
||||
i++; j--;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
// Converts a given integer x to string str[]. d is the number
|
||||
// of digits required in output. If d is more than the number
|
||||
// of digits in x, then 0s are added at the beginning.
|
||||
int intToStr(int x, char str[], int d)
|
||||
{
|
||||
int intToStr(int x, char str[], int d) {
|
||||
int i = 0;
|
||||
while (x)
|
||||
{
|
||||
while (x) {
|
||||
str[i++] = (x%10) + '0';
|
||||
x = x/10;
|
||||
}
|
||||
|
@ -63,8 +60,7 @@ int intToStr(int x, char str[], int d)
|
|||
}
|
||||
|
||||
// Converts a floating point number to string.
|
||||
void ftoa(float n, char *res, int afterpoint)
|
||||
{
|
||||
void ftoa(float n, char *res, int afterpoint) {
|
||||
// Extract integer part
|
||||
int ipart = (int)n;
|
||||
|
||||
|
@ -75,8 +71,7 @@ void ftoa(float n, char *res, int afterpoint)
|
|||
int i = intToStr(ipart, res, 0);
|
||||
|
||||
// check for display option after point
|
||||
if (afterpoint != 0)
|
||||
{
|
||||
if (afterpoint != 0) {
|
||||
res[i] = '.'; // add dot
|
||||
|
||||
// Get the value of fraction part upto given no.
|
||||
|
@ -206,7 +201,6 @@ uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
|
|||
LOCAL struct espconn conn;
|
||||
LOCAL struct _esp_tcp tcp_s;
|
||||
conn.reverse = arg;
|
||||
os_printf("Everything looks good!\n");
|
||||
#ifdef DEBUG
|
||||
os_printf("Entered tcpConnect\n");
|
||||
#endif
|
||||
|
@ -221,13 +215,16 @@ uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
|
|||
conn.proto.tcp->remote_port = session->port;
|
||||
conn.state = ESPCONN_NONE;
|
||||
os_memcpy(conn.proto.tcp->remote_ip, session->ip, 4);
|
||||
|
||||
#ifdef DEBUG
|
||||
os_printf("About to register callbacks\n");
|
||||
#endif
|
||||
// register callbacks
|
||||
espconn_regist_connectcb(&conn, (espconn_connect_callback)connected_callback);
|
||||
espconn_regist_reconcb(&conn, (espconn_reconnect_callback)reconnected_callback);
|
||||
espconn_regist_disconcb(&conn, (espconn_connect_callback)disconnected_callback);
|
||||
#ifdef DEBUG
|
||||
os_printf("About to connect\n");
|
||||
#endif
|
||||
//make the connection
|
||||
if(espconn_connect(&conn) == 0) {
|
||||
os_printf("Connection successful\n");
|
||||
|
@ -239,8 +236,7 @@ uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
|
|||
os_printf("About to return from TCP connect\n");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// set timer to try again
|
||||
os_timer_setfn(&waitForWifiTimer, (os_timer_func_t *)tcpConnect, session);
|
||||
os_timer_arm(&waitForWifiTimer, 1000, 0);
|
||||
|
|
7
mqtt.h
7
mqtt.h
|
@ -20,7 +20,7 @@
|
|||
#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
|
||||
|
@ -28,8 +28,7 @@
|
|||
*
|
||||
* 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
|
||||
{
|
||||
typedef enum mqtt_message_enum {
|
||||
MQTT_MSG_TYPE_CONNECT = 1,
|
||||
MQTT_MSG_TYPE_CONNACK = 2,
|
||||
MQTT_MSG_TYPE_PUBLISH = 3,
|
||||
|
@ -135,7 +134,7 @@ uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength);
|
|||
* @param session a pointer to the active mqtt_session_t
|
||||
* @param data a pointer to the data to be published; only applied to MQTT_MSG_TYPE_PUBLISH
|
||||
* @param len the length of the above data
|
||||
* @param msgType the type of message to be send, one of the mqtt_message_type
|
||||
* @param msgType the type of message to be sent, one of the mqtt_message_type
|
||||
* @return -1 in case of error, 0 otherwise
|
||||
*/
|
||||
uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue