OneWIre and Serial2 side by side

Hi there,

hav tested it with an GPS-6502 and Dev-Board-Plus.

Have working 4 DS18B20 togehter, they schow up, it works.

But now i add Serial-Communication to RX2/TX2, witch works fine, but OneWire dosn’t work any more… Raw Output from OneWire is -1 for all four sensors.

Have tested to Stop Serial1 with Serial1.end(); and request then DS18B20, but it still dosn’t work.

When I put request DS18B20 befor the Serial1 Block, it works for first time but not more after the Serial Communication. Serial works well, but OneWire dosen’t work anymore. When I Disconnect Serial, I have to reset and OneWire works again, as long as I dont add connection to Serial1.
Have tested it with multiple GPIOs for OneWire Data.

One Wire Sensors is Powered by VDD 3,3V and between VDD and GPIO 4k7 Ohm. MEsured between VDD and GND = 3,3V, mesured between GND and GPIO, see some Break when OneWIre is communicatin, but not when Serial is connected, it stays at 3,3V

Maybee an interrupt in Serial Communication after Serial1.end()??? Is there a Way to deaktivate Interrupts Global? CLI(); or SEI(); sems not to work.

#include "LoRaWanMinimal_APP.h"
#include "Arduino.h"
#include "OneWire.h"
#include <Wire.h>  
#include "HT_SSD1306Wire.h"

#define R_Luefter GPIO1
#define R_Hauptschalter GPIO2
#define R_Inverter GPIO3
#define R_Zusatz GPIO4

OneWire DS1820(GPIO7);

SSD1306Wire  display(0x3c, 500000, SDA, SCL, GEOMETRY_128_64, GPIO10); // addr , freq , i2c group , resolution , rst

uint8_t ifDisplayAck=0;
uint8_t isDispayOn=0;
/*
 * set LoraWan_RGB to Active,the RGB active in loraWan
 * RGB red means sending;
 * RGB purple means joined done;
 * RGB blue means RxWindow1;
 * RGB yellow means RxWindow2;
 * RGB green means received done;
 */

//Set these OTAA parameters to match your app/node in TTN


//ABP Parameters


uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };

static uint8_t counter=0;


static uint8_t mydata[11] = { 0x03, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

///////////////////////////////////////////////////
//Some utilities for going into low power mode
TimerEvent_t sleepTimer;
//Records whether our sleep/low power timer expired
bool sleepTimerExpired;

uint32_t SendTime;

//              Battery pre Setting

// Definition von Sensor-Adressen und -Namen
byte Sensor1[8] = { 0x28, 0x0A, 0x0B, 0x09, 0x06, 0x00, 0x00, 0xE3 };  //28-000006090b0a
char Name1[] = "Input: ";
byte Sensor2[8] = { 0x28, 0xF7, 0x4A, 0x08, 0x06, 0x00, 0x00, 0x70 };  //28-000006084af7
char Name2[] = "Victron_lader: ";
byte Sensor3[8] = { 0x28, 0xA1, 0x97, 0x08, 0x06, 0x00, 0x00, 0x5E };  //28-0000060897a1
char Name3[] = "Wechselrichter: ";
byte Sensor4[8] = { 0x28, 0x44, 0xA3, 0x0A, 0x06, 0x00, 0x00, 0x00 };  //28-0000060aa344
char Name4[] = "Solarladeregler: ";                                     

float t_input;
float t_lader;
float t_inverter;
float t_solar;

const int reveivingtime = 10000;  // 1 Sekunde

#define SERIAL_1_TIMEOUT 10000

const int numBytes = 256;
byte receivedBytes_main[numBytes];

static int ndx = 0;
int numReceived = 0;
boolean receivingmode = false;
unsigned long reveivingtimer = 0;


byte ReadAll[21] = { 0x4E, 0x57, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x01, 0x29 };  // read all

bool newData = false;
bool Empfangsdatenchecker = false;
bool datavalid = false;


float totalvoltage = 0;
float CurrentData = 0;
byte Remaining_Battery_Cap = 0;
int Number_of_Battery_life_Cycle = 0;
int Total_Battery_cycle_Capacity = 0;
int Battery_Warning_Massage = 0;
bool charge = false;
bool discharge = false;
bool balance = false;

long Battery_Capacity_Setting = 0;
bool dedicated_Charger_Switch = false;

///////////////////////////////////////////////////
int32_t fracPart(double val, int n)
{
  return (int32_t)((val - (int32_t)(val))*pow(10,n));
}

float getTemperature(bool type_s, byte address[8])
  {
  Serial.println("frage ab");
  // Temperaturwert des adressieren Sensors auslesen
  // Dabei wird zwischen DS18S20 udn den anderen unterschieden
  byte data[12];
  int16_t raw;
  byte i;
  
  DS1820.reset();
  DS1820.select(address);
  DS1820.write(0x44, 0);        // Start Messung, parasitaere Versorgung aus
  delay(1000);
  Serial.println("Messung gestartet");                  // eventuell reichen auch 750 ms
  DS1820.reset();
  DS1820.select(address);    
  DS1820.write(0xBE);           // Read Scratchpad
  for ( i = 0; i < 9; i++)
    { data[i] = DS1820.read(); }
  raw = (data[1] << 8) | data[0];
  //Serial.println(address);
  Serial.println(raw);
  if (type_s) 
    {
    raw = raw << 3;
    if (data[7] == 0x10) 
      { raw = (raw & 0xFFF0) + 12 - data[6]; }
    }
  else
    {
    byte cfg = (data[4] & 0x60);
    // Aufloesung bestimmen, bei niedrigerer Aufloesung sind
    // die niederwertigen Bits undefinier -> auf 0 setzen
    if (cfg == 0x00) raw = raw & ~7;      //  9 Bit Aufloesung,  93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 Bit Aufloesung, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 Bit Aufloesung, 375.0 ms
    // Default ist 12 Bit Aufloesung, 750 ms Wandlungszeit
    }
  return ((float)raw / 16.0);
}

static void wakeUp()
{
  sleepTimerExpired=true;
}

static void lowPowerSleep(uint32_t sleeptime)
{
  sleepTimerExpired=false;
  TimerInit( &sleepTimer, &wakeUp );
  TimerSetValue( &sleepTimer, sleeptime );
  TimerStart( &sleepTimer );
  //Low power handler also gets interrupted by other timers
  //So wait until our timer had expired
  while (!sleepTimerExpired) lowPowerHandler();
  TimerStop( &sleepTimer );
}

bool Data_Check() {
  bool datavalid;
  if (receivedBytes_main[0] == 0x4E && receivedBytes_main[1] == 0x57) {
    datavalid = true;
    //Serial.println("erste zeichen ok");
  } else {
    //Serial.println("Error erste zeichen");
    datavalid = false;
    showNewData();
  }

  if (datavalid) {
    int datenlanege = (int)receivedBytes_main[2] << 8 | receivedBytes_main[3];
    if ((datenlanege + 2) == numReceived) {
      //Serial.println("länge ist ok");
      datavalid = true;
    } else {
      //Serial.println("Error Längen Prüfung");
      datavalid = false;
    }
  }
  if (datavalid) {
    return true;
  } else {
    return false;
  }
}

void runCode() {
  //digitalWrite(R_Zusatz, LOW);
  Serial.println("Seriell Daten abfragen");
  //Daten Anfragen
  Serial1.begin(115200, SERIAL_8N1);
  
  Serial1.write(ReadAll, sizeof(ReadAll)); 
  
  numReceived =  Serial1.read(receivedBytes_main,SERIAL_1_TIMEOUT);
  
  if (numReceived != 0) {
    Serial.print("es wurden ");
    Serial.print(numReceived);
    Serial.println(" byte empfangen");
    newData = true;
  } else {
    Serial.println("es wurden 0 Bytes empfangen");    
  }
  
  if (newData && Data_Check()) {

    DatenAnalyse();

    //Array clear und rücksetzen
    memset(receivedBytes_main, 0, sizeof(receivedBytes_main));
    newData = false;
  } else {

    //Serial.println("setzte Array zurück");
    //Array clear und rücksetzen
    memset(receivedBytes_main, 0, sizeof(receivedBytes_main));
    newData = false;
  }
  
  //Serial1.flush();
  Serial1.end();
  delay(1000);
  VextON();

    //ds1820 auslesen
  Serial.println("Temperaturen abfragen:");
  t_input = getTemperature(false, Sensor1);
  t_inverter = getTemperature(false, Sensor3);
  t_lader = getTemperature(false, Sensor2);
  t_solar = getTemperature(false, Sensor4);
  
    if((t_lader > (t_input +5)) or (t_inverter > (t_input +10)) or (t_solar > (t_input +5)))
  {
    digitalWrite(R_Luefter, LOW);
  }
  else if ((t_lader < (t_input + 2)) and (t_inverter < (t_input +5)) or (t_solar < (t_input +2)))
  {
    digitalWrite(R_Luefter, HIGH);
  }

  if (Remaining_Battery_Cap > 10) {
    digitalWrite(R_Hauptschalter, LOW);
  } else {
    digitalWrite(R_Hauptschalter, HIGH);
  }

  if (Remaining_Battery_Cap > 30) {
    digitalWrite(R_Inverter, LOW);
  } else {
    digitalWrite(R_Inverter, HIGH);
  }

  digitalWrite(R_Zusatz, HIGH);

  

  delay(100);

  displayGPSInof();

        
}

void showNewData() {
  if (newData == true) {
    Serial.print("This just in (HEX values)... ");
    if (numReceived < 26) {
      for (int n = 0; n < numReceived; n++) {
        Serial.print(receivedBytes_main[n], HEX);
        Serial.print(' ');
      }
    }
    if (numReceived >= 26) {
      for (int n = 0; n < 26; n++) {
        Serial.print(receivedBytes_main[n], HEX);
        Serial.print(' ');
      }
    }
    Serial.println();
    newData = false;
  }
}

void DatenAnalyse() {
  //Serial.println("Datenanalyse: ");

  String Command = "";
  String transfertype = "";


  // Command ermitteln
  switch (receivedBytes_main[8]) {
    case 2:
      Command = "Write";
      break;
    case 3:
      Command = "Read";
      break;
    case 5:
      Command = "Command to Password";
      break;
    case 6:
      Command = "Read All Data";
      break;
    default:
      Command = "Not defined!";
      break;  // Wird nicht benötigt, wenn Statement(s) vorhanden sind
  }
  //Serial.print("Command = ");
  //Serial.println(Command);

  //Transfer Type
  switch (receivedBytes_main[10]) {
    case 0:
      transfertype = "Read Data";
      break;
    case 1:
      transfertype = "Answer Frame";
      break;
    case 2:
      transfertype = "BMS Active upload";
      break;
    default:
      transfertype = "Not defined!";
      break;  // Wird nicht benötigt, wenn Statement(s) vorhanden sind
  }
  //Serial.print("Transfer-Type = ");
  //Serial.println(transfertype);

  //showNewData();

  // ist es Read all oder nur eine einzelne abfrage...
  // einzelabfrage:
  if (receivedBytes_main[10] == 0x01 && receivedBytes_main[8] == 0x03) {
    Datencode(11);
  }
  if (receivedBytes_main[10] == 0x01 && receivedBytes_main[8] == 0x06) {
    int zeichen = 0;
    for (int r = 11; r < numReceived; r = r + zeichen) {
      zeichen = Datencode(r);
    }
  }
}


void VextON(void)
{
  pinMode(Vext,OUTPUT);
  digitalWrite(Vext, LOW);
}

void VextOFF(void) //Vext default OFF
{
  pinMode(Vext,OUTPUT);
  digitalWrite(Vext, HIGH);
}



///////////////////////////////////////////////////
void displayGPSInof()
{
  char str[30];
  display.clear();
  display.setFont(ArialMT_Plain_16);
  
  int index = sprintf(str,"SOC: %d",Remaining_Battery_Cap);
  str[index] = 0;
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(0, 0, str);

  display.setFont(ArialMT_Plain_10);
  index = sprintf(str,"I: %d.%dA",(int)CurrentData,fracPart(CurrentData,1));
  str[index] = 0;
  display.drawString(0, 20, str);

  index = sprintf(str,"U: %d.%dV",(int)totalvoltage,fracPart(totalvoltage,2));
  str[index] = 0;
  display.drawString(60, 20, str);
  /*
  index = sprintf(str,"%02d:%02d:%02d",GPS.time.hour(),GPS.time.minute(),GPS.time.second(),GPS.time.centisecond());
  str[index] = 0;
  display.drawString(70, 24, str);
  */
  
  index = sprintf(str,"Input  %d.%d°C",(int)t_input,fracPart(t_input,1));
  str[index] = 0;
  display.drawString(0, 32, str);
   
  index = sprintf(str,"Lader: %d.%d°C",(int)t_lader,fracPart(t_lader,1));
  str[index] = 0;
  display.drawString(70, 32, str); 
 
  index = sprintf(str,"Inv: %d.%d°C",(int)t_inverter,fracPart(t_inverter,1));
  str[index] = 0;
  display.drawString(0, 40, str);   

  index = sprintf(str,"Sol: %d.%d°C",(int)t_solar,fracPart(t_solar,1));
  str[index] = 0;
  display.drawString(70, 40, str); 
  /*
  

  index = sprintf(str,"Sat: %d",(int)GPS.satellites.value());
  str[index] = 0;
  display.drawString(0, 40, str);

  index = sprintf(str,"dist: %d.%dm",(int)Distance,fracPart(Distance,2));
  str[index] = 0;
  display.drawString(70, 48, str);  
  /*
  index = sprintf(str,"alt: %d.%d",(int)Air530.altitude.meters(),fracPart(Air530.altitude.meters(),2));
  str[index] = 0;
  display.drawString(60, 32, str);
  
  index = sprintf(str,"Bat: %d.%dV",(int)(getBatteryVoltage() / 1000.0),fracPart(getBatteryVoltage() / 1000.0,2));
  str[index] = 0;
  display.drawString(0, 16, str);
  
  index = sprintf(str,"Bat: %d%%",(int)((float)BoardGetBatteryLevel_V2()/255*100));
  str[index] = 0;
  display.drawString(0, 48, str);

  display.setFont(ArialMT_Plain_16);
  index = sprintf(str,"%s",DeviceName);
  str[index] = 0;
  display.drawString(0, 0, str);
  */
  display.display();
}



///////////////////////////////////////////////////
void setup() {
	Serial.begin(115200, SERIAL_8N1);
  
  pinMode(R_Luefter ,OUTPUT);
  digitalWrite(R_Luefter, HIGH);

  pinMode(R_Hauptschalter ,OUTPUT);
  digitalWrite(R_Hauptschalter, HIGH);

  pinMode(R_Inverter ,OUTPUT);
  digitalWrite(R_Inverter, HIGH);

  pinMode(R_Zusatz ,OUTPUT);
  digitalWrite(R_Zusatz, HIGH);

  VextON();
  delay(100);

  display.init();
  display.clear();
  display.display();
  display.screenRotate(ANGLE_180_DEGREE);  

  if (ACTIVE_REGION==LORAMAC_REGION_AU915) {
    //TTN uses sub-band 2 in AU915
    LoRaWAN.setSubBand2();
  }
 
  LoRaWAN.begin(LORAWAN_CLASS, ACTIVE_REGION);
  
  //Enable ADR
  LoRaWAN.setAdaptiveDR(true);

  LoRaWAN.joinABP(nwkSKey, appSKey, devAddr);
  /*
  while (1) {
    Serial.print("Joining... ");
    LoRaWAN.joinOTAA(appEui, appKey, devEui);
    if (!LoRaWAN.isJoined()) {
      //In this example we just loop until we're joined, but you could
      //also go and start doing other things and try again later
      Serial.println("JOIN FAILED! Sleeping for 30 seconds");
      lowPowerSleep(30000);
    } else {
      Serial.println("JOINED");
      break;
    }
  }*/
}

///////////////////////////////////////////////////
void loop()
{

  //Counter is just some dummy data we send for the example
  //counter++;
  Serial.println("RunCode");
  runCode();
  //In this demo we use a timer to go into low power mode to kill some time.
  //You might be collecting data or doing something more interesting instead.
  //lowPowerSleep(5000);  
  //Now send the data. The parameters are "data size, data pointer, port, request ack"
  //Here we send confirmed packed (ACK requested) only for the first five (remember there is a fair use policy)
  uint8_t size = sizeof(mydata);
  if (millis()-SendTime > 60000) {
    SendTime = millis();
    if (LoRaWAN.send(size, mydata, 1, false)) {
      Serial.println("Send OK");
    } else {
      Serial.println("Send FAILED");
    }
    delay(100);
  }
  //lowPowerSleep(30000);
}

///////////////////////////////////////////////////
//Example of handling downlink data
void downLinkDataHandle(McpsIndication_t *mcpsIndication)
{
  Serial.printf("Received downlink: %s, RXSIZE %d, PORT %d, DATA: ",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
  for(uint8_t i=0;i<mcpsIndication->BufferSize;i++) {
    Serial.printf("%02X",mcpsIndication->Buffer[i]);
  }
  Serial.println();
}

I also have been seeing issues w/ V3 - and one-wire devices - both in regular KIT32 and Lora boards - I contacted Heltec and they say everything works fine. I noticed operation is fine when running off the built-in lithium battery port.