So my blynk project programmed in arduino IDE and running on a wemos d1 (esp8266 breakout board) has a problem: the void loop runs super slow. The cultprit is the temperature sensor. The sensor only sends 2 readings a second. The loop waits for a reading everytime. How can I fix this. I actually only need a temp update every 2 or 3 seconds.
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(“Requesting temperatures…”);
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println(“DONE”);
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
}
Have simpletimer do the temp reading for you every 5s or so. That would be my solution In fact, it is my solution, because I use it in my home domotic solution and it runs very stable!
SimpleTimer is a very straightforward, but stable solution for doing timed things without hindering your loop(). The PushData example in Blynk is a good start, but here is some more elaborate stuff:
It doesnt happen very often in the tinkering world, but this was really frickin easy.
Thanks so much guys, my problem is totally fixed now, assuming blynk server will be able to deal with a hickyp in my loop every 10 seconds.
Below how i fixed my problem, if ever anyone wondered
// Include the libraries we need #include <OneWire.h> #include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino #define ONE_WIRE_BUS D7 #include <SimpleTimer.h>
int temperature = 20;
SimpleTimer simpleTimer;
boolean ledon;
unsigned long lasttime;
unsigned long now;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/*
The setup function. We only start the sensors here
*/
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println(“Dallas Temperature IC Control Library Demo”);
Main function, get and show the temperature
*/
void loop(void)
{
simpleTimer.run();
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(“Requesting temperatures…”);
Serial.println(“DONE”);
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
Serial.println(temperature);
}
void callback_SimpleTimer(){
now = millis();
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
Serial.println(now - lasttime);
lasttime = now;
}
Now that Blynk is back online, I realise not all is well.
When I call the function below while also having the blynk library on, my OLED crashes…
Any suggestions?
void temperatureFunction(){
now = millis();
if((now - lasttime) > 5000){
sensors.requestTemperatures();
// Begin syncing with temp sensor
temperature = sensors.getTempCByIndex(0);
//store sensor reading in variable temperature
lasttime = now;
}
}
Are all pins are used exclusively for each peripheral?
What happens if you change the interval on the temperature sensor?
(Most times I had weird behavior on my tinkering, it was power related)
The interval change makes my OLED flicker randomly like before , but faster or slower. I ll try hooking up a power supply to see if it is lack of power, good suggestion!
I tried to add a 9V power supply to my wemos D1
I also tried changing the pin config of my temp sensor.
The problem persists
EDIT: def not a power problem tried feeding from severall supplies, even at same time.
Even when the sensor is not plugged in, the oled crashes.
It is enough to call the function.
So the sensor is not sharing any pins with the oled?
If on slower interval the flickering occurs at the same time as the temperature is read, there seems to be a relation.
I don’t know the inner works of the drivers, perhaps it is related to a hardware timer or interrupt.
At this point I would probably pick another type of board (non-ESP arduino, ag. ordinary atmega) and see how the same code behaves when oled and sensor are used at the same time (without blynk). If then its all okay I would add blynk to that board to see if the interaction is hardware or software related.
Edit: if the oled fails when the sensor is not plugged in but the sensor-function is called, there seems to be a software related issue. I would still try the code with the oled on another type board.
Hey Maxint, Thank you for your response.
No shares pins, all is exactly as you describe it.
I only have wemos boards available for this wifi application so other boards are not an option,
But I am going to try to read the sensor from another board and share it s reading over I2C Because I fear this is just one of those glitches that wont be solved without ruining something else…
I am using the wemos modified adafruit libraries for the oled
and their respective Adafruit_GFX.h library
I am using the full size d1 first version
The dallas library for the ds18b20
and the latest blynk library and app.
I am truely clueless as why everything works seperately but they just dont want to play together so any insights you may have would be very much appreciated!