Introduction: Sensors Final Weather Station

Items you will need for this project:

1. SparkFun ESP32 Thing board

2. SpakFun ESP32 Environment Sensor Shield

3.Arduino Operating System

4.Micro USB cord

5. SparkFun Weather Station

6. WIFI

7.A functional device for Blynk

8. The Blynk App

Step 1: Arduino Code

#include SparkFunCCS811.h

#include "SparkFunBME280.h"

#include Sparkfun_APDS9301_Library.h

#include "Wire.h"

#include WiFi.h

#include WiFiClient.h

#include BlynkSimpleEsp32.h

BME280 bme;

CCS811 ccs(0x5B);

APDS9301 apds;

#define BLYNK_PRINT Serial

// Pin assignment definitions

#define WIND_SPD_PIN 14

#define RAIN_PIN 25

#define WIND_DIR_PIN 35

#define AIR_RST 4

#define AIR_WAKE 15

#define DONE_LED 5

// Variables and constants used in calculating the windspeed.

volatile unsigned long timeSinceLastTick = 0;

volatile unsigned long lastTick = 0;

// Variables and constants used in tracking rainfall

#define S_IN_DAY 86400

#define S_IN_HR 3600

#define NO_RAIN_SAMPLES 2000

volatile long rainTickList[NO_RAIN_SAMPLES];

volatile int rainTickIndex = 0;

volatile int rainTicks = 0;

int rainLastDay = 0;

int rainLastHour = 0;

int rainLastHourStart = 0;

int rainLastDayStart = 0;

long secsClock = 0;

String windDir = "";

float windSpeed = 0.0;

// You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon).

char auth[] = "Add Your Own Token Here";

// Your WiFi credentials. // Set password to "" for open networks.

char ssid[] = "WIFI Name";

char pass[] = "WIFI Password";

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5). // In the app, Widget's reading frequency should be set to PUSH. This means // that you define how often to send data to Blynk App.

void myTimerEvent() {

// You can send any value at any time.

// Please don't send more that 10 values per second.

float temp = bme.readTempF();

Blynk.virtualWrite(V5, temp);

float humid = bme.readFloatHumidity();

Blynk.virtualWrite(V4, humid);

float pres = bme.readFloatPressure();

Blynk.virtualWrite(V3, pres);

float winddir = analogRead(WIND_DIR_PIN);

Blynk.virtualWrite(V2, winddir);

float rainLH = (float(rainLastHour)*0.011);

Blynk.virtualWrite(V0, rainLH);

float rainLD = (float(rainLastDay)*0.011);

Blynk.virtualWrite(V6, rainLD);

float rainTD = (float(rainTicks)*0.011);

Blynk.virtualWrite (V7, rainTD);

if (timeSinceLastTick != 0) windSpeed = 1000.0/timeSinceLastTick; float windspeed = (windSpeed*1.492); Blynk.virtualWrite(V1, windspeed); }

void winddir(int vin) {

if (vin < 150) windDir="202.5";

else if (vin < 300) windDir = "180";

else if (vin < 400) windDir = "247.5";

else if (vin < 600) windDir = "225";

else if (vin < 900) windDir = "292.5";

else if (vin < 1100) windDir = "270";

else if (vin < 1500) windDir = "112.5";

else if (vin < 1700) windDir = "135";

else if (vin < 2250) windDir = "337.5";

else if (vin < 2350) windDir = "315";

else if (vin < 2700) windDir = "67.5";

else if (vin < 3000) windDir = "90";

else if (vin < 3200) windDir = "22.5";

else if (vin < 3400) windDir = "45";

else if (vin < 4000) windDir = "0";

else windDir = "0"; }

void windTick(void) {

timeSinceLastTick = millis() - lastTick;

lastTick = millis(); }

void rainTick(void) {

rainTickList[rainTickIndex++] = secsClock;

if (rainTickIndex == NO_RAIN_SAMPLES) rainTickIndex = 0;

rainTicks++; }

void setup() {

// Debug console

Serial.begin(9600);

Wire.begin();

// Wind speed sensor setup. The windspeed is calculated according to the number

// of ticks per second. Timestamps are captured in the interrupt, and then converted

// into mph.

pinMode(WIND_SPD_PIN, INPUT); // Wind speed sensor attachInterrupt(digitalPinToInterrupt(WIND_SPD_PIN), windTick, RISING);

// Rain sesnor setup. Rainfall is tracked by ticks per second, and timestamps of

// ticks are tracked so rainfall can be "aged" (i.e., rain per hour, per day, etc)

pinMode(RAIN_PIN, INPUT); // Rain sensor

attachInterrupt(digitalPinToInterrupt(RAIN_PIN), rainTick, RISING);

// Zero out the timestamp array.

for (int i = 0; i < NO_RAIN_SAMPLES; i++) rainTickList[i] = 0;

// BME280 sensor setup - these are fairly conservative settings, suitable for

// most applications. For more information regarding the settings available

// for the BME280, see the example sketches in the BME280 library.

bme.settings.commInterface = I2C_MODE;

bme.settings.I2CAddress = 0x77;

bme.settings.runMode = 3;

bme.settings.tStandby = 0;

bme.settings.filter = 0;

bme.settings.tempOverSample = 1;

bme.settings.pressOverSample = 1;

bme.settings.humidOverSample = 1;

bme.begin();

Blynk.begin(auth, ssid, pass);

// You can also specify server:

//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

// Setup a function to be called every second timer.setInterval(1000L, myTimerEvent); }

void loop() {

Blynk.run();

timer.run();

// Initiates BlynkTimer

}

Step 2: Blynk App

First go to the Application store on the Cell Phone.

In this example it was the Apple Store on an Iphone. It is known to work on Android.

Download the App.

Then add 8 Value Displays st individually set to a Virtual Pin. In this example I use pins V0-V7.

Follow the code to known which I set them to and labeled or view the images loaded.

Then get the Blynk Authencation code plug that into the code on Arduino.

Then put the Wifi and Password into the code.

Turn on your blynk sketch and run the arduino code.