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

135
main.c
View file

@ -1,37 +1,62 @@
#include <stdint.h>
#include "user_interface.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "espconn.h"
#include "main.h"
#include "mqtt.h"
#include "onewire.h"
#include "main.h"
os_timer_t tcpTimer;
os_timer_t pingTimer;
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);
os_timer_disarm(&oneTimer);
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg);
os_timer_arm(&oneTimer, 200, 0);
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 pub(void *arg) {
os_printf("Entered pub!\n");
LOCAL void ICACHE_FLASH_ATTR pubuint(void *arg) {
os_printf("Entered pubuint!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg;
uint8_t data[] = "2.718281828459045";
uint32_t dataLen = os_strlen(data);
mqtt_send(pSession, &data[0], dataLen, MQTT_MSG_TYPE_PUBLISH);
os_timer_disarm(&oneTimer);
os_timer_setfn(&oneTimer, (os_timer_func_t *)ping, arg);
os_timer_arm(&oneTimer, 200, 0);
uint8_t *data = (uint8_t *)(pSession->userData);
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);
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 pubfloat(void *arg) {
os_printf("Entered pubfloat!\n");
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);
os_printf("Encoded string: %s\tString length: %d\n", dataStr, dataLen);
mqtt_send(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) {
os_printf("Entered ping!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
os_timer_disarm(&oneTimer);
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg);
os_timer_arm(&oneTimer, 200, 0);
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 sub(void *arg) {
@ -45,58 +70,80 @@ 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);
os_timer_disarm(&oneTimer);
os_timer_disarm(&pingTimer);
}
LOCAL void ICACHE_FLASH_ATTR dataLog(void *arg) {
mqtt_session_t *pSession = (mqtt_session_t *)arg;
oneWire_t DS18;
oneWire_t *pDS18 = &DS18;
uint16_t temp = 0;
sint32 time = 0;
pDS18->temperature = 0;
reset();
transact(pDS18, SKIP_ROM);
transact(pDS18, CONVERT_T);
time = system_get_time() + 750000;
while(system_get_time() < time);
reset();
transact(pDS18, SKIP_ROM);
transact(pDS18, SCRATCH_READ);
//os_printf("\nReceived onewire data: %x %x\n", pDS18->scratchpad[0], pDS18->scratchpad[1]);
temp = ((uint16_t)pDS18->scratchpad[1] << 8) | pDS18->scratchpad[0];
pDS18->temperature += (temp >> 4) + ((temp & 0x0F) * 0.0625);
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
}
void ICACHE_FLASH_ATTR user_init() {
uint8_t wifiStatus;
//uint8_t wifiStatus;
LOCAL mqtt_session_t globalSession;
LOCAL mqtt_session_t *pGlobalSession = &globalSession;
char ssid[32] = "yourwifissid";
char passkey[64] = "yourwifipassword";
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
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
static const char testUser[11] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52 };
static const uint8_t testUser_len = 11;
static const char testPass[32] = { 0x63, 0x61, 0x62, 0x31, 0x39, 0x36, 0x36, 0x34, 0x31, 0x66, 0x33, 0x63, 0x34, 0x34, 0x36, 0x32, 0x61, 0x35, 0x30, 0x39, 0x64, 0x62, 0x64, 0x31, 0x34, 0x61, 0x30, 0x61, 0x66, 0x36, 0x64, 0x32 };
static const uint8_t testPass_len = 32;
//static const char testPass[4] = { 0x74, 0x65, 0x73, 0x74 };
//static const uint8_t testPass_len = 4;
static const char testTopic[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 testTopic_len = 37;
//static const char testTopic[4] = { 0x74, 0x65, 0x73, 0x74 };
//static const uint8_t testTopic_len = 4;
static const char clientID[5] = { 0x46, 0x5a, 0x5a, 0x4e, 0x30 };
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 esp_tcp_server_ip[4] = {52, 5, 238, 97};
os_memcpy(pGlobalSession->ip, esp_tcp_server_ip, 4);
pGlobalSession->username_len = testUser_len;
static const char adafruitIO_ip[4] = {52, 5, 238, 97};
os_memcpy(pGlobalSession->ip, adafruitIO_ip, 4);
pGlobalSession->username_len = ioUser_len;
pGlobalSession->username = os_zalloc(sizeof(uint8_t) * pGlobalSession->username_len);
os_memcpy(pGlobalSession->username, testUser, pGlobalSession->username_len);
pGlobalSession->password_len = testPass_len;
os_memcpy(pGlobalSession->username, ioUser, pGlobalSession->username_len);
pGlobalSession->password_len = ioKey_len;
pGlobalSession->password = os_zalloc(sizeof(uint8_t) * pGlobalSession->password_len);
os_memcpy(pGlobalSession->password, testPass, pGlobalSession->password_len);
pGlobalSession->topic_name_len = testTopic_len;
os_memcpy(pGlobalSession->password, ioKey, pGlobalSession->password_len);
pGlobalSession->topic_name_len = ioTopic_len;
pGlobalSession->topic_name = os_zalloc(sizeof(uint8_t) * pGlobalSession->topic_name_len);
os_memcpy(pGlobalSession->topic_name, testTopic, pGlobalSession->topic_name_len);
os_memcpy(pGlobalSession->topic_name, ioTopic, pGlobalSession->topic_name_len);
pGlobalSession->client_id_len = clientID_len;
pGlobalSession->client_id = os_zalloc(sizeof(uint8_t) * pGlobalSession->client_id_len);
os_memcpy(pGlobalSession->client_id, clientID, pGlobalSession->client_id_len);
os_timer_setfn(&oneTimer, (os_timer_func_t *)tcpConnect, pGlobalSession);
os_timer_arm(&oneTimer, 15000, 0);
os_timer_setfn(&testTimer, (os_timer_func_t *)con, pGlobalSession);
os_timer_arm(&testTimer, 16000, 0);
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);
os_timer_arm(&pingTimer, 16000, 0);
os_timer_setfn(&pubTimer, (os_timer_func_t *)dataLog, pGlobalSession);
os_timer_arm(&pubTimer, 20000, 1);
}
#endif