ESP8266 Switch on-board led off - all stops

Hi

in my code I use a mills() if statement
in that statement I switch on board led off.

What happens is that it seems to switch off all chip, or at least void loop stops

if (currentTime - previousTime >= 3000) {   //Change this value to publish at a different interval. 4 pkg pr loop, 60sec/4 pkg = 15 sec
    previousTime = currentTime;
    digitalWrite(LED_BUILTIN, LOW); // LOW = ON
    Serial.println("ON");
    sendReadings();                        //Function
    Serial.println("Send lora done");
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off to save power (HIGH = LED off)
    Serial.println("OFF");
}

Serial, last print is OFF

07:59:33.327 -> HiveMon starter...
07:59:33.327 -> Kube-1
07:59:37.580 -> Zero factor: 259167
07:59:37.580 -> LoRa tranceiver test
07:59:37.626 -> LoRa oppstart OK!
07:59:37.720 -> ON
07:59:37.720 -> Send readings
07:59:37.767 ->
07:59:37.767 ->
07:59:37.767 -> LoRa pakken er i luften:
07:59:37.767 -> Kubenavn: Kube-1
07:59:37.767 -> Weight g: 11326.01
07:59:37.814 -> Temperature: 19.30
07:59:37.814 -> Humidity: 40.00
07:59:37.814 -> Tare: 0.00
07:59:37.814 -> Alarm 1024=Alarm: 0.00
07:59:37.814 -> Packet ID: 1
07:59:37.814 -> HiveMon version: AbdCopy1
07:59:37.814 -> Kalibreringsfaktor: 22.88
07:59:37.814 -> ---------------------------------------------------------------
07:59:37.862 -> Send lora done
07:59:37.862 -> OFF

But we cannot see what is happening in loop().
Please post your full sketch

Hi

thanks for answer.

The prototype was working 100% before this LED code.

Today I started to optimize the code for power saving, and then I wanted led only to blink for each time the mills() statement is triggered.

here is full loop

void loop() {
  weight = scale.get_units();
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();

  //Read switches tare and Alarm
  int switchTareValue = digitalRead(switchTare);     // read switch tare
  int switchAlarmValaue = analogRead(A0);            // read switch alarm

  if (switchTareValue > 0) {    // if tare is pressed high. pin D1 always LOW, pres switch to tare
    functionTare();            // call function tare
    delay(1000);               // pause etter tare
  }

  if (analogRead(A0) < 1024 )     {           // Read alarm value from switch on A0, open = ALARM, closed NO ALARM
    alarmValue = analogRead(A0);              // for serial print
    functionAlarm();                          // call function alarm
    delay(1000);                              // pause for å lese av alarm, etter alarm er sendet
  }

  // Send readings
  //  Sender må sende skjeldnere, mottager må lese det den får.Fewer = power save
  unsigned long currentTime = millis();
  if (currentTime - previousTime >= 3000) {   //Change this value to publish at a different interval. 4 pkg pr loop, 60sec/4 pkg = 15 sec
    previousTime = currentTime;
    digitalWrite(LED_BUILTIN, LOW); // LOW = ON
    Serial.println("ON");
    sendReadings();
    Serial.println("Send lora done");
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off to save power (HIGH = LED off)
    Serial.println("OFF");
  }
}

Function:

void sendReadings() {
String LoRaMessage = String(packetID);
  Serial.println("Send readings");
  packetID++;  //increments LoRa packet +1

  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print(LoRaMessage);
  LoRa.endPacket();
}

You did not understand:

post your

complete sketch.

From the very first to the very last line.

Your loop has two functions

functionTare(); 
functionAlarm();

You are asking for help because you yourself are unable to solve the problem.
There is a high chance that the error is somewhere in the code that you don't think of
and this is the reason why you should post your

complete sketch

using this method

best regards Stefan

Which pin is defined as 'LED_BUILTIN' ? on the board you are using ? eg what board are you using. ?

I use ESP8266, HX711, AI thinker LoRa, dht11, 2 buttons

Led is on board, dont know wich pin, use pinMode(LED_BUILTIN, OUTPUT);

Code:


#include <SPI.h>
#include <Wire.h>

//Includes for LORA
#include <LoRa.h>


#include "HX711.h" 



#include <DHT.h>      //DHT11

#define DHTPIN D2    
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);


//Measuring runtime for when if mills kick in, this to delay sending of packets
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

//Prototype function calls
void sendReadings();
void functionTare();
void functionAlarm();

//hx711 pins:
#define LOADCELL_DOUT_PIN D3
#define LOADCELL_SCK_PIN D4
HX711 scale;
float calibration_factor = 22.88;

//define the pins used by the LoRa transceiver module
#define SCK 14    //D5
#define MISO 12   //D6
#define MOSI 13   //D7
#define SS 15     //D8
#define RST 16    //RST
#define DIO0 D0   //D0 

//433E6 for Asia, 866E6 for Europe,915E6 for North America
#define BAND 433E6

int packetID = 0; //packet counter       //reset after boot

//Variables to store temperature, humidity, weight and tare switch status, hive name
float temperature = 0;
float humidity = 0;
float weight = 0;
float tareValue = 0;
float alarmValue = 0;

//Variable to store unique  hive name
String hiveName = String("Kube-1");

// Switches Tare and Alarm
int switchTare = D1;    
int switchTareValue = 0;  //
int switchAlarm = A0;   
int switchAlarmValue = 1024;  


void setup() {
  Serial.begin(57600); delay(500);
  Serial.println();
  Serial.println("HiveMon starter...");
  Serial.print(hiveName); delay(3000);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();  delay(500);

  pinMode(LED_BUILTIN, OUTPUT);             // initialize digital pin LED_BUILTIN as an output.

  //scale.tare();  //Reset the scale to 0       //disabled to avoid tare when boot, to keep same load on scale
  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);

  //Setup for temperature and humidity functionality
  dht.begin();
  // Wire.setClock(400000);   // for I2C buss
  Serial.println();

  //Setup for LoRa
  Serial.println("LoRa tranceiver test");
  SPI.begin();
  LoRa.setPins(SS, RST, DIO0);
  if (!LoRa.begin(BAND)) {
    Serial.println("Oppsart LoRa feila!");
    while (1);
  }
  Serial.println("LoRa oppstart OK!");
  scale.set_scale(calibration_factor);
}

void loop() {
  weight = scale.get_units();
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();


  //Read switches tare and Alarm
  int switchTareValue = digitalRead(switchTare);    
  int switchAlarmValaue = analogRead(A0);            

  if (switchTareValue > 0) {    
    functionTare();            
    delay(1000);              
  }

  if (analogRead(A0) < 1024 )     {          
    alarmValue = analogRead(A0);              
    functionAlarm();                         
    delay(1000);                              
  }

  // Send readings
   unsigned long currentTime = millis();
  if (currentTime - previousTime >= 3000) {   
    previousTime = currentTime;
    digitalWrite(LED_BUILTIN, LOW); // LOW = ON
    Serial.println("ON");
    sendReadings();
    Serial.println("Send lora done");
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off to save power (HIGH = LED off)
    Serial.println("OFF");
  }
}

Functions:

void sendReadings() {
String LoRaMessage = String(packetID) + "/" + String(temperature) + "&" + String(humidity) + "-" + String(weight) + "#" + String(tareValue) + "¤" + String(alarmValue); // + "%" + String(hiveName);
  Serial.println("Send readings");
  packetID++;  
  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print(LoRaMessage);
  LoRa.endPacket();

}

void functionAlarm() {
  Serial.println("ALARM sent: ");
}
void functionTare() {
  scale.tare();
  tareValue = 0;   // set tare back to 0, tare function done
  Serial.println("Tare done.");
}

what is so hard about doing

if you are using Ardiuno-IDE 1.8.x:

  • doing a right-click with the mouse
  • choose "copy for forum"
  • change to browser and press Ctrl-V

if you are using Ardiuno-IDE 2.0.x:

  • press Ctrl-A to mark all
  • press Shift-Ctrl-C to copy into clipboard including forum-formattings
  • change to browser
  • press Ctrl-V

I have added more serial debug-output.
You haven't specified which exact libraries you are using. So I can't compile your code

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);

#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

#include <SPI.h>
#include <Wire.h>

//Includes for LORA
#include <LoRa.h>
#include "HX711.h" 
#include <DHT.h>      //DHT11

#define DHTPIN D2    
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);

//Measuring runtime for when if mills kick in, this to delay sending of packets
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

//Prototype function calls
void sendReadings();
void functionTare();
void functionAlarm();

//hx711 pins:
#define LOADCELL_DOUT_PIN D3
#define LOADCELL_SCK_PIN D4
HX711 scale;
float calibration_factor = 22.88;

//define the pins used by the LoRa transceiver module
#define SCK 14    //D5
#define MISO 12   //D6
#define MOSI 13   //D7
#define SS 15     //D8
#define RST 16    //RST
#define DIO0 D0   //D0 

//433E6 for Asia, 866E6 for Europe,915E6 for North America
#define BAND 433E6

int packetID = 0; //packet counter       //reset after boot

//Variables to store temperature, humidity, weight and tare switch status, hive name
float temperature = 0;
float humidity = 0;
float weight = 0;
float tareValue = 0;
float alarmValue = 0;

//Variable to store unique  hive name
String hiveName = String("Kube-1");

// Switches Tare and Alarm
int switchTare = D1;    
int switchTareValue = 0;  //
int switchAlarm = A0;   
int switchAlarmValue = 1024;  


void setup() {
  Serial.begin(57600); 
  delay(500);
  Serial.println();
  Serial.println("HiveMon starter...");
  Serial.print(hiveName); delay(3000);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();  delay(500);

  pinMode(LED_BUILTIN, OUTPUT);             // initialize digital pin LED_BUILTIN as an output.

  //scale.tare();  //Reset the scale to 0       //disabled to avoid tare when boot, to keep same load on scale
  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);

  //Setup for temperature and humidity functionality
  dht.begin();
  // Wire.setClock(400000);   // for I2C buss
  Serial.println();

  //Setup for LoRa
  Serial.println("LoRa tranceiver test");
  SPI.begin();
  LoRa.setPins(SS, RST, DIO0);
  
  if (!LoRa.begin(BAND)) {
    Serial.println("Oppsart LoRa feila!");
    while (1);
  }
  Serial.println("LoRa oppstart OK!");
  scale.set_scale(calibration_factor);
}

void loop() {
  dbgi("Top of loop",0,1000);
  weight = scale.get_units();
  dbgi("1:",weight,1000);

  humidity = dht.readHumidity();
  dbgi("1:",humidity,1000);

  temperature = dht.readTemperature();
  dbgi("1:",temperature,1000);

  //Read switches tare and Alarm
  int switchTareValue = digitalRead(switchTare); 
  dbgc("4:",switchTareValue);  

  int switchAlarmValaue = analogRead(A0);            
  dbgc("5:",switchAlarmValaue);   

  if (switchTareValue > 0) {    
    functionTare();            
    delay(1000);              
  }

  if (analogRead(A0) < 1024 )     {          
    alarmValue = analogRead(A0);              
    functionAlarm();                         
    delay(1000);                              
  }
  dbgc("6:",alarmValue);

  // Send readings
   unsigned long currentTime = millis();
  if (currentTime - previousTime >= 3000) {   
    previousTime = currentTime;
    digitalWrite(LED_BUILTIN, LOW); // LOW = ON
    Serial.println("ON");
    sendReadings();
    Serial.println("Send lora done");
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED off to save power (HIGH = LED off)
    Serial.println("OFF");
  }
  dbgi("bottom of loop",999,1000);
}

void sendReadings() {
  dbg("entering sendReadings",packetID);
  String LoRaMessage = String(packetID) + "/" + String(temperature) + "&" + String(humidity) + "-" + String(weight) + "#" + String(tareValue) + "¤" + String(alarmValue); // + "%" + String(hiveName);
  Serial.println("Send readings");
  packetID++;  
  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print(LoRaMessage);
  LoRa.endPacket();
  dbg("leaving sendReadings",packetID);
}

void functionAlarm() {
  Serial.println("ALARM sent: ");
}

void functionTare() {
  dbg("entering functionTare",tareValue);
  scale.tare();
  tareValue = 0;   // set tare back to 0, tare function done
  Serial.println("Tare done.");
}

It differs per board which is used, sometimes GPIO 2 like on nodeMCU and sometimes a different one like GPIO 1 on an ESP-01. It is defined in the boards.tct or available as a setting for generic8266.
a simple way to find out what it is without looking it up is

Serial.println(LED_BUILTIN, DEC);

if it is GPIO1 that is your issue, because that is also the TX pin connected to the USB - TTL chip (and the primary UART)