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:
A.M. Rowsell 2019-01-21 19:53:42 -05:00
commit 5e3be0f561
Signed by: amr
GPG key ID: 0B6E2D8375CF79A9
7 changed files with 231 additions and 75 deletions

View file

@ -19,10 +19,10 @@ $(P): $(OBJ)
%.o: %.c $(DEPS) %.o: %.c $(DEPS)
flash: $(P)-0x00000.bin 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: clean:
rm -f $(P) $(OBJ) $(P)-0x00000.bin $(P)-0x10000.bin rm -f $(P) $(OBJ) $(P)-0x00000.bin $(P)-0x10000.bin *~
library: library:
$(CC) -c -fPIC $(CFLAGS) $(P).c -o $(P).o $(CC) -c -fPIC $(CFLAGS) $(P).c -o $(P).o

99
main.c
View file

@ -9,37 +9,54 @@
#include "onewire.h" #include "onewire.h"
#include "main.h" #include "main.h"
static os_timer_t oneTimer; os_timer_t tcpTimer;
static os_timer_t testTimer; os_timer_t pingTimer;
os_timer_t pubTimer;
LOCAL void ICACHE_FLASH_ATTR con(void *arg) { LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
os_printf("Entered con!\n"); os_printf("Entered con!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg; mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT); mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_CONNECT);
os_timer_disarm(&oneTimer); os_timer_disarm(&pingTimer);
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg); os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
os_timer_arm(&oneTimer, 200, 0); os_timer_arm(&pingTimer, 1000, 0);
} }
LOCAL void ICACHE_FLASH_ATTR pub(void *arg) { LOCAL void ICACHE_FLASH_ATTR pubuint(void *arg) {
os_printf("Entered pub!\n"); os_printf("Entered pubuint!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg; mqtt_session_t *pSession = (mqtt_session_t *)arg;
uint8_t data[] = "2.718281828459045"; uint8_t *data = (uint8_t *)(pSession->userData);
int32_t dataLen = os_strlen(data); char *dataStr = os_zalloc(20 * sizeof(char));
mqtt_send(pSession, &data[0], dataLen, MQTT_MSG_TYPE_PUBLISH); intToStr(*data, dataStr, 4);
os_timer_disarm(&oneTimer); int32_t dataLen = os_strlen(dataStr);
os_timer_setfn(&oneTimer, (os_timer_func_t *)ping, arg); mqtt_send(pSession, (uint8_t *)dataStr, dataLen, MQTT_MSG_TYPE_PUBLISH);
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 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) { LOCAL void ICACHE_FLASH_ATTR ping(void *arg) {
os_printf("Entered ping!\n"); os_printf("Entered ping!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg; mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ); mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
os_timer_disarm(&oneTimer); os_timer_disarm(&pingTimer);
os_timer_setfn(&oneTimer, (os_timer_func_t *)sub, arg); os_timer_setfn(&pingTimer, (os_timer_func_t *)ping, arg);
os_timer_arm(&oneTimer, 200, 0); os_timer_arm(&pingTimer, 1000, 0);
} }
LOCAL void ICACHE_FLASH_ATTR sub(void *arg) { LOCAL void ICACHE_FLASH_ATTR sub(void *arg) {
@ -53,33 +70,59 @@ LOCAL void ICACHE_FLASH_ATTR discon(void *arg) {
os_printf("Entered discon!\n"); os_printf("Entered discon!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg; mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_DISCONNECT); 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() { void ICACHE_FLASH_ATTR user_init() {
//uint8_t wifiStatus; //uint8_t wifiStatus;
LOCAL mqtt_session_t globalSession; LOCAL mqtt_session_t globalSession;
LOCAL mqtt_session_t *pGlobalSession = &globalSession; LOCAL mqtt_session_t *pGlobalSession = &globalSession;
char ssid[32] = "yourwifissid"; char ssid[32] = "Kwangmyong";
char passkey[64] = "yourwifipassword"; char passkey[64] = "vqmfg55020";
struct station_config stationConf; struct station_config stationConf;
gpio_init(); // init gpio so we can use the LED 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 wifi_status_led_install(0, PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); // set GPIO0 as status LED
stationConf.bssid_set = 0; stationConf.bssid_set = 0;
os_memcpy(&stationConf.ssid, ssid, 32); // copy the ssid and passkey into the station_config struct os_memcpy(&stationConf.ssid, ssid, 32); // copy the ssid and passkey into the station_config struct
os_memcpy(&stationConf.password, passkey, 64); os_memcpy(&stationConf.password, passkey, 64);
wifi_set_opmode_current(0x01); //station mode wifi_set_opmode_current(0x01); //station mode
wifi_station_set_config_current(&stationConf); // tell it about our config, this auto-connects us as well wifi_station_set_config_current(&stationConf); // tell it about our config, this auto-connects us as well
// prepare the TCP/MQTT connection stuff // prepare the TCP/MQTT connection stuff
// Adafruit IO is at 52.5.238.97 // Adafruit IO is at 52.5.238.97
static const char ioUser[11] = { 0x4d, 0x72, 0x41, 0x75, 0x72, 0x65, 0x6c, 0x69, 0x75, 0x73, 0x52 }; // MrAureliusR 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 uint8_t ioUser_len = 11;
static const char ioKey[32] = { 0x32, 0x63, 0x39, 0x36, 0x65, 0x30, 0x35, 0x61, 0x66, 0x34, 0x35, 0x30, 0x34, 0x31, 0x66, 0x31, 0x38, 0x36, 0x65, 0x62, 0x39, 0x30, 0x33, 0x32, 0x33, 0x31, 0x31, 0x31, 0x61, 0x32, 0x61, 0x38 }; // 2c96e05af45041f186eb90323111a2a8 static const char ioKey[32] = { 0x32, 0x63, 0x39, 0x36, 0x65, 0x30, 0x35, 0x61, 0x66, 0x34, 0x35, 0x30, 0x34, 0x31, 0x66, 0x31, 0x38, 0x36, 0x65, 0x62, 0x39, 0x30, 0x33, 0x32, 0x33, 0x31, 0x31, 0x31, 0x61, 0x32, 0x61, 0x38 }; // 2c96e05af45041f186eb90323111a2a8
static const uint8_t ioKey_len = 32; static const uint8_t ioKey_len = 32;
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 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 = 37; static const uint8_t ioTopic_len = 27;
static const char clientID[5] = { 0x46, 0x5a, 0x5a, 0x4e, 0x30 }; /* 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; static const uint8_t clientID_len = 5;
pGlobalSession->port = 1883; // mqtt port pGlobalSession->port = 1883; // mqtt port
static const char adafruitIO_ip[4] = {52, 5, 238, 97}; static const char adafruitIO_ip[4] = {52, 5, 238, 97};
@ -97,8 +140,10 @@ void ICACHE_FLASH_ATTR user_init() {
pGlobalSession->client_id = os_zalloc(sizeof(uint8_t) * pGlobalSession->client_id_len); pGlobalSession->client_id = os_zalloc(sizeof(uint8_t) * pGlobalSession->client_id_len);
os_memcpy(pGlobalSession->client_id, clientID, 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_setfn(&tcpTimer, (os_timer_func_t *)tcpConnect, pGlobalSession);
os_timer_arm(&oneTimer, 15000, 0); os_timer_arm(&tcpTimer, 12000, 0);
os_timer_setfn(&testTimer, (os_timer_func_t *)con, pGlobalSession); os_timer_setfn(&pingTimer, (os_timer_func_t *)con, pGlobalSession);
os_timer_arm(&testTimer, 16000, 0); os_timer_arm(&pingTimer, 16000, 0);
os_timer_setfn(&pubTimer, (os_timer_func_t *)dataLog, pGlobalSession);
os_timer_arm(&pubTimer, 20000, 1);
} }

11
main.h
View file

@ -1,7 +1,16 @@
#include "os_type.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 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 sub(void *arg);
LOCAL void ICACHE_FLASH_ATTR ping(void *arg); LOCAL void ICACHE_FLASH_ATTR ping(void *arg);
LOCAL void ICACHE_FLASH_ATTR discon(void *arg); LOCAL void ICACHE_FLASH_ATTR discon(void *arg);

68
mqtt.c
View file

@ -3,6 +3,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <stdint.h> #include <stdint.h>
#include <math.h>
#include "user_interface.h" #include "user_interface.h"
#include "ets_sys.h" #include "ets_sys.h"
#include "osapi.h" #include "osapi.h"
@ -12,6 +13,7 @@
#include "os_type.h" #include "os_type.h"
#include "mqtt.h" #include "mqtt.h"
#include "onewire.h" #include "onewire.h"
#include "main.h"
/* Functions we will need to implement: /* Functions we will need to implement:
* Send -- will handle all sending of all packets * Send -- will handle all sending of all packets
@ -25,6 +27,67 @@
static os_timer_t MQTT_KeepAliveTimer; static os_timer_t MQTT_KeepAliveTimer;
static os_timer_t waitForWifiTimer; 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) { void ICACHE_FLASH_ATTR data_sent_callback(void *arg) {
os_printf("Data sent!\n"); 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_PINGREQ:
case MQTT_MSG_TYPE_DISCONNECT: case MQTT_MSG_TYPE_DISCONNECT:
default: default:
if(msgType == MQTT_MSG_TYPE_DISCONNECT) {
os_printf("MQTT Disconnect packet\n");
}
return; return;
break; break;
@ -124,6 +190,8 @@ void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
void ICACHE_FLASH_ATTR disconnected_callback(void *arg) { void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
os_printf("Disconnected\n"); os_printf("Disconnected\n");
os_timer_disarm(&pubTimer);
os_timer_disarm(&pingTimer);
} }
uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) { uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {

1
mqtt.h
View file

@ -82,6 +82,7 @@ typedef struct {
uint32_t password_len; /**< The length of the password string */ 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() */ 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 */ 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 // Add pointers to user callback functions
void (*publish_cb)(void *arg); /**< Pointer to user callback function for publish */ void (*publish_cb)(void *arg); /**< Pointer to user callback function for publish */
void (*connack_cb)(void *arg); /**< Pointer to user callback function for connack */ void (*connack_cb)(void *arg); /**< Pointer to user callback function for connack */

120
onewire.c
View file

@ -5,17 +5,23 @@
#include "gpio.h" #include "gpio.h"
#include "onewire.h" #include "onewire.h"
sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev) { #define OWBUS BIT5
sint8 ICACHE_FLASH_ATTR reset(void) {
uint32_t time; uint32_t time;
// reset the ow line, check for presence? // reset the ow line, check for presence?
time = system_get_time() + 500; time = system_get_time() + 500;
gpio_output_set(0, BIT4, BIT4, 0); // pull GPIO4 low gpio_output_set(0, OWBUS, OWBUS, 0); // pull GPIO4 low
while(system_get_time() < time); // delay 500uS while(system_get_time() < time); // delay 500uS
gpio_output_set(0, 0, 0, BIT4); // let go of GPIO4 gpio_output_set(0, 0, 0, OWBUS); // let go of GPIO4
time = system_get_time() + 60; time = system_get_time() + 60;
while(system_get_time() < time); while(system_get_time() < time);
uint8_t presence = (uint8_t)(gpio_input_get() >> 4) & 0x1; uint8_t presence = (uint8_t)(gpio_input_get() >> 5) & 0x1;
if(presence) { // 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; return 1;
} else { } else {
return -1; return -1;
@ -27,47 +33,73 @@ void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) {
uint8_t sendBit; uint8_t sendBit;
uint8_t inBit = 0x00; uint8_t inBit = 0x00;
uint8_t inByte = 0x00; uint8_t inByte = 0x00;
if((cmd == SKIP_ROM) | (cmd == CONVERT_T)) { switch(cmd) {
for(uint8_t i = 7; i >= 0; i--) { os_printf("\nOnewire command: %d\n", cmd);
sendBit = (cmd >> i) & 0x1; case SKIP_ROM:
if(sendBit == 1) { case CONVERT_T:
time = system_get_time() + 10; for(uint8_t i = 0; i < 8; i++) {
gpio_output_set(0, BIT4, BIT4, 0); // pull low sendBit = (cmd >> i) & 0x1;
while(system_get_time() < time); //os_printf("\nThe bit is: %d \t i is %d\n", sendBit, i);
gpio_output_set(0, 0, 0, BIT4); // let go if(sendBit == 1) {
} else { //os_printf("\nWe are in sendBit == 1\n");
time = system_get_time() + 100; time = system_get_time() + 10;
gpio_output_set(0, BIT4, BIT4, 0); //pull low gpio_output_set(0, OWBUS, OWBUS, 0); // pull low
while(system_get_time() < time); while(system_get_time() < time);
gpio_output_set(0, 0, 0, BIT4); 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;
} else if(cmd == SCRATCH_READ) { case SCRATCH_READ:
for(uint8_t i = 7; i >= 0; i--) { for(uint8_t i = 0; i < 8; i++) {
sendBit = (cmd >> i) & 0x1; sendBit = (cmd >> i) & 0x1;
if(sendBit == 1) { if(sendBit == 1) {
time = system_get_time() + 10; //os_printf("\nWe are in sendBit == 1\n");
gpio_output_set(0, BIT4, BIT4, 0); // pull low time = system_get_time() + 10;
while(system_get_time() < time); gpio_output_set(0, OWBUS, OWBUS, 0); // pull low
gpio_output_set(0, 0, 0, BIT4); // let go while(system_get_time() < time);
} else { gpio_output_set(0, 0, 0, OWBUS); // let go
time = system_get_time() + 100; time = system_get_time() + 50;
gpio_output_set(0, BIT4, BIT4, 0); //pull low while(system_get_time() < time);
while(system_get_time() < time); } else {
gpio_output_set(0, 0, 0, BIT4); //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
// now read the scratchpad for(uint8_t i = 0; i < 2; i++) {
for(uint8_t i = 0; i < 2; i++) { for(uint8_t j = 0; j < 8; j++) {
for(uint8_t j = 0; j < 8; j++) { time = system_get_time() + 8;
time = system_get_time() + 8; gpio_output_set(0, OWBUS, OWBUS, 0);
gpio_output_set(0, BIT4, BIT4, 0); while(system_get_time() < time);
while(system_get_time() < time); gpio_output_set(0, 0, 0, OWBUS);
gpio_output_set(0, 0, 0, BIT4); time = system_get_time() + 15;
inBit = (uint8_t)(gpio_input_get() >> 4) & 0x01; while(system_get_time() < time);
inByte |= inBit << j; 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
} }
owDev->scratchpad[i] = inByte; break;
} default:
os_printf("\nIncorrect command\n");
break;
} }
} }

View file

@ -9,6 +9,7 @@
typedef struct { typedef struct {
uint8_t address[8]; uint8_t address[8];
uint8_t scratchpad[9]; uint8_t scratchpad[9];
float temperature;
} oneWire_t; } oneWire_t;
/** /**
@ -28,5 +29,5 @@ typedef enum {
E2_RECALL = 0xB8 E2_RECALL = 0xB8
} ds18b20_cmds; } ds18b20_cmds;
sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev); sint8 ICACHE_FLASH_ATTR reset(void);
void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd); void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd);