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>
This commit is contained in:
parent
48702bf328
commit
5e3be0f561
7 changed files with 231 additions and 75 deletions
68
mqtt.c
68
mqtt.c
|
@ -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
|
||||
|
@ -25,6 +27,67 @@
|
|||
static os_timer_t MQTT_KeepAliveTimer;
|
||||
static os_timer_t waitForWifiTimer;
|
||||
|
||||
// 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");
|
||||
}
|
||||
|
@ -98,6 +161,9 @@ void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short
|
|||
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;
|
||||
|
||||
|
@ -124,6 +190,8 @@ void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
|
|||
|
||||
void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
|
||||
os_printf("Disconnected\n");
|
||||
os_timer_disarm(&pubTimer);
|
||||
os_timer_disarm(&pingTimer);
|
||||
}
|
||||
|
||||
uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue