Datalogging Chart Update Timing Issues.

I'm Quite new to programming the ESP8266 with Arduino, I'm at the stage of cutting and pasting code from various sources to create what I need and learning along the way.

Hardware: ESP8266 board, 2.8" TFT Screen and a DS18B Temp Sensor

I've put together a datalogging sketch to log data to the screen over the past 48 hours as well as sending to a MQTT server for more long term storage.
HOWEVER
I have the updating of the data points drawn on the screen (and hence logging rate) via a time delay. I'm finding that under testing with a fast update / delay of 10,000 ms (10seconds) actually translates to more like 11 seconds between updates.
So I'm assuming something else in the code is skewing what I want to be the update / logging rate, and hence using a "delay" is not the best thing to use to set logging rate.
Any ideas on what would be a better way and (as I'm a beginner) how to implement it.

include <Wire.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <AsyncMqttClient.h>
#include <PubSubClient.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Fonts/FreeSerif9pt7b.h> 
#include <Fonts/FreeSans9pt7b.h>

#define TFT_DC D4
#define TFT_CS D8
#define WIFI_SSID 
#define WIFI_PASSWORD "
#define MQTT_HOST IPAddress(192, 168, 1, 113)
#define MQTT_PORT 1883
#define MQTT_PUB_TEMP "esp/ds18b20/temperature"
const int oneWireBus = 5; //(GPIO5 - D1)
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
float temp;
float xscale;
float del;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
float humidity, temperature;  
float min_temp = 100, max_temp = -100, min_humi = 100, max_humi = -100;
#define max_readings 290
#define del 10000 // measurement rate in ms 600000
float temp_readings[max_readings+1] = {0};
float humi_readings[max_readings+1] = {0};
int   reading = 1;
#define temp_error_offset   1
#define humi_error_offset   1
#define autoscale_on  true
#define autoscale_off false
#define barchart_on   true
#define barchart_off  false


 void DrawGraph(int x_pos, int y_pos, int width, int height, int Y1Max, String title, float DataArray[max_readings], boolean auto_scale, boolean barchart_mode, int graph_colour) {
  #define auto_scale_major_tick 5 // Sets the autoscale increment, so axis steps up in units of e.g. 5
  #define yticks 15                // 5 y-axis division markers
  int maxYscale = 0;
  if (auto_scale) {
    for (int i=1; i <= max_readings; i++ ) if (maxYscale <= DataArray[i]) maxYscale = DataArray[i];
    maxYscale = ((maxYscale + auto_scale_major_tick + 2) / auto_scale_major_tick) * auto_scale_major_tick; // Auto scale the graph and round to the nearest value defined, default was Y1Max
    if (maxYscale < Y1Max) Y1Max = maxYscale; 
  }
  tft.drawRect(x_pos,y_pos,width+2,height+3,WHITE);
  tft.setTextSize(2);
  tft.setTextColor(graph_colour);
  tft.setCursor(x_pos + (width - title.length()*12)/2,y_pos-20); 
  tft.print(title);
  tft.setTextSize(1);
  // Draw the data
  int x1,y1,x2,y2;
  for(int gx = 1; gx <= max_readings; gx++){
    x1 = x_pos + gx * width/max_readings; 
    y1 = y_pos + height;
    x2 = x_pos + gx * width/max_readings; 
    y2 = y_pos + height - constrain(DataArray[gx],0,Y1Max) * height / Y1Max + 1;
    if (barchart_mode) {
      tft.drawLine(x1,y1,x2,y2,graph_colour);
    } else {
      tft.drawPixel(x2,y2,graph_colour);
      tft.drawPixel(x2,y2-1,graph_colour); 
  }
  //Draw the Y-axis scale
  for (int spacing = 0; spacing <= yticks; spacing++) {
    #define number_of_dashes 40
    for (int j=0; j < number_of_dashes; j++){ // Draw dashed graph grid lines
      if (spacing < yticks) tft.drawFastHLine((x_pos+1+j*width/number_of_dashes),y_pos+(height*spacing/yticks),width/(2*number_of_dashes),WHITE);
    }
    tft.setFont(); 
    tft.setTextSize(1);
    tft.setTextColor(YELLOW);
    tft.setCursor((x_pos-20),y_pos+height*spacing/yticks-4);
    tft.print(Y1Max - Y1Max / yticks * spacing);
    tft.setFont(); 
    }
}
AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;

unsigned long previousMillis = 0;   
const long interval = 10000;        

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.begin(SSID,PASSWORD);
 }
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
  Serial.println("C-Wi-Fi.");
  tft.setTextColor(GREEN);
  tft.setTextSize(1);
  tft.setCursor(0,0);
//  tft.print("WiFi");
  connectToMqtt();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  Serial.println("Disconnected from Wi-Fi.");
  mqttReconnectTimer.detach(); 
  wifiReconnectTimer.once(2, connectToWifi);
  tft.setTextColor(RED);
  tft.setTextSize(1);
  tft.setCursor(0,0);
}
void connectToMqtt() {
  Serial.println("CMQTT...");
  mqttClient.connect();
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Re");
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Rem");
 }
void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Remote Database: Connecting.....");
  tft.setTextColor(GREEN);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Remote Database: Conneted");
 }
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Rem.");
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Rem");
  tft.setTextColor(RED);
  tft.setTextSize(1);
  tft.setCursor(0,233);
  tft.print("Rem");
  tft.setTextColor(BLACK);
  tft.setTextSize(1);
  tft.setCursor(265,0);
  tft.print("DB  :OK");
  tft.setTextColor(RED);
  tft.setTextSize(1);
  tft.setCursor(270,0);
  tft.print("DB:FAIL");
  if (WiFi.isConnected()) {
    mqttReconnectTimer.once(2, connectToMqtt);
}
}
void onMqttPublish(uint16_t packetId) {
  Serial.print("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
  tft.setTextColor(GREEN);
  tft.setTextSize(1);
  tft.setCursor(265,0);
  tft.print("DB  :OK");
  //tft.print(packetId);
}
void setup(){
  Serial.begin(9600);
  wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
  wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
 
   connectToWifi();
  sensors.begin();
  tft.begin(); 
  tft.setRotation(3);
  tft.setTextSize(2);
  tft.setTextColor(WHITE);
  tft.fillScreen(BLACK);   
  analogWriteFreq(500);    
  analogWrite(D0, 750);    
  for (int x = 0; x <= max_readings; x++){
    temp_readings[x] = 0;
    humi_readings[x] = 0;
  } 
  
}

void loop(){
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  float temperature = sensors.getTempCByIndex(0);
  int temp=sensors.getTempCByIndex(0);
  int unit=temp%10;
  int tens=temp/10;
  float rem=temperature-temp;
  int temp1=rem*100;
  int tens1=temp1/10;
  int unit1=temp1%10;
  tft.setFont(&FreeSans9pt7b); 

 
 
DrawGraph(20,20,290,180,30,"",temp_readings, autoscale_off,  barchart_off, RED); //YI_Max is the Y scale max figure.
tft.setFont();//reset font
 reading = reading + 1;
  if (reading > max_readings) { 
    reading = max_readings;
    for (int i = 1; i < max_readings; i++) {
      temp_readings[i] = temp_readings[i+1];
      humi_readings[i] = humi_readings[i+1];
    }
    temp_readings[reading] = temperature;
    }
  delay(del); //Data logging rate??? 
  tft.fillScreen(BLACK);

unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
   
    previousMillis = currentMillis;
 
    sensors.requestTemperatures(); 

    temp = sensors.getTempCByIndex(0);
       
 
    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                            
    Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
    Serial.printf("Message: %.2f \n", temp);

xscale = (290 * (((del / 1000))/60)/60)/2;
}  
}

Just make the "Blink without Delay" sketch do "Update screen and log without Delay". It's the same logic.

The DS18B20 data sheet shows a conversion time of 750ms at 12-bit resolution.

The code is a complete mess. A lot of your problem probably lies here

  temp = sensors.getTempCByIndex(0);
  float temperature = sensors.getTempCByIndex(0);
  int temp=sensors.getTempCByIndex(0);

I have no idea what the rest of that part of the loop even implies, let alone actually does. Whatever it is, DS18B20s send floats and mere mortals find that simply getting a float and printing it suffices. It is not as if you are actually pressed for time. You will surely find an example in the Dallas library you already have which should be all you need. I'm not familiar with that particular display code, but it seems that you are drawing a graph each time you go round the loop, which cannot possibly be a good idea. Redrawing only pixels that have changed is sufficient.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.