Minimum interval of DS18B20 readings

Hi Guys,

I’m using the default onewire and dallastemperature libraries from spark. I’m wondering whats the fastest interval anyone has been able to retrieve readings from the DS18B20 without crashing the spark core.

My core is stable at 3 second intervals but at 1 second it will crash after 30 seconds. I’m sending the readings through WiFi also.

@Hoalarious, could you post your code (setup & loop) so we can get a better idea of what you are doing?

@Hoalarious, I’ve been fine reading out at the maximum frequency possible at full resolution (once every 750ms). When you say that you are sending the readings through wifi, what method do you mean?

I am using the spark core as a TCP server. In my loop I have a timing interval, set to retrieve and send the temperature information every 3 seconds. I’ve never had any crashes on arduino devices before until I started implementing wifi/tcp.

I can only think that it has to do with either the buffer or timing issues with wifi at the moment. I haven’t gotten around to looking into optimizing that yet. I would have switched to serial connection first to check but I thought someone might know something about the instability of using the spark core as a TCP server.

Sure, and thanks for your interest. I would clean the code up but I need to get some sleep for now. I’ll post this up anyway so you can have a look if you’d like.

#include "FadingRGB.h"

#include "OneWire/OneWire.h"
#include "spark-dallas-temperature/spark-dallas-temperature.h"

TCPServer server = TCPServer(90);
TCPClient client;

Servo myservo;
String read_buffer;
char read_char;
int read_int;
bool connected = false;
bool servoRun = false;
bool sensor_success = false;
int temp;
long flow;
bool DEBUG = false;
long interval = 3000;
long currentMillis;
long servoDelay = 1500;
long previousMillis = 0;
long previousServo = 0;
int servoPIN = D0;

volatile int NbTopsFan;
int Calc;                               
int hallsensor = D2; 

void rpm () 
{ 
 NbTopsFan++; 

} 

#define ONE_WIRE_BUS D3

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;

void printAddress(DeviceAddress deviceAddress);
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    server.print(deviceAddress[i], HEX);
  }
}

FadingRGB fadingRGB(A6,A5,A4);

void setup()
{
    Serial.begin(9600);
    server.begin();
    pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
    attachInterrupt(hallsensor, rpm, RISING); 
    myservo.attach(servoPIN);
    SPARK_WLAN_Loop();
}

void loop()
{
    fadingRGB.RGBfaderloop();
  if (client.connected()) {
        if (connected == false){
            server.println("Server connected");
            connected = true;
            sensor_success = connect_sensor();
        }
        
        currentMillis = millis();
        
        if(currentMillis - previousMillis > interval) {
            if (!sensor_success) sensor_success = connect_sensor();
            sensors.requestTemperatures();
            previousMillis = currentMillis;
            Calc = (NbTopsFan * 60 / 7.5 / (interval / 1000)); //(Pulse frequency x 60) / 7.5Q, = flow rate 
            NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
            interrupts();         //Enables interrupts
            //delay (500);      //Wait 1 second
            //noInterrupts();         //Disable interrupts
            
            //Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
            flow = Calc/60;
            //flow = random(7,16);
            temp = random(25,35);
            server.print("$");
            server.print(flow);
            server.print(", ");
            //server.println(temp);
            server.println(sensors.getTempC(insideThermometer));
        }
        
        if(servoRun) {
            if(currentMillis - previousServo > servoDelay) {
                myservo.detach();
                myservo.attach(servoPIN);
                servoRun = false;
                server.println("Restarting servo.");
            }
        }

        
    while (client.available()) {
        read_int = client.read();
        read_char = read_int;
        if (DEBUG) server.print("char: ");
        if (DEBUG) server.print(read_char); 
        if (DEBUG) server.println(read_int); 
        if (read_int == 10) read_int = 13;
        switch (read_int)
        {
            case 13:
            if (read_buffer.indexOf("@") == 0) {
                if (DEBUG) server.println("Servo command recieved");
                if (DEBUG) server.println(read_buffer); 
                read_buffer.setCharAt(0, ' ');
                read_buffer.trim();
                    if (read_buffer.indexOf("@") == 0) {
                        if (DEBUG) server.println("Override command recieved");
                        writeservo_override();
                    } else {
                        writeservo(read_buffer.toInt());
                    }
            } else if(read_buffer.indexOf("DEBUG") == 0) {
                DEBUG = !DEBUG;
                if (DEBUG) server.println("DEBUG ON");
                else server.println("DEBUG OFF");
            } else {
                if (DEBUG) server.print("reads: ");
                if (DEBUG) server.println(read_buffer); 
            }
            read_buffer = "";
            break;
            default:
            read_buffer += read_char;  
        }
    }
    
  } else {
    client = server.available();
    delay(1000);
  }
}

void writeservo(int angletoservo)
{
    if (angletoservo <= 180 && (angletoservo >= 0)) {
    if (DEBUG) server.print("Writing to servo: ");
    if (DEBUG) server.println(angletoservo);
        myservo.write(angletoservo);
        servoRun = true;
        previousServo = currentMillis;
        server.println(previousServo);
    } else if (DEBUG) server.println("Must be angle between 0 and 180"); 
}

void writeservo_override()
{
    //TO DO:
}

bool connect_sensor()
{
    sensors.begin();
    server.print("Found ");
    server.print(sensors.getDeviceCount(), DEC);
    server.println(" devices.");
    
    server.print("Parasite power is: "); 
    if (sensors.isParasitePowerMode()) Serial.println("ON");
    else server.println("OFF");
    
    if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
    server.print("Device 0 Address: ");
    
    printAddress(insideThermometer);
    server.println();
    
    sensors.setResolution(insideThermometer, 10);
    
    int resolution = sensors.getResolution(insideThermometer);
    server.print("Device 0 Resolution: ");
    server.print(resolution, DEC); 
    server.println();
    
    if (resolution == 0) return false;
    else return true;
}

Commenting out “(sensors.getTempC(insideThermometer)” seems to allow the spark to run indefinitely without problems. Seems it is crashing when getTempC function is being run when no temperature is retrieved. I believe this might be an issue with porting the dallas library to spark.