Oilsensor via Thingspeak

Hello,
i got some problems with my programming. Can´t locate the problem and would be great if someone can me a hint.



#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 12
#include "ESP8266WiFi.h"
#include "SoftwareSerial.h"
#include "ThingSpeak.h"


const char* ssid = "MYWlanSSID";
const char* pass = "MYWlanSecret";
int status = WL_IDLE_STATUS;

WiFiClient client;

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
#define PIN_TRIGGER 12
#define PIN_ECHO    13

const int SENSOR_MAX_RANGE = 200; // in cm
unsigned long dauer;
unsigned int abstand;
int liter;
int Litercm = 27;     // 5000 l bei 185cm
unsigned long temperatur;
unsigned long Channel= 1598514;
const char * APIKey = "3630Z4P6DITZFZGB";

void setup()
{
  Serial.begin(9600);
    sensors.begin();
  pinMode(PIN_TRIGGER, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
  WiFi.begin(ssid, pass);
 
while (WiFi.status() != WL_CONNECTED) 
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

}


void loop()
{
 
 digitalWrite(PIN_TRIGGER, LOW);
 delayMicroseconds(2);
 digitalWrite(PIN_TRIGGER, HIGH);
 delayMicroseconds(10);
 digitalWrite(PIN_TRIGGER, LOW);
  
  dauer = pulseIn(PIN_ECHO, HIGH);
  abstand = dauer/58;

  //if (abstand > SENSOR_MAX_RANGE || abstand <= 0){
  //  Serial.println("Ausserhalb des Messbereichs!");
 // } else {
    Serial.println("Abstand: " + String(abstand) + " cm");
    
    liter=5000-(abstand*Litercm);
    Serial.println("Entspricht ca."+ String(liter));
    int x = ThingSpeak.writeField(Channel, 1, liter, APIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  

   Serial.print("Hole Temperaturen...");
   sensors.requestTemperatures(); // Send the command to get temperatures
   float tempC = sensors.getTempCByIndex(1);
   //Check if reading was successful
   if(tempC != DEVICE_DISCONNECTED_C) 
    {
      Serial.print("Temperatur 1 (index 0) : ");
      Serial.println(String(tempC));
      ThingSpeak.writeField(Channel, 2, tempC, APIKey);
  } 
  else
 {
  Serial.println("Error: Keine Temperaturdaten vorhanden.");
 }
  delay(1000);
}

The code is uploaded and the ultrasonic seems to work. I can´t get a temperature from the sensor and it doesn´t update thingspeak.

There´s is a crash in the software on Serial Monitor:

WiFi connected
Abstand: 41 cm
Entspricht ca.3893

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (28):
epc1=0x40201331 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffd60 end: 3fffffc0 offset: 0190
3ffffef0:  3fffff1c 3fffff1c 3ffe8712 3ffee56c  
3fffff00:  00186432 00000001 3fffff60 40201441  
3fffff10:  3ffefa84 000b000f 00000020 3ffefc84  
3fffff20:  000b000f 07fee6cc 3ffe8692 3ffe84cc  
3fffff30:  3ffe87e3 00000004 3fffff60 40204370  
3fffff40:  00186432 3fffff60 3fffff74 3ffe84cc  
3fffff50:  00186432 3ffee6cc 3ffee4f8 40201694  
3fffff60:  33393833 00120000 84fe0020 40203c51  
3fffff70:  3fffdad0 33393833 3ffee600 80203d48  
3fffff80:  3ffe87e3 3ffee550 3ffee6cc 40203d94  
3fffff90:  3fffdad0 3ffee550 3ffee6cc 3ffee744  
3fffffa0:  3fffdad0 00000000 3ffee704 402049e0  
3fffffb0:  feefeffe feefeffe 3ffe84f8 40100c29  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Tried to block part of the code with // but i can´t get it work

I use a Wemos d1 mini pro cause of the external antenna for this project and want to use 2 more of this wlan sensors for my watertank in the garden and so on.

Hope someone could explain what´s wrong

Hi
This delay " delay(1000);" causes this problem:
Exception (28):
epc1=0x40201331 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ref:
https://arduino-esp8266.readthedocs.io/en/latest/faq/a02-my-esp-crashes.html

RV mineirin

HI,

can´t understand why... (thanks for the link...)

I deleted the delay but there´s still the same problem. Seem´s the Loop part resets after getting the Ultrasonic measurement part.

ACTUALLY seems its a thingspeak problem..... Just trying to get it running without thingspeak so long.

Services like Thingspeak don't usually like to be spammed continuously. Try using millis to send less frequently.

Thanks....

My son just looked on the code and sayed: ThingSpeak.begin is missing.....

That´s all.... added ThingSpeak.begin(client); and actually it´s working fine.
To do:
DeepSleep so refresh is twice a day. It´s enough to check the oiltank.

Thanks to all....