Category Archives: IoT Tutorials

ESP8266 PWM Example

This ESP8266 PWM example explains how to use the Pulse Width Modulation (PWM)  with the ESP8266.

analogWrite(PIN,VALUE);
ESP8266 can generate PWM on all IO pins. The ESP8266 analogWrite is different than the Arduino Uno. ESP8266 uses 10-bit resolution for PWM generation PWM value varries from 0 to 1023. Arduino Uses 8-Bit Resolution i.e.PWM range is 0-254.

Continue reading ESP8266 PWM Example

ESP8266 Fatal exception and Wdt Reset

This post will guide you common issues and mistakes that cause Fatal Exception and wdt reset. Fatal exception comes at execution time. program compiles well logically looks correct but at Running suddenly these fatal exceptions come. They are difficult to find out. But I have kept a record of few causes of these fatal exception. That I am sharing here.

First we will simulate these errors with test code and correct it.

Example 1: LED Blink Wdt Reset

LED Blink Program Giving Wdt Reset Cause Error

Now we will look into program that caused wdt reset

wdt reset esp8266
wdt reset esp8266

The program is simple LED Blink but Why this wdt reset error ? can you guess….

This error is caused simply because #define LED 9. This IO 9 of ESP8266 is on bottom side these IOs cannot be used. Its My First Lesson.

Lesson 1: Make sure you are using correct IO numbers.

Example 2: External Interrupt Causing Fatal Exception(9)

The error in serial monitor.

Lets Look at the program that caused fatal exception(9)

esp8266 Fatal exception (9)
Fatal Exception(9)

Look closely to the Serial monitor You can See “You Pressed Flash Button” message that we are sending from interrupt handler.

Behavior of program:

LED Blinking works perfectly No Wdt Reset or any Fatal Exception until we send external interrupt. Right. Whats the problem then? Interrupt is called we can see message also.

Where is our second message “I am after 1 Sec” Yes you are right some thing after first serial out. i.e. delay(1000); This line causes fatal exception(9). Then Why? Logically every thing is correct then this fatal exception(9) from a delay????

Yes its bug in ESP8266 libraries. that are get used while compilation.

Lesson No. 2: Never Use Delays in External Interrupt Handler

How to Solve this Fatal Exception(9)? I want to use delay in interrupt

To solve this issue I use delayMicroseconds in interrupt handler this will not create any problem.

Modified Program with Same Functionality Without Fatal Exception(9) Error

Example 3: ESP as Web Server and AP

This program Looks perfect. But causing  No errors or Doing nothing.

After Uploading This program.

  1. We must see HomeServer wifi network.
  2. But there is no such network created by this code.

Why No WiFi Access point ?

Compilation is ok, No Errors or Exceptions in serial monitor then Why missing HotSpot?

The problem is in Analog = String(analogRead(A0));

By commenting Analog read line and re-uploading the program you will immediately see the HomeServer access point

Lesson No.3: Give Enough Time to server.handleClient();

To solve this problem update loop() with below code

You can use analogRead in hadleRoot() subroutine also.

Example No 4: ESP8266 Trapped in Infinite Loop

This type of error occurs when there is any loop that holding the program. Such as polling of a IO line. Waiting for some event to occur. Such as Wait until Motion Detected. Motion sensor related programs.

Lesson 4: Never Use IO polling in program holding state

Secret Function yield() to get rid of many problems

The yield() function is also implemented inside the ESP8266 libraries:

Yielding

This is one of the most critical differences between the ESP8266 and a more classical Arduino microcontroller. The ESP8266 runs a lot of utility functions in the background – keeping WiFi connected, managing the TCP/IP stack, and performing other duties. Blocking these functions from running can cause the ESP8266 to crash (fatal exceptions) and reset itself. To avoid these mysterious resets, avoid long, blocking loops in your sketch.

The amazing creators of the ESP8266 Arduino libraries also implemented a yield() function, which calls on the background functions to allow them to do their things.

How to use yield() Function?

Example 1: Causing Hang and WDT reset

while(digitalRead(Key)==HIGH); //This will hold the program until button event is occurs

To solve this issue use yield() function in this way

Now Let’s See List of Exception Codes of ESP8266

ESP8266 Exception cause

Notice:
The reboot state will not change after software WDT reset or software reset. For example, when the first reset is caused by a power reboot, the rst cause number is 1. After software reset, the rst cause number will still be 1.
EXC-CAUSE Code Cause Name Cause Description Required Option EXC-VADDR Loaded
0 IllegalInstructionCause Illegal instruction Exception No
1 SyscallCause SYSCALL instruction Exception No
2 InstructionFetchErrorCause Processor internal physical address or data error during instruction fetch Exception Yes
3 LoadStoreErrorCause Processor internal physical address or data error during load or store Exception Yes
4 Level1InterruptCause Level-1 interrupt as indicated by set level-1 bits in the INTERRUPT register Interrupt No
5 AllocaCause MOVSP instruction, if caller�s registers are not in the register file Windowed Register No
6 IntegerDivideByZeroCause QUOS, QUOU, REMS, or REMU divisor operand is zero 32-bit Integer Divide No
7 Reserved for Tensilica
8 PrivilegedCause Attempt to execute a privileged operation when CRING ? 0 MMU No
9 LoadStoreAlignmentCause Load or store to an unaligned address Unaligned Exception Yes
10..11 Reserved for Tensilica
12 InstrPIFDataErrorCause PIF data error during instruction fetch Processor Interface Yes
13 LoadStorePIFDataErrorCause Synchronous PIF data error during LoadStore access Processor Interface Yes
14 InstrPIFAddrErrorCause PIF address error during instruction fetch Processor Interface Yes
15 LoadStorePIFAddrErrorCause Synchronous PIF address error during LoadStore access Processor Interface Yes
16 InstTLBMissCause Error during Instruction TLB refill MMU Yes
17 InstTLBMultiHitCause Multiple instruction TLB entries matched MMU Yes
18 InstFetchPrivilegeCause An instruction fetch referenced a virtual address at a ring level less than CRING MMU Yes
19 Reserved for Tensilica
20 InstFetchProhibitedCause An instruction fetch referenced a page mapped with an attribute that does not permit instruction fetch Region Protection or MMU Yes
21..23 Reserved for Tensilica
24 LoadStoreTLBMissCause Error during TLB refill for a load or store MMU Yes
25 LoadStoreTLBMultiHitCause Multiple TLB entries matched for a load or store MMU Yes
26 LoadStorePrivilegeCause A load or store referenced a virtual address at a ring level less than CRING MMU Yes
27 Reserved for Tensilica
28 LoadProhibitedCause A load referenced a page mapped with an attribute that does not permit loads Region Protection or MMU Yes
29 StoreProhibitedCause A store referenced a page mapped with an attribute that does not permit stores Region Protection or MMU Yes
30..31 Reserved for Tensilica
32..39 CoprocessornDisabled Coprocessor n instruction when cpn disabled. n varies 0..7 as the cause varies 32..39 Coprocessor No
40..63 Reserved

Infos from Xtensa Instruction Set Architecture (ISA) Reference Manual
 

 

 

ESP8266 Ceramic vs PCB Antenna

Ceramic Chip Antennas vs. PCB Trace Antennas: A Comparison

Multi – purpose machine-to-machine devices require a high quality radio interface that will operate in the Zigbee, ISM, and cellular bands including LTE, bands which lie between 700 and 2500 MHz.
A trace antenna on a printed circuit board (PCB) is often the first type of interface considered for one of these applications. However, using a ceramic antenna can be a good or even better alternative. Continue reading ESP8266 Ceramic vs PCB Antenna

Thing Speak ESP8266

ThingSpeak™ is an IoT analytics cloud platform service that allows you to aggregate, visualize and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by ESP8266 to ThingSpeak. ThingSpeak is often used for prototyping and proof of concept IoT systems that require analytics.

In this tutorial, We will learn following thinds

  1. How to Configuring ThingSpeak Cloud server Account ?
  2. How to program ESP8266 to upload sensor data ?

Continue reading Thing Speak ESP8266

ESP8266 External Interrupt Example

In system programming, an interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing. The processor responds by suspending its current activities, saving its state, and executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal with the event. This interruption is temporary, and, after the interrupt handler finishes, the processor resumes normal activities.

In this tutorial we learn how to use external interrupts with ESP8266? External interrupts configuration requires three step process.

  1. Initialize IO pin as Input.
  2. Initialize IO with Interrupt Subroutine definition.
  3. Interrupt Subroutine.

The ESP8266 has two different kinds of interrupts: “external”, and “pin change”. ESP8266 all pins have external interrupt except GPIO 16. These interrupts can be set to trigger on RISING or FALLING signal edges, or CHANGE of level.

Continue reading ESP8266 External Interrupt Example