stevemarple / AS3935

Arduino library to support Austrian Microsystems AS3935 lightning sensor.
GNU Lesser General Public License v2.1
9 stars 4 forks source link

IRQ handling with the ESP8266? #3

Open 3PO opened 6 years ago

3PO commented 6 years ago

I modified the as3935_demo.ino as follow:

/*
-------------------------------
AS3935   |  ESP8266 (ModeMCU)
---------+---------------------
MISO/SDA |  D3
MISO     |  GND
SCK/SCL  |  D4
IRQ      |  D5
SI       |  +3,3V
CS       |  GND
GND      |  GND
Vdd      |  +3,3V
------------------------------

Pin mapping:
https://github.com/esp8266/Arduino/issues/584
*/

#include <AsyncDelay.h>
#include <SoftWire.h>
#include <AS3935.h>

#define SDA_PIN D3
#define SCL_PIN D4
#define IRQ_PIN D5

AS3935 as3935;
bool ledState = true;
AsyncDelay d;

void int2Handler(void)
{
  as3935.interruptHandler();
}

void readRegs(uint8_t start, uint8_t end)
{
  for (uint8_t reg = start; reg < end; ++reg) {
    delay(50);
    uint8_t val;
    as3935.readRegister(reg, val);

    Serial.print("Reg: 0x");
    Serial.print(reg, HEX);
    Serial.print(": 0x");
    Serial.println(val, HEX);
    Serial.flush();
  }
  Serial.print("State: ");
  Serial.println(as3935.getState(), DEC);
  Serial.println("-------------");
}

void setup(void)
{

  Serial.begin(115200);
  as3935.initialise(SDA_PIN, SCL_PIN, 0x03, 3, true, NULL);
  as3935.start();
  d.start(1000, AsyncDelay::MILLIS);

  while (!d.isExpired())
    as3935.process();

  readRegs(0, 0x09);
  as3935.setNoiseFloor(0);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, ledState);
  pinMode(IRQ_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(IRQ_PIN), int2Handler, RISING);
  d.start(1000, AsyncDelay::MILLIS);
  Serial.println("setup() done");
}

uint8_t count = 0;
void loop(void)
{
  if (as3935.process()) {
    uint8_t flags = as3935.getInterruptFlags();
    uint8_t dist = as3935.getDistance();

    Serial.println("-------------------");
    Serial.print("Interrupt flags: ");
    Serial.println(flags, HEX);
    Serial.print("Distance: ");
    Serial.println(dist, DEC);
  }
  if (as3935.getBusError()) 
    Serial.println("Bus error!");

    if (as3935.getTriggered()) 
    Serial.println("Triggered!");

  if (d.isExpired()) {
    ledState = !ledState;
    digitalWrite(LED_BUILTIN, ledState);

    if (++count > 5) {
      count = 0;
      readRegs(0, 0x09);
    }
    d.start(1000, AsyncDelay::MILLIS);
  }

}

But the issue is allways "Triggered!"

PORT OPEN 115200

Communication with MCU..Got answer! Communication with MCU established.
AutoDetect firmware...

Can't autodetect firmware, because proper answer not received (may be unknown firmware). 
Please, reset module or continue.
$
Reg: 0x1: 0x22
Reg: 0x2: 0xC2
Reg: 0x3: 0x0
Reg: 0x4: 0x0
Reg: 0x5: 0x0
Reg: 0x6: 0x0
Reg: 0x7: 0x3F
Reg: 0x8: 0x3
State: 4
-------------
setup() done
Reg: 0x0: 0x24
Reg: 0x1: 0x2
Reg: 0x2: 0xC2
Reg: 0x3: 0x0
Reg: 0x4: 0x0
Reg: 0x5: 0x0
Reg: 0x6: 0x0
Reg: 0x7: 0x3F
Reg: 0x8: 0x3
State: 5
-------------
-------------------
Interrupt flags: 1
Distance: 63
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
-------------------
Interrupt flags: 1
Distance: 63
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
-------------------
Interrupt flags: 1
Distance: 63
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
Triggered!
-------------------

Where is my fail?

Info about the external IRQs at the ESP8266: https://techtutorialsx.com/2016/12/11/esp8266-external-interrupts/

stevemarple commented 6 years ago

In your case above Interrupt flags: 1 indicates that the noise level is too high; you need to find a better location away from electrical interference.

The AS3935_demo sketch in v1.0.4 tidies up the printing and removes the extra "Triggered!" lines. The reason for the interrupt is now given by a string so it is not necessary to decode the AS3935 interrupt flags to identify environments with too much background noise or disturbers.