Corrupt message after dallas temperature call

Hi,

// setup the library
dallas.begin();
  
Serial1.println("test");

I got “�st” from the Arduino side. The first two characters are corrupt. Dallas is a temperature probing library.

Thanks.

Hey there! Can you share the rest of your code so we can attempt to replicate?

I do not know the library you are using, but try to check that Arduino and photon have the same baud rate on the serial connection after dallas.begin() call. It could also be a problem of charset conversion when transformed from byte to char arduino side. It 'just a hypothesis would need more details.

Okay.

Electron code:

#define LOG_VERBOSE 0
#define LOG_DEBUG 1
#define LOG_INFO 2
#define LOG_WARN 3
#define LOG_ERROR 4
#define LOG_LEVEL LOG_INFO

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"

// This #include statement was automatically added by the Particle IDE.
#include "SparkJson/SparkJson.h"

// This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"

OneWire oneWire(D6 );

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

// Create a variable that will store the temperature value
double temperature = 0.0;
double temperatureF = 0.0;

bool sendLiquidLevel = false;

int dr = D5; 
int dr_echo = A5;

// HTTP related variables

HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

// Timer related variables

int wakeInterval = 100;
int drInterval = 200;
int upSeconds = 10;

int updateConfigInterval = 600;    // todo
long lastUpdateConfigTime = Time.now();

long startTime = Time.now();
long lastTurnOnDRTime = Time.now();

long lastSendTemperatureTime = 0;

long lastSendLiquidLevel = 0;
long sendLiquidLevelInterval = 60;

void getConfigValuesFromCloud() {

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "depthray.zhitis.com";
    request.port = 5000;
    request.path = "/api/timers?deviceId=" + System.deviceID();

    // Get request
    http.get(request, response, headers);
    Serial.print("Application>\tResponse status: ");
    Serial.println(response.status);

    if(response.status != 200)
        return;

    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(response.body);
    
    StaticJsonBuffer<256> jsonBuffer;
    char json[256];
    response.body.toCharArray(json, 256);
    JsonObject& root = jsonBuffer.parseObject(json);


    wakeInterval = root["wake"].as<long>();
    drInterval = root["dr"].as<long>();
    upSeconds  = root["up"].as<long>();
    sendLiquidLevelInterval = root["liquidLevel"].as<long>();
    
    Serial.println("wake: " + String(wakeInterval));
    Serial.println("dr: " + String(drInterval));
    Serial.println("up: " + String(upSeconds));
    Serial.println("sendLiquidLevelInteral: " + String(sendLiquidLevelInterval));
}

void getParametersFromCloud() {

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "depthray.zhitis.com";
    request.port = 5000;
    request.path = "/api/parameters?deviceId=" + System.deviceID();

    // Get request
    http.get(request, response, headers);
    Serial.print("Application>\tResponse status: ");
    Serial.println(response.status);

    if(response.status != 200)
        return;

    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(response.body);
    
    String result = response.body;
    result.replace("\r","");
    result.replace("\n","");
    
    Serial1.println("send parameters");
    //Serial1.println("parameters:" + result);
    
    for(int i=0; i<result.length(); ){
        Serial1.print(result.substring(i, i+30));
        Serial.println(result.substring(i, i+30));
        i = i+30;
        delay(1000);
    }
    Serial1.println(" ");
    Serial1.println(" end ");
    //Serial.println(result);
    
    StaticJsonBuffer<1256> jsonBuffer;
    char json[1256];
    result.toCharArray(json, 1256);
    JsonObject& root = jsonBuffer.parseObject(json);
    
    for (JsonObject::iterator it=root.begin(); it!=root.end(); ++it)
    {
       delay(200);
       Serial.print(String(it->key) + String(":"));
       Serial1.print(String(it->key) + String(":"));
       if(it->value.is<double>()){
           Serial.println(it->value.as<double>());
          Serial1.println(it->value.as<double>());
        }else if(it->value.is<long>()){
           Serial.println(it->value.as<long>());
          Serial1.println(it->value.as<long>());
        }else { 
           Serial1.println(it->value.asString());
           Serial.println(it->value.asString());
        }
        //Serial1.println("\r\n");
        Serial1.flush();
    }

    
    //Serial1.println(result);
    //delay(5000);
    
    //Serial1.flush();
    delay(25000);
}

String getLogLevelString(int level)
{
    switch (level)
    {
      case LOG_VERBOSE:
      return String("VERBOSE");
      case LOG_DEBUG:
      return String("DEBUG");
      case LOG_INFO:
      return String("INFO");
      case LOG_WARN:
      return String("WARN");
      case LOG_ERROR:
      return String("ERROR");
  }
}

void log(int level, char *message)
{
    if (level >= LOG_LEVEL)
        Serial.println(NowTimeString() + " - " + getLogLevelString(level) + " - " + message);
}

void log(int level, String message)
{
    if (level >= LOG_LEVEL)
        Serial.println(NowTimeString() + " - " + getLogLevelString(level) + " - " + message);
}

String NowTimeString()
{
    unsigned long t = Time.now();
    return String(Time.hour(t)) + ":" + Time.minute(t) + ":" + Time.second(t);
}

void getTemperatures()
{
  // Request temperature conversion
  dallas.requestTemperatures();

  // get the temperature in Celcius
  float tempC = dallas.getTempCByIndex(0);
  // convert to double
  temperature = (double)tempC;
  log(LOG_INFO, "Temperature is: " + String(temperature));

  // convert to Fahrenheit
  float tempF = DallasTemperature::toFahrenheit( tempC );
  // convert to double
  temperatureF = (double)tempF;
}

String readStringFromSerial()
{
    // initialize 
    String readString = "";
  
    while (Serial1.available()) // While receiving characters over serial...
    {
        delay(3); // Necessary delay
        char c = Serial1.read(); // Read the character

        if(c == '\r' || c == '\n')
            break;
    
        readString += c; // Add the character to the string
    }

    readString.trim();

    return readString;
}

void setup() 
{
    Serial.begin(115200);
    Serial1.begin(9600);
  
    log(LOG_INFO, "setup begin");
  
    log(LOG_INFO, "setup link");
    pinMode(dr, OUTPUT);
    pinMode(dr_echo, INPUT);
    pinMode(TX, OUTPUT);

    // update config values from cloud
    getConfigValuesFromCloud();
    getParametersFromCloud();
    
    log(LOG_INFO, "register particle variables for temperature");

    // Register a Particle variable here
    Particle.variable("temperature", &temperature, DOUBLE);
    Particle.variable("temperatureF", &temperatureF, DOUBLE);

    // setup the library
    dallas.begin();
  
    Serial1.println("test");
    // send device id to DR
    Serial1.println("deviceID:" + System.deviceID());
    //digitalWrite(TX, LOW);
    //Serial1.end();
  
    //Particle.publish("log", "setup message");
}

void loop() {
    Serial1.begin(9600);
    //digitalWrite(TX, LOW);
    
    if(Time.now() - lastUpdateConfigTime > updateConfigInterval) {
        getConfigValuesFromCloud();
        lastUpdateConfigTime = Time.now();
    }
  
    if(Time.now() - lastSendTemperatureTime > wakeInterval) {
        getTemperatures();
        Particle.publish("temperature", "{\"deviceId\":\"" + System.deviceID() + "\", \"temperature\":" + String(temperature) + "}");
        
        lastSendTemperatureTime = Time.now();
    }
    
    log(LOG_INFO, "loop begin");
    log(LOG_INFO, String(lastTurnOnDRTime));
    log(LOG_INFO, String(Time.now() - lastTurnOnDRTime));
  
    if(Time.now() - lastTurnOnDRTime > drInterval ) {
        log(LOG_INFO, "turn on DR");
        digitalWrite(dr, HIGH);
        lastTurnOnDRTime = Time.now();

        delay(2000);
    }

    log(LOG_INFO, "try to read from dr_echo");
    if(digitalRead(dr_echo)) 
        digitalWrite(dr, LOW);

    log(LOG_INFO, "try to read strings from DR");
    String readString = readStringFromSerial();
    log(LOG_INFO,"received from DR:" + readString);
    //Particle.publish("log", readString);

    // if the received string is a valid data message from DR
    if(readString.charAt(0) == '{' && readString.charAt(readString.length()-1) == '}' && (Time.now()-lastSendLiquidLevel) > sendLiquidLevelInterval) {
        Particle.publish("liquidLevel", readString.substring(0, readString.length()-1) + ", \"deviceId\":\"" + System.deviceID() + "\"}");
        sendLiquidLevel = true;
        lastSendLiquidLevel = Time.now();
    }    

    log(LOG_INFO, "check if need to sleep");

    if(readString.indexOf("shutdown") >= 0 || sendLiquidLevel || (Time.now() - startTime)  > upSeconds ){
        log(LOG_INFO, "sleep now");
        digitalWrite(dr, LOW);
        digitalWrite(TX, LOW);
        Serial1.end();
        
        System.sleep(dr_echo, RISING, wakeInterval, SLEEP_NETWORK_STANDBY);  
        Serial1.begin(9600);
        //digitalWrite(TX, LOW);
        sendLiquidLevel = false;
        startTime = Time.now();
    }

    delay(500);

    log(LOG_INFO, "loop end");
}

Yes, they have the same baud rate of 9600.

Have you tried a simpler scenario? only to manage the serial communication.

For example:

void setup()
{
 
  Serial1.begin(9600);
  

}

void loop() {

  Serial1.println("TEST");
  delay(1000);

}

what kind of Arduino are using? For example if you are using Arduino UNO, you probably need a logic level converter for a correct communication.