Hi again!
Since I managed to operated two SPI interfaces in parallel to connect MAX31865 Boards and the TFT Display I became megalomaniac. I tried to init a third SPI interface to make the touch function of the screan run. Unfortunately the third SPI seems not to work as I can't see any edges with the oscilloscope. Does anyone ever used three SPI in parallel with the ESP32? This Is my code so far. Of course it can be optimized.
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h> //Arduino Uno; SDA = A4, SCL = A5
#include <SPI.h>
#include <Arduino_GFX_Library.h>
#include <sheme.h>
#include <XPT2046_Touchscreen.h>
// Pins for SPI bus for the display
#define TFT_CLK 40 //SPI Clock
#define TFT_DA 41 //SPI Data (MOSI)
#define TFT_DC 1 //Data/Command
#define TFT_CS 2 //Chip select
#define TFT_RST 37 //Reset
#define WHITE 0xFFFF
#define BLACK 0x0000
Arduino_DataBus *bus = new Arduino_ESP32SPI(TFT_DC, TFT_CS,TFT_CLK, TFT_DA);
Arduino_GFX *tft = new Arduino_ILI9341(bus, TFT_RST, 0, false);
// define SPI for MAX31865
#define TEMP_CS 10
#define TEMP_MOSI 11
#define TEMP_CLK 12
#define TEMP_MISO 13
#define VSPI FSPI
SPIClass *TempSPI = NULL;
//SPIClass TempSPI = SPIClass(VSPI);
// define Touchscreen SPI pins
#define XPT2046_IRQ 20 // T_IRQ
#define XPT2046_MOSI 47 // T_DIN
#define XPT2046_MISO 21 // T_OUT
#define XPT2046_CLK 36 // T_CLK
#define XPT2046_CS 35 // T_CS
SPIClass *TouchSPI = NULL;
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
const char* SSID = "##############";
const char* PSK = "###############";
const char* MQTT_BROKER = "###.###.#.###";
WiFiClient espClient;
PubSubClient client(espClient);
char ResolTempPuffer_1_1_string[7]={0};
char ResolTempPuffer_1_3_string[7]={0};
//Variables for the PT100 boards
double resistance;
double Temperature[8] ={0,0,0,0,0,0,0,0};
double Old_Temperature[8] ={0,0,0,0,0,0,0,0};
float ResolTempPuffer_1_1 = 0, ResolTempPuffer_1_1_old = 0, ResolTempPuffer_1_3 = 0, ResolTempPuffer_1_3_old = 0;
uint8_t PowerFlowHeater = 0, Old_PowerFlowHeater = 0;
uint8_t PowerPump = 0, Old_PowerPump = 0;
uint16_t Offset = 10;
uint8_t Turn = 1;
uint8_t NumberOfPT100 = 2;
//uint8_t chipSelectPin;
uint8_t X1 = 40, X2 = 200, X3 = 130;
uint8_t Y = 45;
uint8_t OffSet = 35, OffSet2 = 22;
int x, y, z;
//--Display---------------------------------------------
const int ChipSelectPin[8] ={10,9,46,3,8,18,17,16};
const int chipSelectPin_1 = 10;
const int chipSelectPin_2 = 9;
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
client.setCallback(MQTTreceive);
tft->begin();
tft->setRotation(1);
TouchSPI = new SPIClass(); // Software SPI
TouchSPI->begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
TempSPI = new SPIClass(VSPI);
TempSPI->begin(TEMP_CLK, TEMP_MISO, TEMP_MOSI, TEMP_CS);
Serial.begin(115200); //Start serial
for (uint8_t n = 0; n < 8; n++){
pinMode(ChipSelectPin[n], OUTPUT); //because CS is manually switched
}
// prepare Screen once
FillWholeScreen(BLACK);
tft->drawBitmap(0,0, epd_bitmap_Sheme,320,240,WHITE);
}
void loop()
{
if (!client.connected()) {
while (!client.connected()) {
client.connect("FlowHeaterESP32");
client.subscribe("Flowheater/Puffer_1_1_Temperature");
client.subscribe("Flowheater/Puffer_1_3_Temperature");
delay(100);
}
}
client.loop();
// read all 8 Temperature Sensors
for (uint8_t n = 0; n < 8; n++){
resistance = readRegister(ChipSelectPin[n]);
Temperature[n] = convertToTemperature(resistance);
//Serial.printf("Temperature %d:",n);
//Serial.println(Temperature[n]); //Temperature in Celsius degrees
}
//Delete all Values
// left row
tft->setTextSize(1);
tft->setTextColor(BLACK);
tft->setCursor(X1, Y);
tft->print(ResolTempPuffer_1_1_old);
tft->setCursor(X1, Y + OffSet);
tft->print(Old_Temperature[0]);
tft->setCursor(X1, Y + 2*OffSet);
tft->print(ResolTempPuffer_1_3_old);
tft->setCursor(X1, Y + 3*OffSet);
tft->print(Old_Temperature[1]);
// rigth row
tft->setCursor(X2, Y);
tft->print(Old_Temperature[2]);
tft->setCursor(X2, Y + OffSet);
tft->print(Old_Temperature[3]);
tft->setCursor(X2, Y + 2*OffSet);
tft->print(Old_Temperature[4]);
tft->setCursor(X2, Y + 3*OffSet);
tft->print(Old_Temperature[5]);
// Temperatures ans Power of the Flowheater
tft->setCursor(X3, Y + OffSet);
tft->print(Old_Temperature[6]);
tft->setCursor(X3, Y + OffSet + OffSet2);
tft->printf("%u %%",Old_PowerFlowHeater);
tft->setCursor(X3, Y + OffSet + 2*OffSet2);
tft->print(Old_Temperature[7]);
tft->setCursor(X3, Y + OffSet + 3*OffSet2);
tft->printf("%u %%",Old_PowerPump);
// Print new Values
tft->setTextSize(1);
tft->setTextColor(WHITE);
// left row
tft->setCursor(X1, Y);
tft->print(ResolTempPuffer_1_1);
tft->setCursor(X1, Y + OffSet);
tft->print(Temperature[0]);
tft->setCursor(X1, Y + 2*OffSet);
tft->print(ResolTempPuffer_1_3);
tft->setCursor(X1, Y + 3*OffSet);
tft->print(Temperature[1]);
// rigth row
tft->setCursor(X2, Y);
tft->print(Temperature[2]);
tft->setCursor(X2, Y + OffSet);
tft->print(Temperature[3]);
tft->setCursor(X2, Y + 2*OffSet);
tft->print(Temperature[4]);
tft->setCursor(X2, Y + 3*OffSet);
tft->print(Temperature[5]);
// Temperatures ans Power of the Flowheater
tft->setCursor(X3, Y + OffSet);
tft->print(Temperature[6]);
tft->setCursor(X3, Y + OffSet + OffSet2);
tft->printf("%u %%",PowerFlowHeater);
tft->setCursor(X3, Y + OffSet + 2*OffSet2);
tft->print(Temperature[7]);
// Power of the Pump
tft->setCursor(X3, Y + OffSet + 3*OffSet2);
tft->printf("%u %%",PowerFlowHeater);
// store old Values for deleting
for (uint8_t i = 0; i < 8; i++){
Old_Temperature[i] = Temperature[i];
}
delay(200);
if (touchscreen.tirqTouched() && touchscreen.touched()) {
// Get Touchscreen points
TS_Point p = touchscreen.getPoint();
// Calibrate Touchscreen points with map function to the correct width and height
x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
z = p.z;
printTouchToSerial(x, y, z);
delay(100);
}
}
double convertToTemperature(double resistance_read)
{
double Z1, Z2, Z3, Z4, Rt;
double RTDa = 3.9083e-3;
double RTDb = -5.775e-7;
double rpoly = 0;
double temp;
Rt = resistance_read;
Rt /= 32768;
Rt *= 430; //This is now the real resistance in Ohms
Z1 = -RTDa;
Z2 = RTDa * RTDa - (4 * RTDb);
Z3 = (4 * RTDb) / 100;
Z4 = 2 * RTDb;
temp = Z2 + (Z3 * Rt);
temp = (sqrt(temp) + Z1) / Z4;
if (temp >= 0)
{
// Serial.print("Temperature: ");
// Serial.println(temp); //Temperature in Celsius degrees
return temp; //exit
}
else
{
Rt /= 100;
Rt *= 100; // normalize to 100 ohm
rpoly = Rt;
temp = -242.02;
temp += 2.2228 * rpoly;
rpoly *= Rt; // square
temp += 2.5859e-3 * rpoly;
rpoly *= Rt; // ^3
temp -= 4.8260e-6 * rpoly;
rpoly *= Rt; // ^4
temp -= 2.8183e-8 * rpoly;
rpoly *= Rt; // ^5
temp += 1.5243e-10 * rpoly;
// Serial.print("Temperature: ");
// Serial.println(temp); //Temperature in Celsius degrees
}
//Note: all formulas can be found in the AN-709 application note from Analog Devices
return temp;
}
double readRegister(uint8_t chipSelectPin)
{
double resist;
uint8_t reg1, reg2; //reg1 holds MSB, reg2 holds LSB for RTD
uint16_t fullreg; //fullreg holds the combined reg1 and reg2
TempSPI->beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE1));
digitalWrite(chipSelectPin, LOW);
TempSPI->transfer(0x80); //80h = 128 - config register
TempSPI->transfer(0xB0); //B0h = 176 - 10110000: bias ON, 1-shot, start 1-shot, 3-wire, rest are 0
digitalWrite(chipSelectPin, HIGH);
digitalWrite(chipSelectPin, LOW);
TempSPI->transfer(1);
reg1 = TempSPI->transfer(0xFF);
reg2 = TempSPI->transfer(0xFF);
digitalWrite(chipSelectPin, HIGH);
fullreg = reg1; //read MSB
fullreg <<= 8; //Shift to the MSB part
fullreg |= reg2; //read LSB and combine it with MSB
fullreg >>= 1; //Shift D0 out.
resist = fullreg; //pass the value to the resistance variable
//note: this is not yet the resistance of the RTD!
digitalWrite(chipSelectPin, LOW);
TempSPI->transfer(0x80); //80h = 128
TempSPI->transfer(144); //144 = 10010000
TempSPI->endTransaction();
digitalWrite(chipSelectPin, HIGH);
// Serial.print("Resistance: ");
// Serial.println(resist);
return resist;
}
void FillWholeScreen(uint16_t color)
{
for (uint16_t x = 0; x < 320; x++)
{
for (uint8_t y = 0; y < 240;y++)
{
tft->drawPixel(x,y,color);
}
}
}
void setup_wifi() {
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.println(WiFi.localIP());
}
void MQTTreceive(char* topic, byte* payload, unsigned int length){
// ResolTempPuffer_1_1
if (strcmp(topic,"Flowheater/Puffer_1_1_Temperature") == 0){
ResolTempPuffer_1_1_old = ResolTempPuffer_1_1;
for (int i = 0; i < length-3; i++){ // only consider the numbers
ResolTempPuffer_1_1_string[i]= char(payload[i]);
}
ResolTempPuffer_1_1_string[4] = {'\0'};
ResolTempPuffer_1_1 = atof(ResolTempPuffer_1_1_string);
if (ResolTempPuffer_1_1 != ResolTempPuffer_1_1_old){
//Output_Power1_Update = true;
}
Serial.println(ResolTempPuffer_1_1_string);
}
// ResolTempPuffer_1_3
if (strcmp(topic,"Flowheater/Puffer_1_3_Temperature") == 0){
ResolTempPuffer_1_3_old = ResolTempPuffer_1_3;
for (int i = 0; i < length-3; i++){ // only consider the numbers
ResolTempPuffer_1_3_string[i]= char(payload[i]);
}
ResolTempPuffer_1_3_string[4] = {'\0'};
ResolTempPuffer_1_3 = atof(ResolTempPuffer_1_3_string);
if (ResolTempPuffer_1_3 != ResolTempPuffer_1_3_old){
//Output_Power1_Update = true;
}
Serial.println(ResolTempPuffer_1_3_string);
}
}
void printTouchToSerial(int touchX, int touchY, int touchZ) {
Serial.print("X = ");
Serial.print(touchX);
Serial.print(" | Y = ");
Serial.print(touchY);
Serial.print(" | Pressure = ");
Serial.print(touchZ);
Serial.println();
}