Project Disconnecting, Temp Sensor going to -127

Hi, I have been working on a project. It controls most parts of my aquarium. It has a solenoid valve, pump and 4 float sensors and 1 flood sensor. On the Blynk I have 4 buttons. The 1st button turns on the solenoid valve to drain the aquarium. 2nd button turns on the pump which puts new water in the tank. The 3rd button is automatic mode. It drains the some of the water with the use of the solenoid and then fills new water from another bucket. The flood sensors control how much of the tank needs to be drained and filled. 2 of the sensors
are placed in the bucket with the pump so that the pump stops when the bucket is empty and another sensor to prevent the drain bucket from being over filled. The other 2 are placed where the tank is topped off and the other one below it and it is used to drain to that point. After I finished the code the project kept disconnecting and connecting instantly sometimes when a button is pressed. Even when the Esp turns on in the app it says connected , disconnects and then reconnects again. This all happens within a second. This morning the pump was turned on and I am sure it was off the day before. I have no idea how it magically turned no. Also I have a DS18B20 temp sensor that constantly displays -127 and then the actual Temp. Maybe this is causing the disconnects?? I am so confused​:disappointed_relieved::confused:. It`s quite a big sketch. Ignore void turn_on that just turns the lights on. I am using a Wemos D1 Mini Pro. If you need pictures just ask.

Thanks in Advance

#include <ArduinoOTA.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include "Adafruit_MCP23008.h"
#include <TimeLib.h>
#include <WidgetRTC.h>

#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire (14); //esp
DallasTemperature sensors(&oneWire);

WidgetLCD lcd(V13);

WidgetRTC rtc;

Adafruit_MCP23008 mcp;

#define white 13 //esp
#define fan 12 //esp

#define SUNRISE 9*60 + 0
#define DAWN 10*60 + 0
#define DUSK 16*60 + 0
#define SUNSET 17*60 + 0

#define drain_bucket_full 4 //mcp
#define fill_bucket_empty 6 //mcp
#define aquarium_empty 5 //mcp
#define aquarium_full 1 //mcp

#define Fill_Pump 2 //mcp
#define solenoid 0 //mcp

#define watersensor 3

int sensor = 0;

WidgetLED Pump(V2);
WidgetLED Solenoid_valve(V7);
WidgetLED Done(V8);
WidgetLED Auto(V9);
WidgetLED error(V10);

WidgetLED fanled(V12);
WidgetLED whiteled(V11);

BlynkTimer timer;

boolean emergencystop = 0;

int timer1;
int timer2;
int timer3;
boolean start = 0;

int var;
int state;

char auth[] = "5xxxxxxxxxxxxxxxxxxxxxxxxxxxx1a2";
char ssid[] = "Claxxxxxxxxxxxxxxxxxxxxx-Fi";
char pass[] = "claxxxxxxxxxxxxx3";


BLYNK_WRITE(V0) // At global scope (not inside of the function)

{
  if ( param.asInt() == 1 )
  {
    timer.enable(timer1);
  } else {
    timer.disable(timer1);
    state = 0;
    Done.off();
    var = 4;
    lcd.clear();
  }
}

BLYNK_WRITE(V1) // At global scope (not inside of the function)
{
  if ( param.asInt() == 1 )
  {
    timer.enable(timer2);
  } else {
    timer.disable(timer2);
    state = 0;
    var = 2;
    Done.off();
    lcd.clear();
  }
}

BLYNK_WRITE(V5) // At global scope (not inside of the function)

{
  if ( param.asInt() == 1 )
  {
    timer.enable(timer3);
  }
  else
  {
    timer.disable(timer3);
    Auto.off();
    Done.off();
    var = 5;
    lcd.clear(); //Use it to clear the LCD Widget
    state = 0;

  }
}

BLYNK_WRITE(V6) // Stop Button

{
  if ( param.asInt() == 1 )
  {
    var = 5;
    lcd.clear():

    }
  }

  BLYNK_WRITE(V14) //Debug Purposes

{
  if ( param.asInt() == 1 )
  {
    Blynk.virtualWrite(V15, var);
    Blynk.virtualWrite(V16, state);
  }
}

void water_sensor () {

  if (mcp.digitalRead(watersensor) == 1) {
    error.off();

  }
  else
  {
    var = 5;
    lcd.print(0, 0, "Water on the    "); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "floor          ");
    error.on();
    mcp.digitalWrite(Fill_Pump, LOW);
    Pump.off();
    mcp.digitalWrite(solenoid, LOW);
    Solenoid_valve.off();
    Done.off();
    Auto.off();

  }
}
void solenoid_on() {
  mcp.digitalWrite(solenoid, HIGH);
  Solenoid_valve.on();
}

void solenoid_off() {
  mcp.digitalWrite(solenoid, LOW);
  Solenoid_valve.off();
}
void Pump_on() {
  mcp.digitalWrite(Fill_Pump, HIGH);
  Pump.on();
}
void Pump_off() {
  mcp.digitalWrite(Fill_Pump, LOW);
  Pump.off();
}

void all_off() {
  mcp.digitalWrite(Fill_Pump, LOW);
  Pump.off();
  mcp.digitalWrite(solenoid, LOW);
  Solenoid_valve.off();
  Done.off();
  Auto.off();
}


void fill() {
  if (state == 0 && sensor == 0 && mcp.digitalRead(fill_bucket_empty) == HIGH && mcp.digitalRead(aquarium_full) == LOW)
  {
    var = 3;
    lcd.print(0, 0, "Filling with   "); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "Water          ");
    state = 1;

  }

  if (state == 1 && mcp.digitalRead(fill_bucket_empty) == LOW) {
    lcd.print(0, 0, "Filling Bucket "); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "is Empty       ");
    state = 0;
    var = 4;
  }


  if (state == 1 && mcp.digitalRead(aquarium_full) == HIGH)
  {
    var = 4;
    state = 3;
    lcd.print(0, 0, "Change Done     "); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "               ");
    Done.on();
  }


}




//---------------------------------------------------------------------------------------------------------------------

void drain() {

  //Drain
  if (state == 0 && mcp.digitalRead(drain_bucket_full) == LOW && mcp.digitalRead(aquarium_empty) == HIGH) {
    var = 1;
    lcd.print(0, 0, "Draining mode "); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "Draining Water");
    state = 1;

  }

  if (state == 1 && mcp.digitalRead(drain_bucket_full) == HIGH) {

    lcd.print(0, 0, "Drain Bucket is"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "Full           ");
    var = 2;
    state = 0;
  }

  if (state == 1 && mcp.digitalRead(aquarium_empty) == LOW) {
    lcd.print(0, 0, "Water Drained   "); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(0, 1, "               ");
    var = 2;
    state = 3;
    Done.on();
  }
}
//------------------------------------------------------------------------------------------------------------------------


void turn_on() { //light control
  int value = 0;
  int msm = hour() * 60 + minute(); //minutes since midnight

  if ( msm < SUNRISE ) {
    value = 0;
  } else if (msm < DAWN) {
    value = map(msm, SUNRISE, DAWN, 0, 1024);
  } else if (msm < DUSK ) {
    value = 1024;
  } else if (msm < SUNSET ) {
    value = map(msm, DUSK, SUNSET, 1024, 0);
  } else {
    value = 0;
  }

  int brightness = map(value, 0, 1024, 0, 255);

  whiteled.setValue(brightness);



  //BLYNK_LOG("Time: %d %d - setting lights to %d", hour(), minute(), value);

  analogWrite(white, value);

}

void temp() {
  sensors.requestTemperatures();
  float currentTemp;
  currentTemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V4, currentTemp); // Virtual 0


  if (currentTemp >= 28.5) {
    analogWrite(fan, 1024);
    fanled.on();
  }
  else if (currentTemp <= 28.5)
  {
    analogWrite(fan, 0);
    fanled.off();
  }

}

void setup() {
  WiFi.mode(WIFI_STA);
  Serial.begin(9600);
  mcp.begin();      // use default address 0
  // Start up the library

  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {}
  sensors.begin();
  // sensors.setResolution(10);
  ArduinoOTA.setHostname("Aquarium"); // OPTIONAL
  ArduinoOTA.begin();

  pinMode(white, OUTPUT);
  pinMode(fan, OUTPUT);

  mcp.pinMode(drain_bucket_full, INPUT);
  mcp.pinMode(fill_bucket_empty, INPUT);
  mcp.pinMode(aquarium_empty, INPUT);
  mcp.pinMode(watersensor, INPUT);
  mcp.pinMode(aquarium_full, INPUT);

  mcp.pullUp(drain_bucket_full, HIGH);  // turn on a 100K pullup internally
  mcp.pullUp(fill_bucket_empty, HIGH);
  mcp.pullUp(aquarium_empty, HIGH);
  mcp.pullUp(aquarium_full, HIGH);

  mcp.pinMode(Fill_Pump, OUTPUT);
  mcp.pinMode(solenoid, OUTPUT);

  mcp.digitalWrite(Fill_Pump, LOW);
  mcp.digitalWrite(solenoid, LOW);
  lcd.clear();

  // Begin synchronizing time
  rtc.begin();

  timer.setInterval(10000L, turn_on);
  //timer.setInterval(1000L, clockDisplay);
  timer.setInterval(2000L, temp);
  timer.setInterval(1000L, water_sensor);
  timer1 = timer.setInterval(500L, fill);
  timer2 = timer.setInterval(500L, drain);
  timer3 = timer.setInterval(1500L, auto_mode);
  timer.disable(timer1);
  timer.disable(timer2);
  timer.disable(timer3);
}

void loop() {

  switch (var) {
    case 1: solenoid_on();
      break;
    case 2: solenoid_off();
      break;
    case 3: Pump_on();
      solenoid_off();
      break;
    case 4: Pump_off();
      break;
    case 5: all_off();
      break;

    default:
      // if nothing else matches, do the default
      // default is optional
      break;
  }

  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
}

Yes… lots of code :stuck_out_tongue_winking_eye: so I can not follow it all through, but these two areas jump out as possible issues…

1st FYI, while() statements are blocking until the conditions break them out… many never hit the void loop()?

2nd, this is a lot of switch case steps to follow before it even hits the Blynk.run() command. This entire function should be on a timer of it’s own and making allowances for any longer causes to have limited delays/blocking to avoid WDT or disconnection.

Hi @Gunner

Sorry for my late response I was busy these past few days. I tried a new sketch with evrything removed but the temperature read out and the lights. The sensor still goes to -127 and back to normal temperature every few seconds. The project dissconected problem has been resolved for now. This is the code I used. Is it possible that the problem is with the code?

#include <ArduinoOTA.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>

#include <TimeLib.h>
#include <WidgetRTC.h>

#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 14

// 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);
WidgetRTC rtc;



#define white 13 //esp
#define fan 12 //esp

#define SUNRISE 9*60 + 0
#define DAWN 10*60 + 0
#define DUSK 16*60 + 0
#define SUNSET 17*60 + 0

WidgetLED fanled(V12);
WidgetLED whiteled(V11);

BlynkTimer timer;

float currentTemp;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx071a2";
char ssid[] = "Clxxxxxxxxxi";
char pass[] = "clzxxxxxxxxxxxxxxxxx";

void turn_on() {
  int value = 0;
  int msm = hour() * 60 + minute(); //minutes since midnight

  if ( msm < SUNRISE ) {
    value = 0;
  } else if (msm < DAWN) {
    value = map(msm, SUNRISE, DAWN, 0, 1024);
  } else if (msm < DUSK ) {
    value = 1024;
  } else if (msm < SUNSET ) {
    value = map(msm, DUSK, SUNSET, 1024, 0);
  } else {
    value = 0;
  }

  int brightness = map(value, 0, 1024, 0, 255);

  whiteled.setValue(brightness);



  //BLYNK_LOG("Time: %d %d - setting lights to %d", hour(), minute(), value);

  analogWrite(white, value);

}

void temp() {
  sensors.requestTemperatures();

  currentTemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V4, currentTemp); // Virtual 0


  if (currentTemp >= 28.5) {
    analogWrite(fan, 1024);
    fanled.on();
  }
  else if (currentTemp <= 28.5)
  {
    analogWrite(fan, 0);
    fanled.off();
  }

}

void setup() {
  WiFi.mode(WIFI_STA);
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);


  // sensors.setResolution(10);
  ArduinoOTA.setHostname("Aquarium"); // OPTIONAL
  ArduinoOTA.begin();

  pinMode(white, OUTPUT);
  pinMode(fan, OUTPUT);



  // Begin synchronizing time
  rtc.begin();
  
  sensors.begin();

  timer.setInterval(10000L, turn_on);
timer.setInterval(2000L, temp);

}

void loop() {
  Blynk.run();
  ArduinoOTA.handle();
  timer.run();
}


@claytoncamilleri100 try running the bare Blynk sketch with the DS18B20 and see if you still get the -127 entries. Better still does this DS18B20 sketch, without Blynk, give the -127 entries:

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  Serial.begin(115200);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();
}
 

void loop(void)
{
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0));
}

Hi @Costas

I tried the sketch with my project. I learned something wierd. When I uploaded the sketch and opened the serial monitor I got the right temperature. At the time the Wemos D1 Mini was powered with the micro USB and when I turned on the power supply that was powering the whole circuit the sensor was going to -127 and then to normal temperature repeatedly. It the power supply interfering with the sensor and can the problem be because I extended the wires of the sensor with 1 wire for the data pin of the Ds18b20 and one 2-core wire for power to the sensor?

USB powered Wemos/No power Supply

Wemos powered with External Power and USB connected for debugging

My understanding is you can use either but not both together.

But the problem is still there when I use Blynk to read my data without a USB connected. The Wemos powered from the transformer. :confused:

What results do you get if you power the WeMos from the USB port as that’s what’s it there for?

Yes I said that I powered the Wemos only from the USB the temp is read correctly as expected.

Problem solved, power supply issue.

I am using one like this

.

I am using a buck convertor to get 5v to the Wemos board and sensor. How can I fix this damn problem. Can I add a big capacitor to help?

I have a buck converter for my 12V car battery and the WeMos with GPS runs fine. It also runs fine with a small power bank. I’m not a big user of caps but you could try, failing that just hunt around for a stable PSU.

Hi @costas

I just got this psu especially for this project. Is there a way I can make a circuit that can regulate the 12 v coming from the psu?

Is the 12V PSU and buck converter just powering the Mini Pro or is it powering some of your other aquarium items?

The Mini Pro only needs a regular USB connection (5V, 0.5A) and you should be able to pick up a genuine charger for a few dollars. Don’t buy cheap PSU’s from the Far East unless you categorically know they provide the power stated on the device.

sorry for jumping in, but for these sensors you have to use a pullup resistor, which value is based on the cable length between sensor / mcu. for short distances (under 1.5m) you can try to use the mcu built in pullup resistor.

do you have any kind of pullup on this sensor?

if you do not experience system crashes or restarts, than not the psu is the problem. if you have the above 2 simptoms, than psu. low quality buck / boost converters sometimes can be very noisy, and this high frequency noise you can not filter with caps.

try a linear regulator, (like the lm7805, with the reccomended caps in the datasheet).

1 Like

The 12v Psu is powering an Rgb led strip and a 12v solenoid. These are controlled with a buz11 mosfet using the Wemos D1 Mini. The 12 v coming from the psu is converted to 5v which is connected to the 5v input on the Wemos. The sensor is connected to the 5v from the buck convertor. Maybe I should try to connect it to the 3v3 pin on the Wemos?

Maybe you should use the USB port as that’s what it’s there for. All the caps and regulators are tied to the USB port.

1 Like

also, if you supply the sensor directly from 5v, it means that it will send 5v signal to the wemos, which is waiting a max 3.3v signal… this is why you should hook up the sensor supply to the wemos.

Don’t be sorry actually thanks for helping. The sensor is 2 meters away from the mcu and I have a 4k7 pull up resistor connected to the data line. No this is the only problem I have with this project. The sensor read out. This is the buck convertor I am using.

I will try the linear regulator and see what happens.

Oh will try that also

i see. also, check that the wemos pin where the sensor is hooked, it does not have a built in pulldown. because the pins on wemos are very tricky, some are pulled up some are pulled down. if this is the case, your 4.7k pullup will fight against the built in pulldown, potentially cancelling each other out.

1 Like