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");
}
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");
}
}
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
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");
}
}
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)