nodemcu v1 , code inside loop not running!

Hello ! I'm using a nodemcu v1 and i have a strange problem, i have some tft functions from adafruit inside the loop that are not running. The only thing that runs inside the loop is server.handleClient() function.

Regards Antmar

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include <Adafruit_ILI9341esp.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Arduino.h>



// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);



// For the Adafruit shield, these are the default.
#define TFT_RST 16
#define TFT_DC 5
#define TFT_CS 0

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

 
// arrays to hold device addresses
  byte TempRetur[8] =   { 0x28, 0xFF, 0xFC, 0x36, 0x51, 0x14, 0x4, 0xE8 };
  byte TempTur[8] = { 0x28, 0xFF, 0xC6, 0x83, 0x50, 0x14, 0x4, 0x6A };





const char* ssid = "OpenWrt";
const char* password = "";

ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!");
  digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){

  SPI.setFrequency(ESP_SPI_FREQ);

  tft.begin();
  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(1);

  sensors.begin();
  sensors.setResolution(TempTur, TEMPERATURE_PRECISION);
  sensors.setResolution(TempRetur, TEMPERATURE_PRECISION);

  DesenCentrala(); 
  
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
 
  sensors.requestTemperatures();

 

  // temperatura tur centrala   
     tft.setCursor(170, 60);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(2);
     tft.print(printTemperature(TempTur));

//temperatura retur centrala
    // tft.setCursor(170, 110);
     tft.setCursor(20, 20);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(2);
     tft.print((int)printTemperature(TempRetur));
    
     server.handleClient();
    
}

void DesenCentrala() {
   
 //centrala 
     tft.drawRect( 40,90 ,60 ,100 ,ILI9341_WHITE);
     tft.drawLine(40,90 ,60 ,70 , ILI9341_WHITE);
     tft.drawLine(60,70 ,120 ,70 , ILI9341_WHITE);
     tft.drawLine(120,70 ,120 ,170 , ILI9341_WHITE);
     tft.drawLine(100,90 ,120 ,70 , ILI9341_WHITE);
     tft.drawLine(99,189 ,120 ,170 , ILI9341_WHITE);
     tft.drawRect( 50,100 ,40 ,45 ,ILI9341_WHITE);
     tft.drawRect( 50,150 ,40 ,30 ,ILI9341_WHITE);
     tft.drawCircle(46, 96, 2, ILI9341_GREEN);      //se muta in loop !!!
     tft.fillCircle(46, 96, 2, ILI9341_GREEN);

// tub amestec
     tft.drawRoundRect(205, 50, 35, 30, 15,ILI9341_WHITE);
     tft.drawRoundRect(205, 160, 35, 30, 15,ILI9341_WHITE);
     tft.drawLine(205,71 ,205 ,170 , ILI9341_WHITE);
     tft.drawLine(239,71 ,239 ,170 , ILI9341_WHITE);
     tft.fillRect( 206,141 ,33 ,30 ,ILI9341_BLACK);
     
//tevi centrala tub
     tft.drawLine(120,90 ,130 ,80 , ILI9341_WHITE);
     tft.drawLine(130,80 ,195 ,80 , ILI9341_WHITE);
     tft.drawLine(195,80 ,205 ,90 , ILI9341_WHITE);
     
     tft.drawLine(120,140 ,130 ,130 , ILI9341_WHITE);
     tft.drawLine(130,130 ,195 ,130 , ILI9341_WHITE);
     //tft.drawLine(175,130 ,195 ,130 , ILI9341_WHITE);
     tft.drawCircle(145, 130, 8, ILI9341_WHITE);       //symbol pompa central - tub
     tft.fillCircle(145, 130, 6, ILI9341_GREEN);       // se muta in loop !!!
     tft.drawLine(195,130 ,205 ,140 , ILI9341_WHITE);  
     tft.setCursor(142, 150);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(1);
     tft.print("P1");
     tft.drawLine(239,110 ,249 ,100 , ILI9341_WHITE);
     tft.drawLine(249,100 ,310 ,100 , ILI9341_WHITE);
     tft.drawCircle(295, 100, 8, ILI9341_WHITE);       //symbol pompa casa
     tft.fillCircle(295, 100, 6, ILI9341_GREEN);              // se muta in loop !!!
     tft.setCursor(290, 120);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(1);
     tft.print("P2");
     tft.drawLine(239,165 ,249 ,155 , ILI9341_WHITE);
     tft.drawLine(249,155 ,310 ,155 , ILI9341_WHITE);

     tft.fillCircle(195, 80, 3, ILI9341_RED);            //tur centrala 
     tft.fillCircle(195, 130, 3, ILI9341_BLUE);          //retur centrala
     tft.fillCircle(249, 100, 3, ILI9341_RED);           //tur casa
     tft.fillCircle(249, 155, 3, ILI9341_BLUE);          //retur casa

     tft.drawCircle(198, 57, 3, ILI9341_WHITE);
     tft.drawCircle(198, 109, 3, ILI9341_WHITE);

     tft.setCursor(0,0);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(2);
     tft.print((int)printTemperature(TempRetur));
     //tft.print("20");

     
  }

  float printTemperature(DeviceAddress deviceAddress){
  float tempC = sensors.getTempC(deviceAddress);
  return tempC;
}

What happens if you try a minimal "hello world" sort of sketch that only prints something to the TFT, without all the other unrelated code?

Initially i thought it was a problem related to ds18b20 (a hardware one) . I've called the printTemperature function inside setup and it's working.
I also tried to draw a simple rectangle inside loop without luck!

I've sorted out !!! :slight_smile: I was all because off const int led = 13;

Thanks everyone!

If you don't mind me asking, what about it was the problem?

The code that i had inside the loop was not running. I mean this code :

// temperatura tur centrala   
     tft.setCursor(170, 60);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(2);
     tft.print(printTemperature(TempTur));

//temperatura retur centrala
    // tft.setCursor(170, 110);
     tft.setCursor(20, 20);
     tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
     tft.setTextSize(2);
     tft.print((int)printTemperature(TempRetur));

It was all because const int led was defined on 13 and i think this was interfering with SPI pins.