From 627df20e53d30f4266a08147e5fa4a9dedb49320 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Mon, 21 Jan 2019 20:14:09 -0500 Subject: [PATCH] Squashed commit of the following: commit a4632bb7ad684a5587f313bd25304609d7c66e16 Author: A.M. Rowsell Date: Mon Jan 21 20:13:12 2019 -0500 Sanitizing key and wifi Signed-off-by: A.M. Rowsell commit 5e3be0f5615d55d76ec1a09cd919add82cb21bdf Author: A.M. Rowsell 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 commit 48702bf3288cc7f9646617baf057173d609fb2c7 Author: A.M. Rowsell 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 commit 1cd81916822aae46332569951381706f20869910 Author: A.M. Rowsell 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 Signed-off-by: A.M. Rowsell --- Makefile | 12 ++--- main.c | 135 +++++++++++++++++++++++++++++++++++----------------- main.h | 14 ++++-- mqtt.c | 112 ++++++++++++++++++++++++++++++++++++++----- mqtt.h | 21 ++++---- onewire.c | 109 ++++++++++++++++++++++++++++++++++-------- onewire.h | 16 +++---- strtoarr.py | 4 +- 8 files changed, 317 insertions(+), 106 deletions(-) diff --git a/Makefile b/Makefile index be6a43f..4d82140 100644 --- a/Makefile +++ b/Makefile @@ -2,14 +2,14 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -P=mqtt +P=main CC=xtensa-lx106-elf-gcc AR=xtensa-lx106-elf-ar LDLIBS=-nostdlib -ggdb -Wl,-Map=output.map -Wl,--start-group -lm -lc -lhal -lpp -llwip -lphy -lnet80211 -lwpa -lat -lwpa2 -lmain -Wl,--end-group -lgcc -CFLAGS= -I. -mlongcalls -std=gnu11 +CFLAGS= -I. -mlongcalls -std=gnu11 -Wall LDFLAGS=-Teagle.app.v6.ld -OBJ=mqtt.o -DEPS=mqtt.h +OBJ=main.o mqtt.o onewire.o +DEPS=main.h mqtt.h onewire.h $(P)-0x00000.bin: $(P) esptool.py elf2image $^ @@ -19,10 +19,10 @@ $(P): $(OBJ) %.o: %.c $(DEPS) flash: $(P)-0x00000.bin - esptool.py --port /dev/feather1 write_flash 0 $(P)-0x00000.bin 0x10000 $(P)-0x10000.bin + esptool.py --port /dev/feather0 write_flash 0 $(P)-0x00000.bin 0x10000 $(P)-0x10000.bin clean: - rm -f $(P) $(P).o $(P)-0x00000.bin $(P)-0x10000.bin + rm -f $(P) $(OBJ) $(P)-0x00000.bin $(P)-0x10000.bin *~ library: $(CC) -c -fPIC $(CFLAGS) $(P).c -o $(P).o diff --git a/main.c b/main.c index b544ebe..9d96351 100644 --- a/main.c +++ b/main.c @@ -1,37 +1,62 @@ +#include +#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 diff --git a/main.h b/main.h index b0fd905..b37edbe 100644 --- a/main.h +++ b/main.h @@ -1,8 +1,16 @@ -#include "mqtt.h" -#include "onewire.h" +#include "os_type.h" + +extern os_timer_t tcpTimer; +extern os_timer_t pingTimer; +extern os_timer_t pubTimer; + +void reverse(char *str, int len); +int intToStr(int x, char str[], int d); +void ftoa(float n, char *res, int afterpoint); LOCAL void ICACHE_FLASH_ATTR con(void *arg); -LOCAL void ICACHE_FLASH_ATTR pub(void *arg); +LOCAL void ICACHE_FLASH_ATTR pubuint(void *arg); +LOCAL void ICACHE_FLASH_ATTR pubfloat(void *arg); LOCAL void ICACHE_FLASH_ATTR sub(void *arg); LOCAL void ICACHE_FLASH_ATTR ping(void *arg); LOCAL void ICACHE_FLASH_ATTR discon(void *arg); diff --git a/mqtt.c b/mqtt.c index 61b3cfe..7192c25 100644 --- a/mqtt.c +++ b/mqtt.c @@ -3,6 +3,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include +#include #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 (ireverse; // 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; } diff --git a/mqtt.h b/mqtt.h index 0f50434..a593341 100644 --- a/mqtt.h +++ b/mqtt.h @@ -82,7 +82,10 @@ typedef struct { uint32_t password_len; /**< The length of the password string */ char validConnection; /**< Boolean value which indicates whether or not the TCP connection is established, set in tcpConnect() */ struct espconn *activeConnection; /**< A pointer to the espconn structure containing details of the TCP connection */ + void *userData; /**< Used to pass data to the PUBLISH function */ // Add pointers to user callback functions + void (*publish_cb)(void *arg); /**< Pointer to user callback function for publish */ + void (*connack_cb)(void *arg); /**< Pointer to user callback function for connack */ } mqtt_session_t; /** @@ -90,9 +93,9 @@ typedef struct { * @param arg A pointer to void, so it can be called from a timer or task * @return 0 on success */ -LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg); -LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg); -LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err); +uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg); +void ICACHE_FLASH_ATTR disconnected_callback(void *arg); +void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err); /** * A callback function which is called when TCP connection is successful. @@ -101,7 +104,7 @@ LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err); * * This function is called once the TCP connection to the server is completed. This allows us to register the callbacks for received and sent data, as well as enable TCP keepalive and set validConnection in mqtt_session_t to 1 to show the connection has been successful. */ -LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg); +void ICACHE_FLASH_ATTR connected_callback(void *arg); /** * A callback function that deals with received data. @@ -112,9 +115,9 @@ LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg); * * At this point the function does little more than detect what type of MQTT message is sent, and prints out debug info to the serial port. The next few versions should improve this so the user can register their own callback functions which will be called when the particular message type is receieved. */ -LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len); -LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg); -LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg); +void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len); +void ICACHE_FLASH_ATTR data_sent_callback(void *arg); +void ICACHE_FLASH_ATTR pingAlive(void *arg); /** * A function which takes a uint32_t and returns a pointer to a properly formatted MQTT length. @@ -125,7 +128,7 @@ LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg); * The MQTT standard uses a very strange method of encoding the lengths of the various sections * of the packets. This makes it simpler to implement in code. */ -LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength); +uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength); /** * This function handles all the sending of various MQTT messages. @@ -135,4 +138,4 @@ LOCAL 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 */ -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); diff --git a/onewire.c b/onewire.c index 85ae1d5..ce22697 100644 --- a/onewire.c +++ b/onewire.c @@ -1,38 +1,105 @@ #include +#include "os_type.h" +#include "osapi.h" +#include "user_interface.h" #include "gpio.h" -#include "hw_timer.c" #include "onewire.h" -LOCAL void timerISR(void) { - return; -} +#define OWBUS BIT5 -LOCAL void reset(oneWire_t *owDev) { +sint8 ICACHE_FLASH_ATTR reset(void) { + uint32_t time; // reset the ow line, check for presence? + time = system_get_time() + 500; + gpio_output_set(0, OWBUS, OWBUS, 0); // pull GPIO4 low + while(system_get_time() < time); // delay 500uS + gpio_output_set(0, 0, 0, OWBUS); // let go of GPIO4 + time = system_get_time() + 60; + while(system_get_time() < time); + uint8_t presence = (uint8_t)(gpio_input_get() >> 5) & 0x1; + // give a 480uS pause so the next onewire event doesn't get trampled + time = system_get_time() + 480; + while(system_get_time() < time); + + if(!presence) { + return 1; + } else { + return -1; + } } -LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) { +void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) { + uint32_t time; + uint8_t sendBit; + uint8_t inBit = 0x00; + uint8_t inByte = 0x00; switch(cmd) { - case READ_ROM: - break; - case MATCH_ROM: - break; - case SEARCH_ROM: - break; - case ALARM_SEARCH: - break; + os_printf("\nOnewire command: %d\n", cmd); case SKIP_ROM: - break; case CONVERT_T: + for(uint8_t i = 0; i < 8; i++) { + sendBit = (cmd >> i) & 0x1; + //os_printf("\nThe bit is: %d \t i is %d\n", sendBit, i); + if(sendBit == 1) { + //os_printf("\nWe are in sendBit == 1\n"); + time = system_get_time() + 10; + gpio_output_set(0, OWBUS, OWBUS, 0); // pull low + while(system_get_time() < time); + gpio_output_set(0, 0, 0, OWBUS); // let go + time = system_get_time() + 50; + while(system_get_time() < time); + } else { + //os_printf("\nWe are in the sendBit else\n"); + time = system_get_time() + 100; + gpio_output_set(0, OWBUS, OWBUS, 0); //pull low + while(system_get_time() < time); + gpio_output_set(0, 0, 0, OWBUS); // let go + time = system_get_time() + 10; + while(system_get_time() < time); + } + } break; case SCRATCH_READ: + for(uint8_t i = 0; i < 8; i++) { + sendBit = (cmd >> i) & 0x1; + if(sendBit == 1) { + //os_printf("\nWe are in sendBit == 1\n"); + time = system_get_time() + 10; + gpio_output_set(0, OWBUS, OWBUS, 0); // pull low + while(system_get_time() < time); + gpio_output_set(0, 0, 0, OWBUS); // let go + time = system_get_time() + 50; + while(system_get_time() < time); + } else { + //os_printf("\nWe are in the sendBit else\n"); + time = system_get_time() + 100; + gpio_output_set(0, OWBUS, OWBUS, 0); //pull low + while(system_get_time() < time); + gpio_output_set(0, 0, 0, OWBUS); // let go + time = system_get_time() + 10; + while(system_get_time() < time); + } + } + // now read the scratchpad + for(uint8_t i = 0; i < 2; i++) { + for(uint8_t j = 0; j < 8; j++) { + time = system_get_time() + 8; + gpio_output_set(0, OWBUS, OWBUS, 0); + while(system_get_time() < time); + gpio_output_set(0, 0, 0, OWBUS); + time = system_get_time() + 15; + while(system_get_time() < time); + inBit = (uint8_t)(gpio_input_get() >> 5) & 0x01; + inByte |= inBit << j; + time = system_get_time() + 45; + while(system_get_time() < time); + } + owDev->scratchpad[i] = inByte; + inByte = 0; // clear inByte + } break; - case SCRATCH_WRITE: - break; - case SCRATCH_COPY: - break; - case E2_RECALL: + default: + os_printf("\nIncorrect command\n"); break; } - } diff --git a/onewire.h b/onewire.h index 2e4c474..9993365 100644 --- a/onewire.h +++ b/onewire.h @@ -1,5 +1,6 @@ #include -#include "hw_timer.c" +#include "os_type.h" +#include "gpio.h" /** * @struct oneWire_t @@ -7,14 +8,15 @@ */ typedef struct { uint8_t address[8]; - uint8_t scratchpad[20]; + uint8_t scratchpad[9]; + float temperature; } oneWire_t; /** * @enum ds18b20_cmds * This enum simply gives easy to read names to all the various one-wire commands that the DS18B20 can accept */ -enum ds18b20_cmds { +typedef enum { READ_ROM = 0x33, MATCH_ROM = 0x55, SEARCH_ROM = 0xF0, @@ -25,9 +27,7 @@ enum ds18b20_cmds { SCRATCH_WRITE = 0x4E, SCRATCH_COPY = 0x48, E2_RECALL = 0xB8 -}; +} ds18b20_cmds; - -LOCAL void timerISR(void); -LOCAL void ICACHE_FLASH_ATTR reset(oneWire_t *owDev); -LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd); +sint8 ICACHE_FLASH_ATTR reset(void); +void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd); diff --git a/strtoarr.py b/strtoarr.py index b038afb..5f3198d 100755 --- a/strtoarr.py +++ b/strtoarr.py @@ -3,7 +3,7 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. - +#!/usr/bin/python3 ## # @file # @brief File to create string arrays @@ -28,5 +28,5 @@ for letter in inString[:-1]: p = ord(inString[-1]) print("{0:#x} ".format(int(p)), end='') -print("};") +print("}; // " + inString) print("static const uint8_t {0}_len = {1};".format(sys.argv[2], len(inString)))