25/04/2019

3 Step to use External Interrupt on ESP8266

By snorlaxprime

I was in the middle of replicating my 433 MHz remote control using Arduino. The receiver circuit is quite simple and it is using Arduino pin 2 (interrupt) to detect the data signal from the receiver.

I was wondering whether I can do the same thing with ESP8266. After a short google, I found some information about Interrupt pin D3 (GPIO0) and also D0 (GPIO16). At a glance it seems do able.

Here are the steps to use the interrupt.

  1. Initialise IO pin as input
  2. Initialise IO with Interrupt Subroutine definition
  3. Interrupt Subroutine.

Here are the code that I stumble upon from the Circuits4you.com website.

const int interruptPin = 0; //GPIO 0 (Flash Button) STEP 1
const int LED=2; //On board blue LED
 
void setup() {
  Serial.begin(115200);
  pinMode(LED,OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE); // STEP 2
}
 
void loop()
{
    digitalWrite(LED,HIGH); //LED off
    delay(1000);
    digitalWrite(LED,LOW); //LED on
    delay(1000);
}
 
//This program get executed when interrupt is occurs i.e.change of input state - STEP 3
void handleInterrupt() {
    Serial.println("Interrupt Detected");
}

I will try this tonight and report back whether the Interrupt detection works or not.

The Remote.

BAUHN Remote Control

The remote consist of 4 ON button and 4 OFF buttons, each one is labelled A, B, C and D.It works independently controlling the remote power point switch.

The power point needs to be sync against one of the button pair (A, B, C or D). In this experiment I am detecting button B.

Below are the connection between ESP8266 to the 433 MHz receiver. As you can see I only connected 3 cables. 3.3V Vcc for Power (red cable), GND (green cable) and Data (Yellow) connected to GPIO0 of ESP8266 (D3).

I had used a slightly different sketch which is utilising the rc-switch library, and it also can provide the same detection that I had done using Arduino Uno. Here are the sketch that I had used to detect the remote.

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin GPIO0 or D3 in ESP8266

pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (mySwitch.available()) {
Serial.print("Received ");
output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
mySwitch.resetAvailable();
}
}

I had connect the receiver to Pin D3 on ESP8266, and getting the following result when I press the B-ON button.

Received Decimal: 9085236 (24Bit) Binary: 100010101010000100110100 Tri-State: not applicable PulseLength: 500 microseconds Protocol: 5
Raw data: 7036,1061,455,589,934,607,918,613,952,1071,454,575,955,1065,457,565,971,1033,509,509,1021,1022,506,526,995,542,981,548,977,543,979,1055,482,561,1057,466,1080,942,548,980,540,486,1067,956,552,484,1071,564,966,
Received Decimal: 9085236 (24Bit) Binary: 100010101010000100110100 Tri-State: not applicable PulseLength: 500 microseconds Protocol: 5
Raw data: 6969,1116,407,629,907,553,987,554,928,1085,445,593,924,1123,405,623,912,1122,403,625,901,1049,489,537,988,546,974,556,965,565,970,1045,483,561,944,559,969,1070,453,1073,453,578,949,1066,460,577,946,545,1021,

So I can conclude that the Interrupt definitely working ok. The next step would be to capture a few more data and trying to send it back via the 433MHz transmitter to try to replicate each of the button. Given that the ESP8266 can be connected to WIFI and also can server as a webserver this will make it possible to control the remote via internet as an IoT device.

Stay tune for the next article. Please share or subscribe if you like to see more of this article and feel free to drop me a comment.