Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Attila Tőkés
Published © CC BY

Extreme Weather Monitoring with Specialized Sensors

Detect lightnings and extreme UVA radiation detection with the AS3935 and VEML6070 sensors, and a TTGO T-Bean ESP32 based sensor.

BeginnerFull instructions provided12 hours5,401
Extreme Weather Monitoring with Specialized Sensors

Things used in this project

Hardware components

Arduino MKR Fox 1200
Arduino MKR Fox 1200
×1
AS3935 Lightning Sensor
×1
TTGO T-Beam ESP32
×1
MIKROE UV 3 Click Board
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

Code

AS3935 Basic Lightning I2C Example

Arduino
/*

  This example demonstrates how to detect lightning! It has a few basic
  settings to help with rejecting noise or "disturbers" (false lightning events). 
  It uses the onboard interrupt hardware pin, so in addition to attaching to
  it data lines you'll need to connnect to the interrupt pin labled "INT". 

  By: Elias Santistevan
  SparkFun Electronics
  Date: May, 2019
  License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

*/

#include <SPI.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"

// 0x03 is default, but the address can also be 0x02, 0x01.
// Adjust the address jumpers on the underside of the product. 
#define AS3935_ADDR 0x03 
#define INDOOR 0x12 
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01

SparkFun_AS3935 lightning(AS3935_ADDR);

// Interrupt pin for lightning detection 
const int lightningInt = 4; 

// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector. 
int intVal = 0;
int noise = 2; // Value between 1-7 
int disturber = 2; // Value between 1-10

void setup()
{
  while (!Serial);
  
  // When lightning is detected the interrupt pin goes HIGH.
  pinMode(lightningInt, INPUT); 

  Serial.begin(115200); 
  Serial.println("AS3935 Franklin Lightning Detector"); 

  Wire.begin(); // Begin Wire before lightning sensor. 

  if( !lightning.begin() ) { // Initialize the sensor. 
    Serial.println ("Lightning Detector did not start up, freezing!"); 
    while(1); 
  }
  else
    Serial.println("Schmow-ZoW, Lightning Detector Ready!");

  // The lightning detector defaults to an indoor setting at 
  // the cost of less sensitivity, if you plan on using this outdoors 
  // uncomment the following line:
  //lightning.setIndoorOutdoor(OUTDOOR); 
}

void loop()
{
  if(digitalRead(lightningInt) == HIGH){
    // Hardware has alerted us to an event, now we read the interrupt register
    // to see exactly what it is. 
    intVal = lightning.readInterruptReg();
    if(intVal == NOISE_INT){
      Serial.println("Noise."); 
      // Too much noise? Uncomment the code below, a higher number means better
      // noise rejection.
      //lightning.setNoiseLevel(setNoiseLevel); 
    }
    else if(intVal == DISTURBER_INT){
      Serial.println("Disturber."); 
      // Too many disturbers? Uncomment the code below, a higher number means better
      // disturber rejection.
      //lightning.watchdogThreshold(threshVal);  
    }
    else if(intVal == LIGHTNING_INT){
      Serial.println("Lightning Strike Detected!"); 
      // Lightning! Now how far away is it? Distance estimation takes into
      // account any previously seen events in the last 15 seconds. 
      byte distance = lightning.distanceToStorm(); 
      Serial.print("Approximately: "); 
      Serial.print(distance); 
      Serial.println("km away!"); 
    }
  }
  delay(100); // Slow it down.
}

UV Click 3 Example Sketch

Arduino
#include <Wire.h>
#include "Adafruit_VEML6070.h"

Adafruit_VEML6070 uv = Adafruit_VEML6070();

void setup() {
  Serial.begin(9600);
  Serial.println("VEML6070 Test");
  uv.begin(VEML6070_1_T);  // pass in the integration time constant
}


void loop() {
  Serial.print("UV light level: ");
  Serial.println(uv.readUV());
  
  delay(1000);
}

MQTT Application

Arduino
#include <WiFi.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"
#include "Adafruit_VEML6070.h"
#include <PubSubClient.h>

// WiFi & MQTT
const char* ssid     = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_server = "YOUR_MQTT_SERVER";
WiFiClient espClient;
PubSubClient client(espClient);

// AS 3935 Lighthing Sensor
// 0x03 is default, but the address can also be 0x02, 0x01.
// Adjust the address jumpers on the underside of the product.
#define AS3935_ADDR 0x03
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01

SparkFun_AS3935 lightning(AS3935_ADDR);

// Interrupt pin for lightning detection
const int lightningInt = 4;

// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector.
int intVal = 0;
int noise = 2; // Value between 1-7
int disturber = 2; // Value between 1-10

// VEML6070 UVA Sensor
Adafruit_VEML6070 uv = Adafruit_VEML6070();

void as3935_setup() {
  if ( !lightning.begin() ) { // Initialize the sensor.
    Serial.println ("Lightning Detector did not start up, freezing!");
    while (1);

  } else {
    Serial.println("Schmow-ZoW, Lightning Detector Ready!");
  }
}

void veml6070_setup() {
  uv.begin(VEML6070_1_T);  // pass in the integration time constant
}

void wifi_setup() {
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  client.setServer(mqtt_server, 1883);

  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("esp32/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(115200);
  delay(10);

  Wire.begin(21, 22);

  wifi_setup();
  as3935_setup();
  veml6070_setup();
}

int value = 0;

void wifi_loop() {
  client.loop();
}

void as3935_loop() {
  if (digitalRead(lightningInt) == HIGH) {
    // Hardware has alerted us to an event, now we read the interrupt register
    // to see exactly what it is.
    intVal = lightning.readInterruptReg();
    if (intVal == NOISE_INT) {
      Serial.println("Noise.");
      // Too much noise? Uncomment the code below, a higher number means better
      // noise rejection.
      //lightning.setNoiseLevel(setNoiseLevel);
      client.publish("ttgo/lightning", "Noise!");
    }
    else if (intVal == DISTURBER_INT) {
      Serial.println("Disturber.");
      // Too many disturbers? Uncomment the code below, a higher number means better
      // disturber rejection.
      //lightning.watchdogThreshold(threshVal);
      client.publish("ttgo/lightning", "Disturber!");
    }
    else if (intVal == LIGHTNING_INT) {
      Serial.println("Lightning Strike Detected!");
      // Lightning! Now how far away is it? Distance estimation takes into
      // account any previously seen events in the last 15 seconds.
      byte distance = lightning.distanceToStorm();
      Serial.print("Approximately: ");
      Serial.print(distance);
      Serial.println("km away!");

      String message = "";
      message += distance;
      client.publish("ttgo/lightning", message.c_str());
    }
    delay(100); // Slow it down.
  }
}

void veml6070_loop() {
  long uva = uv.readUV();
  Serial.print("UV light level: ");
  Serial.println(uva);

  String message = "";
  message += uva;
  client.publish("ttgo/uva", message.c_str());
}

void loop()
{
  wifi_loop();
  as3935_loop();
  veml6070_loop();
  delay(500);
}

Credits

Attila Tőkés

Attila Tőkés

36 projects • 221 followers
Software Engineer experimenting with hardware projects involving IoT, Computer Vision, ML & AI, FPGA, Crypto and other related technologies.

Comments