I have a memory exception on core 1. The funny thing is that I experienced the same issue with the wifi connection block at the beginning of setting the code without anything else block interacting with wifi but it solved somehow when I put it in the setup(). The first time I suspected the loads or the cables but it isn't because if I use another sketch with thingspeak functions is ok; Here is the other code that interacts ok with thinkspeak.
#include <AHT20.h>
#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
#include "ThingSpeak.h"
#include <Adafruit_BMP280.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#define SENSOR 27
Adafruit_BMP280 bmp;
AHT20 aht20;
const char *ssid = "CYTA_CRby_2.4G"; // replace with your wifi ssid and wpa2 key
const char *password = "PhYkD6YZ";
//const char* server = "184.106.153.149";
unsigned long myChannelNumber = 2514774;
const char * myWriteAPIKey = "Y9V8GLKAH5ZOAA7Q";
const char * myReadAPIKey = "X7GOTNVKG9JH4XJT";
const int FieldNumber6 = 6; // The field you wish to read
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000; // your network key Index number (needed only for WEP)
WiFiClient client;
#define TFT_DC 2 //VCC, GND, SCL = CLK, SDA = DIN, RS / DC = D/C, RES = RST, CS
#define TFT_RST -1
#define TFT_CS 5
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// Initialize our values
String myStatus = "";
float temp; // get temperature in °C
float humi; // get humidity in %
float pres;
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 0.45;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
void setup() {
Serial.begin(115200);
Serial.println(F("AHT20+BMP280 test"));
Wire.begin();
if (aht20.begin() == false) {
Serial.println("AHT20 not detected. Please check wiring. Freezing.");
while (1);
}
Serial.println("AHT20 acknowledged.");
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
tft.setTextWrap(false); // Allow text to run off right edge
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setTextSize(1.5); // text size = 2
tft.setCursor(7, 4); // move cursor to position (5, 76) pixel
tft.print("Flr L/min");
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setTextSize(1); // text size = 2
//tft.setCursor(40, 28); // move cursor to position (5, 76) pixel
//tft.print("L/m");
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setTextSize(1.5); // text size = 2
tft.setCursor(70, 4); // move cursor to position (5, 76) pixel
tft.print("Total Ltrs");
// Print the cumulative total of litres flowed since starting
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setTextSize(1.5); // text size = 2
//tft.setCursor(125, 28); // move cursor to position (5, 76) pixel
//tft.print("L");
tft.drawFastHLine(0, 47, tft.width(), ST7735_WHITE); // draw horizontal white line at position (0, 30)
tft.drawFastVLine(67, -80, tft.width(), ST7735_WHITE);
tft.setTextColor(ST7735_WHITE, ST7735_BLACK); // set text color to white and black background
tft.setTextSize(1);
tft.drawFastHLine(0, 90, tft.width(), ST7735_WHITE); // draw horizontal white line at position (0, 76)
tft.drawFastHLine(0, 125, tft.width(), ST7735_WHITE); // draw horizontal white line at position (0, 122)
tft.setTextColor(ST7735_RED, ST7735_BLACK); // set text color to red and black background
tft.setCursor(25, 54); // move cursor to position (25, 39) pixel
tft.print("TEMPERATURE");
tft.setTextColor(ST7735_CYAN, ST7735_BLACK); // set text color to cyan and black background
tft.setCursor(34, 94); // move cursor to position (34, 85) pixel
tft.print("HUMIDITY");
tft.setTextColor(ST7735_GREEN, ST7735_BLACK); // set text color to green and black background
tft.setCursor(34, 131); // move cursor to position (34, 131) pixel
tft.print("PRESSURE");
tft.setTextSize(2); // text size = 2
// print °C
tft.drawCircle(89, 70, 2, ST7735_YELLOW); // print degree symbol ( ° )
tft.setCursor(95, 70); // move cursor to position (95, 54) pixel
tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); // set text color to yellow and black background
tft.print("C");
tft.setCursor(91, 146);
tft.print("hPa");
unsigned status;
//status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
status = bmp.begin();
if (!bmp.begin())
{
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
//pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop() {
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(10000);
}
Serial.println("\nConnected.");
printWifiStatus();
readTLData(); // read last logged totallitres data
Serial.println("Updating totalLitres adding+");
long ThTL=readTLData() ;
totalLitres=ThTL;
}
float totalMilliLitres;
if ((millis() - lastTime) > timerDelay) {
// Get
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, humi);
ThingSpeak.setField(3, pres/100);
ThingSpeak.setField(4, flowLitres);
ThingSpeak.setField(5, totalMilliLitres/1000);
ThingSpeak.setField(6, totalLitres);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
temp = aht20.getTemperature(); // get temperature in °C
humi = aht20.getHumidity(); // get humidity in %
pres = bmp.readPressure(); // get pressure in Pa
Serial.printf("Temperature: %.02f *C\n", temp);
Serial.printf("Humidity: %.02f %RH\n", humi);
Serial.printf("Pressure: %.02f hPa\n", pres/100);
// CODE FROM flow.h
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
pulse1Sec = pulseCount;
pulseCount = 0;
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(float(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t");
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL / ");
Serial.print(totalLitres);
Serial.println("L");
}
tft.setCursor(5, 24);
tft.setTextColor(0xFD00, ST7735_BLACK); // set text color to orange and black background
tft.printf("%02u.%01u", (int)flowRate, (int)(flowRate* 100) % 100 );
tft.setCursor(78, 24);
tft.setTextColor(0xFD00, ST7735_BLACK); // set text color to orange and black background
tft.printf("%04u", (int)totalLitres, (int)(totalLitres* 100) % 100 );
// print data on the display
// print temperature (in °C)
tft.setCursor(11, 70);
tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); // set text color to yellow and black background
if(temp < 0) // if temperature < 0
tft.printf( "-%02u.%02u", (int)abs(temp), (int)(abs(temp) * 100) % 100 );
else // temperature >= 0
tft.printf( " %02u.%02u", (int)temp, (int)(temp * 100) % 100 );
// 2: print humidity
tft.setCursor(23, 108);
tft.setTextColor(ST7735_MAGENTA, ST7735_BLACK); // set text color to magenta and black background
tft.printf( "%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 );
// 3: print pressure (in hPa)
tft.setCursor(3, 146);
tft.setTextColor(0xFD00, ST7735_BLACK); // set text color to orange and black background
tft.printf( "%04u.%02u", (int)(pres/100), (int)((uint32_t)pres % 100) );
delay(3000); // Wait 20 seconds to update the channel again
}
void pulseCounter()
{
pulseCount++;
}
int readTLData() {
long TL = ThingSpeak.readLongField(myChannelNumber, FieldNumber6, myReadAPIKey);
int y = ThingSpeak.getLastReadStatus();
if (y == 200)
{
Serial.print("Totallitres from ThingSpeak: ");
Serial.println(TL);
return TL;
}
else
{
Serial.println("Unable to read channel / No internet connection");
return -1;
}
delay(100);
//-------------- End of Channel 1 -------------//
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}