
Most important of all, it is relatively inexpensive for the given performance. The DHT11 is chosen because it is lab calibrated, accurate and stable and its signal output is digital.
Nodemcu lua simple delay how to#
However, manipulating hardware timers requires manipulation on register level, such that the resulting code is not easily portable to other devices using the Arduino IDE.Robo India presents tutorial on how to read temperature and humidity data through DHT11 sensor using ESP8266 wifi module on NODEMCU LUA platform. It uses hardware timers to create a PWM signal of a desired duty cycle and frequency. The LED will blink perfectly on time, regardless of what your main program was just doing.Ī typical example is the PWM functionality. However, with a timer interrupt, you can set up the interrupt, then turn on the timer. If you are not using timers but just conventional code techniques, you’d have to set a variable with the next time the LED should blink, then check constantly to see if that time had arrived. So suppose you have a device that needs to do something –like blink an LED every 5 seconds. Rather than running a loop or repeatedly calling millis(), you can let the hardware (here: a timer) do that work for you while your code does other things. Timers, like external interrupts, run independently from your main program.
Nodemcu lua simple delay serial#
With this method, the voltage will change between ground and VCC and, when the falling edge occurs, the interrupt will trigger the execution of interrupt function and the console log can be seen in the serial monitor. Be carefull to avoid connecting the ground pin to the wrong GPIO. Then, the easiest way to generate interrupts without any additional hardware is to connect and disconnect the ground pin of the ESP8266 to the GPIO with the interrupt routine configured. Total: ") Īfter uploading the code to the ESP8266, just open the serial console. when interrupt is triggered function "myInterruptServiceRoutine" is called.ĪttachInterrupt(digitalPinToInterrupt(interruptPin), myInterruptServiceRoutine, FALLING) sets interruptPin (pin13) as interrupt that is triggered on a falling edge. sets interruptPin (pin 13) as input with pull-up Thus, it needs to be declared as "volatile" variable interruptCounter will be used in the interrupt service routine. Standard Arduino interrupt types are supported: CHANGE, RISING, FALLING. Interrupts may be attached to any GPIO pin except GPIO16, but since GPIO6-GPIO11 are typically used to interface with the flash memory ICs on most esp8266 modules, applying interrupts to these pins are likely to cause problems. Pin interrupts are supported through attachInterrupt(), detachInterrupt() functions. Then, the main code should have the logic necessary to handle it. The best approach is to signal the main code, using for example a flag or a counter, to indicate that the interrupt has happened. So, we should not do blocking calls or handle communication, for example, inside an ISR. setting a flag), so the processor gets back to the execution of the main program. It is a good practice to design interrupt service routines as small as possible (e.g. Naturally, when an interrupt occurs, we need to handle it in a so-called interrupt service routine (ISR), which is the function that will be executed, when the interrupt happens. In other words, interrupts allow us to avoid polling. Interrupts are very useful because they allow us to be able to receive data from sensors in a microcontroller without constantly asking the sensor if it has new data. Interrupts are events or conditions that cause the microprocessor or microcontroller to stop the execution of the task that it is performing, work in a different task temporarily and come back to the initial task.

The delayMicroseconds function, on the other hand, does not yield to other tasks, so using it for delays more than 20 milliseconds is not recommended. There is also a yield() function which is equivalent to delay(0).

If you have a loop somewhere in your sketch that takes a lot of time (>50ms) without calling delay(), you might consider adding a call to delay function to keep the WiFi stack running smoothly.

WiFi and TCP/IP libraries get a chance to handle any pending events each time the loop() function completes, OR when delay() is called. Remember that there is a lot of code that needs to run on the chip besides the sketch to keep an existing WiFi connection alive.

delayMicroseconds(us) pauses for a given number of microseconds. Millis() and micros() return the number of milliseconds and microseconds elapsed after reset, respectively.ĭelay(ms) pauses the sketch for a given number of milliseconds and allows WiFi and TCP/IP tasks to run.
