#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define LED_PIN 25
#define SDA_PIN 21 // 0 wemosLolin32 I2C1
#define SCL_PIN 22 // 4 wemoslolin32 I2C1
// Setup Oled display 128x64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C //< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float BaroPres = 1010.12;
float Humidity = 56.10;
float Temperature = 23.32;
String TrendDisp = "langzaam ";
int TrendUp = 24; //arrow up
int TrendDown = 25; //arrow down
bool displayState = false;
unsigned long lastTime10sec = 0;
unsigned long timerDelay10sec = 10 * 1000; //10 sec delay
void initDisplay() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.setRotation(0); //0= no rotation,1=90deg clockwise,2=180deg upside down,3=90(270)deg anti-clockwise
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20, 30); //horz, vert
display.println("weerstation");
display.display();
delay(1000);
//display.clearDisplay();
//display.display();
}
void setupTime() {
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600; // 1h offset with GMT
const int daylightOffset_sec = 3600; //3600 = summertime, 0 = wintertime
// Init and get the time
//configTime(timezone, dst, "pool.ntp.org","time.nist.gov");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
display.println("\nWaiting for NTP...");
}
void displayTime() {
time_t now = time(nullptr);
struct tm *p_tm = localtime(&now);
int r = 35;
// Now draw the clock face
display.drawCircle(display.width() / 2, display.height() / 2, 2, WHITE);
//
//hour ticks
for (int z = 0; z < 360; z = z + 30) {
//Begin at 0° and stop at 360°
float angle = z;
angle = (angle / 57.29577951); //Convert degrees to radians
int x2 = (64 + (sin(angle) * r));
int y2 = (32 - (cos(angle) * r));
int x3 = (64 + (sin(angle) * (r - 5)));
int y3 = (32 - (cos(angle) * (r - 5)));
display.drawLine(x2, y2, x3, y3, WHITE);
}
// display second hand
float angle = p_tm->tm_sec * 6;
angle = (angle / 57.29577951); //Convert degrees to radians
int x3 = (64 + (sin(angle) * (r)));
int y3 = (32 - (cos(angle) * (r)));
display.drawLine(64, 32, x3, y3, WHITE);
//
// display minute hand
angle = p_tm->tm_min * 6;
angle = (angle / 57.29577951); //Convert degrees to radians
x3 = (64 + (sin(angle) * (r - 3)));
y3 = (32 - (cos(angle) * (r - 3)));
display.drawLine(64, 32, x3, y3, WHITE);
//
// display hour hand
angle = p_tm->tm_hour * 30 + int((p_tm->tm_min / 12) * 6);
angle = (angle / 57.29577951); //Convert degrees to radians
x3 = (64 + (sin(angle) * (r - 11)));
y3 = (32 - (cos(angle) * (r - 11)));
display.drawLine(64, 32, x3, y3, WHITE);
display.setTextSize(1);
display.setCursor((display.width() / 2) + 10, (display.height() / 2) - 3);
display.print(p_tm->tm_mday);
// update display with all data
display.display();
display.clearDisplay();
}
void displayData(){
display.clearDisplay();
//display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0); //horz, vert
display.print("Temperatuur ");
display.print(Temperature);
display.print(" C");
display.setCursor(0,14); //horz, vert
display.print("Vochtigheid ");
display.print(Humidity);
display.print(" %");
display.setCursor(0,28); //horz, vert
display.print("Barometer ");
display.print(BaroPres);
display.print(" hPa");
display.setCursor(0,42); //horz, vert
display.print("Comfort ");
//display.print(BaroPres);
display.print("prima");
display.setCursor(0,56); //horz, vert
display.print("Trend ");
display.print(TrendDisp);
display.print((char)TrendDown);
display.display();
}
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
initDisplay();
setupTime();
// configure Arduino LED pin for output
pinMode(LED_PIN, OUTPUT);
}
void loop() {
//displayTime();
if (displayState == false) {
displayTime();
digitalWrite(18, HIGH); // activity led
}
if (displayState == true) {
displayData();
digitalWrite(18, LOW);
}
if ((millis() - lastTime10sec) > timerDelay10sec) {
display.clearDisplay();
display.display();
displayState = !displayState;
lastTime10sec = millis();
}
}