In 2014 I started contributing to Open Source electronics, and life has just gotten better from there. Founder of Maven Technologies.
View all posts by Manoj R. Thakur →
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.
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.
Lets Look at the program that caused 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
#define InterruptButton 0 //On board Flash Button as interrupt
#define LED 2 //on Board Blue LED
void setup() {
Serial.begin(115200);
pinMode(LED,OUTPUT);
pinMode(InterruptButton,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(InterruptButton),myInterruptHandler,CHANGE);
}
void loop() {
// LED Blinking repeatedly:
digitalWrite(LED,HIGH);
delay(1000);
digitalWrite(LED,LOW);
delay(1000);
}
void myInterruptHandler()
{
Serial.println("You Pressed Flash Button");
delayMicroseconds(1000000); //Simple 1 Sec Delay
Serial.println("I am after 1 Sec");
}
Example 3: ESP as Web Server and AP
This program Looks perfect. But causing No errors or Doing nothing.
After Uploading This program.
We must see HomeServer wifi network.
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));
void loop() {
server.handleClient();
Analog = String(analogRead(A0));
}
By commenting Analog read line and re-uploading the program you will immediately see the HomeServer access point
/*
* Copyright (c) 2015, circuits4you.com
* All rights reserved.
/* Create a WiFi access point and provide a web server on it. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "HomeServer";
ESP8266WebServer server(80);
String Analog;
//=======================================================================
// handles main page 192.168.4.1
//=======================================================================
/* Just a little test message. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
*/
void handleRoot() {
String s = "hello from esp8266!";
s = s +" ADC:" + Analog;
digitalWrite(2, HIGH);
server.send(200, "text/plain", s);
digitalWrite(2, LOW);
}
//=======================================================================
// Power on setup
//=======================================================================
void setup() {
delay(1000);
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid); //AP without password Open Network
IPAddress myIP = WiFi.softAPIP();
server.on("/", handleRoot);
server.begin();
}
//=======================================================================
// Main Program Loop
//=======================================================================
void loop() {
server.handleClient();
Analog = String(analogRead(A0));
}
//=======================================================================
Lesson No.3: Give Enough Time to server.handleClient();
To solve this problem update loop() with below code
void loop() {
server.handleClient();
Analog = String(analogRead(A0));
delay(1000);
}
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
while(digitalRead(Key)==HIGH)
{
yield(); //This will prevent wdt reset
}
Now Let’s See List of Exception Codes of ESP8266
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
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→
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
How to Configuring ThingSpeak Cloud server Account ?
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.
Initialize IO pin as Input.
Initialize IO with Interrupt Subroutine definition.
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.