Weatherstation2 — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by szaboj0924.

Components

Sketch code

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <time.h>
#include <DHT.h>
#include <varoskep.h>
#include <moon.h>
#include <space.h>
#include <nameday.h>

// =====================================================
// HARDVERBEÁLLÍTÁSOK
// =====================================================

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32

#define OLED_ADDRESS 0x3C

#define SDA_PIN 21
#define SCL_PIN 22

#define DHTPIN 19
#define DHTTYPE DHT11

#define MODE_SWITCH 23

// =====================================================
// WIFI
// =====================================================

const char* ssid = "test";
const char* password = "test";

// Tárkány koordinátái
const float LATITUDE = 47.72;
const float LONGITUDE = 18.49;

// =====================================================
// IDŐZÍTÉSEK
// =====================================================

const unsigned long WEATHER_INTERVAL = 600000UL; // 10 perc
const unsigned long DHT_INTERVAL = 5000UL;        // 5 másodperc
unsigned long lastAnimation = 0;
uint8_t animFrame = 0;
uint8_t spaceFrame = 0;
unsigned long lastAnimUpdate = 0;

// =====================================================
// OBJEKTUMOK
// =====================================================

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  -1
);

DHT dht(DHTPIN, DHTTYPE);

// =====================================================
// GLOBÁLIS VÁLTOZÓK
// =====================================================

String sunriseTime = "";
String sunsetTime = "";

bool oledOk = false;
bool wifiTest = true;

bool lastButtonState = HIGH;
bool currentButtonState = HIGH;

unsigned long lastDHTRead = 0;
unsigned long lastWeatherUpdate = 0;

float insideTemp = 0.0;
int insideHumidity = 0;

float temperature = 0.0;
int humidity = 0;

int frame = 0;

// =====================================================
// ENUMOK
// =====================================================

enum WeatherType
{
  SUNNY,
  CLOUDY,
  RAINY,
  SNOWY
};

enum ScreenMode
{
  SCREEN_NORMAL,
  SCREEN_CITY,
  SCREEN_MOON,
  SCREEN_NAME
};

ScreenMode screenMode = SCREEN_NORMAL;

WeatherType weather = SUNNY;

bool forceMoonPhase = false;
MoonPhase testMoonPhase = FULL_MOON;

// =====================================================
// HOLDÁZIS
// =====================================================

MoonPhase getMoonPhase()
{
  if (forceMoonPhase)
  {
    return testMoonPhase;
  }

  struct tm timeinfo;

  if (!getLocalTime(&timeinfo))
  {
    return FULL_MOON;
  }

  int year = timeinfo.tm_year + 1900;
  int month = timeinfo.tm_mon + 1;
  int day = timeinfo.tm_mday;

  if (month < 3)
  {
    year--;
    month += 12;
  }

  month++;

  long c = 365.25 * year;
  long e = 30.6 * month;

  double jd = c + e + day - 694039.09;
  jd /= 29.5305882;

  double phase = jd - (int)jd;

  int index = (int)(phase * 8 + 0.5);
  index &= 7;

  return (MoonPh