188 lines
4.6 KiB

7 years ago
//Define DEBUG to get the Output from DEBUG_PRINTLN
#define DEBUG 1
#include <Arduino.h>
7 years ago
//Include Basecamp in this sketch
#include <Basecamp.hpp>
#include <Configuration.hpp>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <WiFiUdp.h>
#include <NTPClient.h>
7 years ago
#include "main.h"
7 years ago
#include "hardware.h"
#include "mp3.h"
7 years ago
#include "BME280.h"
#include "rotary.h"
#include "screen.h"
7 years ago
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 15, /* data=*/ 4);
7 years ago
//Create a new Basecamp instance called iot
Basecamp iot;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000);
char timeStr[20];
char weatherStr[32];
7 years ago
BME280 bme280;
MP3 mp3;
Rotary rotary;
Screen* screen;
7 years ago
7 years ago
menuType menuChange = eNone;
uint32_t lastButtonPress = 0;
uint32_t lastUpdate = 0;
7 years ago
uint32_t lastTransmit = 0;
7 years ago
//Variables for the mqtt packages and topics
uint16_t statusPacketIdSub = 0;
7 years ago
String commandTopic;
7 years ago
String bme280Topic;
7 years ago
void setup() {
7 years ago
// gpio configuration
7 years ago
pinMode(buttonPin, INPUT_PULLUP);
pinMode(18, OUTPUT);
digitalWrite(18, HIGH); // disable LoRa_CS
7 years ago
//Initialize Basecamp
iot.begin();
u8g2.begin();
7 years ago
// delay(50);
screen = new WelcomeScreen();
screen->draw();
menuChange = eMainScreen;
u8g2.setContrast(127);
bme280.begin();
bme280.printValues();
7 years ago
//Configure the MQTT topics
7 years ago
commandTopic = "esp32-node/cmd/" + iot.hostname + "/play";
bme280Topic = "esp32-node/stat/" + iot.hostname + "/bme280";
7 years ago
//Set up the Callbacks for the MQTT instance. Refer to the Async MQTT Client documentation
// TODO: We should do this actually _before_ connecting the mqtt client...
iot.mqtt.onConnect(onMqttConnect);
7 years ago
//iot.mqtt.onPublish(NULL);
7 years ago
iot.mqtt.onMessage(onMqttMessage);
while (iot.wifi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
7 years ago
screen->draw();
}
IPAddress localIP = iot.wifi.getIP();
char ipStr[16];
localIP.toString().toCharArray(ipStr, 16);
timeClient.begin();
timeClient.update();
mp3.begin();
rotary.registerCallback(rotation);
rotary.begin(rotaryPinA, rotaryPinB, rotaryPinButton);
}
7 years ago
//This function is called when the MQTT-Server is connected
void onMqttConnect(bool sessionPresent) {
DEBUG_PRINTLN(__func__);
//Subscribe to the delay topic
7 years ago
iot.mqtt.subscribe(commandTopic.c_str(), 0);
7 years ago
//Trigger the transmission of the current state.
transmitStatus();
7 years ago
lastTransmit = millis();
7 years ago
}
void transmitStatus() {
DEBUG_PRINTLN(__func__);
7 years ago
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["temperature"] = bme280.readTemperature();
root["humidity"] = bme280.readHumidity();
root["pressure"] = bme280.readPressure();
char sensorBuf[root.measureLength()+1];
root.printTo(sensorBuf, sizeof(sensorBuf));
statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 0, false, sensorBuf);
// statusPacketIdSub = iot.mqtt.publish(bme280Topic.c_str(), 1, true, sensorBuf);
7 years ago
}
7 years ago
7 years ago
//This topic is called if an MQTT message is received
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
DEBUG_PRINTLN(__func__);
//Since we only subscribed to one topic, we only have to compare the payload
7 years ago
if (strcmp(payload, "ON") == 0) {
mp3.start();
} else if (strcmp(payload, "OFF") == 0) {
mp3.stop();
7 years ago
}
}
7 years ago
void suspend() {
DEBUG_PRINTLN("Entering deep sleep");
esp_sleep_enable_ext0_wakeup((gpio_num_t)sensorPin, 0);
//properly disconnect from the MQTT broker
iot.mqtt.disconnect();
//send the ESP into deep sleep
esp_deep_sleep_start();
7 years ago
}
void rotation(int i, int direction, int buttonPressed) {
7 years ago
if(millis() - lastButtonPress >= 300) {
lastButtonPress = millis();
if (buttonPressed == 1) screen->select();
}
if (direction == 1) screen->next();
else if (direction == -1) screen->previous();
}
7 years ago
void loop()
{
if(millis() - lastUpdate >= 1000) {
lastUpdate = millis();
timeClient.update();
timeClient.getFormattedTime().toCharArray(timeStr, 50);
sprintf(weatherStr, "%.1f°C %.1f%% %.0fhPa", bme280.readTemperature(), bme280.readHumidity(), bme280.readPressure());
}
7 years ago
if(millis() - lastTransmit >= 60000) {
lastTransmit = millis();
transmitStatus();
}
7 years ago
if (menuChange != eNone) {
delete screen;
if (menuChange == eMainScreen) screen = new MainScreen();
else if (menuChange == eMainMenu) screen = new MainMenu();
else if (menuChange == eStationMenu) screen = new StationMenu();
else screen = new MainScreen();
7 years ago
menuChange = eNone;
}
screen->draw();
7 years ago
delay(100);
}