DS18B20 library Problems with adding into vm

Issue #329 resolved
C WEIB created an issue

Hi John

Recently, I've been trying to add DS18B20 to vm, build and burn everything normally, it also runs normally in MicorBlocks, but it seems that after putting the blocks to read the temperature into the loop, it will occasionally trigger the esp32 to restart or lose connection with the platform, I would like to ask how to solve it? Attached is the code and the block diagram.

#include <OneWire.h>
#include <DallasTemperature.h>

static uint8_t ds18b20Data;
static uint8_t oldpin;
static OneWire oneWire;
static DallasTemperature sensors;

static int __not_in_flash_func(readDS18B20)(int pin)
{
    if (pin != oldpin) {
        oneWire = OneWire(pin);
        sensors = DallasTemperature(&oneWire);
        sensors.begin();
        oldpin = pin;
        char a[100];
        sprintf(a, "Pin:%d ", pin);
        outputString(a);
    }

    sensors.requestTemperatures();
    float temperatureC = sensors.getTempCByIndex(0);

    char s[100];
    sprintf(s, "%.1f ­°C", temperatureC);
    outputString(s);

    ds18b20Data = int(temperatureC);
    return true;
}

static OBJ primReadDS18B20(int argCount, OBJ *args)
{
    if (!isInt(args[0]))
        return fail(needsIntegerError);
    int pin = mapDigitalPinNum(obj2int(args[0]));
    if ((pin < 0) || (pin > pinCount()))
        return falseObj;

    if (!readDS18B20(pin))
        return falseObj;

    OBJ result = int2obj(ds18b20Data);

    return result;
}

Comments (5)

  1. C WEIB reporter

    It will occasionally disconnect from the platform after clicking on the forever block in the picture above, but the chance is very small, if the click is normal it can also run normally all the way through the cycle, the disconnection only occurs in the moment of the click, not in the cycle after the click is disconnected.

  2. John Maloney repo owner

    My guess is that the ESP32 watchdog timer is getting triggered. Try adding a 1 millisecond delay to your MicroBlocks loop.

  3. Log in to comment