I've hashed this code together to logg /chart data on a screen however it starts to draw pixels on the left of the screen and continues across the screen to the right. Then auto scrolls across the screen.
Therefore the X scale changes once the data fills the screen.
Initially (untill auto scroll begins) the latest data is on the left of the screen.....then, once the data fills the screen, the latest data is on the right. - Have I explained this correctly?
What I want to do is start the data logging on the LEFT not the RIGHT of the screen.
As this is not totally my code I'm unable to work it our but I assume it will be a cordinate somewhere??
Thanks in advance.
(FYI this is for a Beer Fermentation fridge, so you'll be helping the me produce better beer!!)
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;
}
}