Squashed commit of the following:

commit a4632bb7ad
Author: A.M. Rowsell <amrowsell@frozenelectronics.ca>
Date:   Mon Jan 21 20:13:12 2019 -0500

    Sanitizing key and wifi

    Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>

commit 5e3be0f561
Author: A.M. Rowsell <amrowsell@frozenelectronics.ca>
Date:   Mon Jan 21 19:53:42 2019 -0500

    It's alive! New code all working.

    The OneWire library works. It's a bit hacked together, as neither
    the software timer or hardware timer APIs would have worked well,
    because they are implemented terribly by Espressif. The easiest
    way to get around this was to just use system_get_time() and work
    off of that for timing in one-wire comms.

    Split the publish function into two separate functions: one to
    publish floating point numbers, and one to publish integers. In
    a language like Lua or C++ you could have these as one function,
    but in C it's easier to just split them.

    The main.c has a new function called dataLog that deals with
    getting the DS18B20 data and then handing that off to pubfloat().

    I updated the timer names to be more descriptive.

    I grabbed some code to convert integers and floats to strings, as
    I can't be bothered to write that code myself for the millionth
    time.

    If something goes wrong and we are disconnected from our TCP
    connection, all timers are halted so we don't blindly keep
    trying to send packets over a non-existent link.

    Unfortunately the onewire library is hardcoded to use pin 5.
    That will be the next update.

    Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>

commit 48702bf328
Author: A.M. Rowsell <amrowsell@frozenelectronics.ca>
Date:   Sun Jan 20 21:13:04 2019 -0500

    First version that compiles with new file layout.

    Had to make many changes to get it to compile across many files,
    including removing the LOCAL specifier from almost all functions.
    Edited the Makefile to compile all three files and link them.
    Haven't tested on hardware yet, that's the next step.

    Lots of small changes to avoid warnings, now that I have turned
    -Wall on. This makes the code a bit "better".

    Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>

commit 1cd8191682
Author: A.M. Rowsell <amrowsell@frozenelectronics.ca>
Date:   Sat Jan 19 15:25:44 2019 -0500

    New branch for my new AIO key.

    Will have to remember to sanitize before committing to master.

    Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>

Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>
This commit is contained in:
A.M. Rowsell 2019-01-21 20:14:09 -05:00
commit 627df20e53
Signed by: amr
GPG key ID: 0B6E2D8375CF79A9
8 changed files with 317 additions and 106 deletions

112
mqtt.c
View file

@ -3,6 +3,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <stdint.h>
#include <math.h>
#include "user_interface.h"
#include "ets_sys.h"
#include "osapi.h"
@ -12,6 +13,7 @@
#include "os_type.h"
#include "mqtt.h"
#include "onewire.h"
#include "main.h"
/* Functions we will need to implement:
* Send -- will handle all sending of all packets
@ -22,18 +24,75 @@
* security or QoS in this basic implementation.
*/
#ifdef DEBUG
static os_timer_t oneTimer;
static os_timer_t testTimer;
#endif
static os_timer_t MQTT_KeepAliveTimer;
static os_timer_t waitForWifiTimer;
LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg) {
// reverses a string 'str' of length 'len'
void reverse(char *str, int len)
{
int i=0, j=len-1, temp;
while (i<j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
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 i = 0;
while (x)
{
str[i++] = (x%10) + '0';
x = x/10;
}
// If number of digits required is more, then
// add 0s at the beginning
while (i < d)
str[i++] = '0';
reverse(str, i);
str[i] = '\0';
return i;
}
// Converts a floating point number to string.
void ftoa(float n, char *res, int afterpoint)
{
// Extract integer part
int ipart = (int)n;
// Extract floating part
float fpart = n - (float)ipart;
// convert integer part to string
int i = intToStr(ipart, res, 0);
// check for display option after point
if (afterpoint != 0)
{
res[i] = '.'; // add dot
// Get the value of fraction part upto given no.
// of points after dot. The third parameter is needed
// to handle cases like 233.007
fpart = fpart * pow(10, afterpoint);
intToStr((int)fpart, res + i + 1, afterpoint);
}
}
void ICACHE_FLASH_ATTR data_sent_callback(void *arg) {
os_printf("Data sent!\n");
}
LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len) {
void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len) {
struct espconn *pConn = arg;
mqtt_session_t *session = pConn->reverse;
// deal with received data
@ -72,9 +131,15 @@ LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned
os_printf("Connection refused -- illegal CONNACK return code.\n");
break;
}
if(session->connack_cb != NULL) {
session->connack_cb(pdata);
}
break;
case MQTT_MSG_TYPE_PUBLISH:
os_printf("Application message from server: %s\n", &pdata[3]); // probably incorrect
if(session->publish_cb != NULL) {
session->publish_cb(pdata);
}
break;
case MQTT_MSG_TYPE_SUBACK:
os_printf("Subscription acknowledged\n");
@ -85,11 +150,27 @@ LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned
case MQTT_MSG_TYPE_PINGRESP:
os_printf("Pong!\n");
break;
// all remaining cases listed to avoid warnings
case MQTT_MSG_TYPE_CONNECT:
case MQTT_MSG_TYPE_PUBACK:
case MQTT_MSG_TYPE_PUBREC:
case MQTT_MSG_TYPE_PUBREL:
case MQTT_MSG_TYPE_PUBCOMP:
case MQTT_MSG_TYPE_SUBSCRIBE:
case MQTT_MSG_TYPE_UNSUBSCRIBE:
case MQTT_MSG_TYPE_PINGREQ:
case MQTT_MSG_TYPE_DISCONNECT:
default:
if(msgType == MQTT_MSG_TYPE_DISCONNECT) {
os_printf("MQTT Disconnect packet\n");
}
return;
break;
}
}
LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg) {
void ICACHE_FLASH_ATTR connected_callback(void *arg) {
struct espconn *pConn = arg;
mqtt_session_t *pSession = pConn->reverse;
#ifdef DEBUG
@ -102,16 +183,18 @@ LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg) {
pSession->validConnection = 1;
}
LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
os_printf("Reconnected?\n");
os_printf("Error code: %d\n", err);
}
LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
os_printf("Disconnected\n");
os_timer_disarm(&pubTimer);
os_timer_disarm(&pingTimer);
}
LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
os_timer_disarm(&waitForWifiTimer);
struct ip_info ipConfig;
mqtt_session_t *session = arg;
@ -158,11 +241,13 @@ LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
// set timer to try again
os_timer_setfn(&waitForWifiTimer, (os_timer_func_t *)tcpConnect, session);
os_timer_arm(&waitForWifiTimer, 1000, 0);
return 2;
}
return 0;
}
LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) {
uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) {
uint8_t *encodedByte = os_zalloc(sizeof(uint8_t) * 5); // can't be more than 5 bytes
uint8_t numBytes = 1;
do {
@ -179,12 +264,12 @@ LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) {
}
LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg) {
void ICACHE_FLASH_ATTR pingAlive(void *arg) {
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
}
LOCAL 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 mqtt_send(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
@ -355,4 +440,5 @@ LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data
} else {
os_printf("No wifi! Narf!\n");
}
return 0;
}