initial commit
This commit is contained in:
commit
9bc3fc5910
11 changed files with 358 additions and 0 deletions
19
src/config.hpp
Normal file
19
src/config.hpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
/***********************************************
|
||||
* Variants *
|
||||
***********************************************/
|
||||
// @note You can use -D flag to define the variant and comment out the line below
|
||||
// #define AC_TYPE AC_TYPE_CEILING
|
||||
|
||||
/***********************************************
|
||||
* Pin Definitions *
|
||||
***********************************************/
|
||||
#define TEMP_SENSOR_PIN 32
|
||||
|
||||
/***********************************************
|
||||
* Display Configuration *
|
||||
***********************************************/
|
||||
// UART Configuration
|
||||
#define INTERNAL_DISPLAY_UART Serial
|
||||
#define INTERNAL_DISPLAY_BAUDRATE 115200
|
105
src/main.cpp
Normal file
105
src/main.cpp
Normal file
|
@ -0,0 +1,105 @@
|
|||
#include <main.hpp>
|
||||
/************************************************
|
||||
* Global Variables *
|
||||
************************************************/
|
||||
ESPMegaPRO espmega = ESPMegaPRO();
|
||||
ESPMegaDisplayOTA internalDisplayOTA = ESPMegaDisplayOTA();
|
||||
|
||||
DHTNEW dht(TEMP_SENSOR_PIN);
|
||||
|
||||
/************************************************
|
||||
* End of Global Variables *
|
||||
************************************************/
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize both displayports
|
||||
INTERNAL_DISPLAY_UART.begin(INTERNAL_DISPLAY_BAUDRATE);
|
||||
ESP_LOGI("CUD IoT OS", "Starting Initialization");
|
||||
send_stop_bytes(INTERNAL_DISPLAY_UART);
|
||||
INTERNAL_DISPLAY_UART.print("rest");
|
||||
send_stop_bytes(INTERNAL_DISPLAY_UART);
|
||||
// Wait for the display to reset
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
// Initialize the ESPMegaPRO
|
||||
ESP_LOGV("CUD IoT OS", "Initializing ESPMegaPRO");
|
||||
espmega.begin();
|
||||
// Initialize IoT Modules
|
||||
ESP_LOGV("CUD IoT OS", "Initializing IoT Modules");
|
||||
espmega.setTimezone("ICT-7");
|
||||
espmega.enableIotModule();
|
||||
// Setup Ethernet
|
||||
ESP_LOGV("CUD IoT OS", "Initializing Ethernet");
|
||||
ETH.begin();
|
||||
ESP_LOGD("CUD IoT OS", "Binding Ethernet Interface");
|
||||
espmega.iot->bindEthernetInterface(Ð);
|
||||
// Connect to Network
|
||||
ESP_LOGV("CUD IoT OS", "Connecting to Network");
|
||||
espmega.iot->loadNetworkConfig();
|
||||
espmega.iot->connectNetwork();
|
||||
// Connect to the MQTT Broker
|
||||
ESP_LOGV("CUD IoT OS", "Connecting to MQTT Broker");
|
||||
espmega.iot->loadMqttConfig();
|
||||
vTaskDelay(3000 / portTICK_PERIOD_MS); // Wait for the network to stabilize
|
||||
espmega.iot->connectToMqtt();
|
||||
// Enable Web Server
|
||||
ESP_LOGV("CUD IoT OS", "Enabling Web Server");
|
||||
espmega.enableWebServer(80);
|
||||
// Initialize Internal Display
|
||||
ESP_LOGV("CUD IoT OS", "Initializing Internal Display");
|
||||
espmega.enableInternalDisplay(&INTERNAL_DISPLAY_UART);
|
||||
// Register all cards with iot
|
||||
ESP_LOGV("CUD IoT OS", "Registering Cards with IoT");
|
||||
espmega.iot->registerCard(0);
|
||||
espmega.iot->registerCard(1);
|
||||
// Input callbacks
|
||||
// This pre-load the input buffers
|
||||
// We need to do this to prevent switches that are left on the "on" position from triggering the callback
|
||||
ESP_LOGV("CUD IoT OS", "Pre-loading input buffers");
|
||||
espmega.inputs.loop();
|
||||
espmega.inputs.registerCallback(handle_input_change);
|
||||
espmega.iot->registerMqttCallback(handle_mqtt_message);
|
||||
internalDisplayOTA.begin("/intdisp", espmega.display, espmega.webServer);
|
||||
// Setup the temperature sensor
|
||||
ESP_LOGI("CUD IoT OS", "Initialization Complete");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static uint32_t lastTempUpdate = 0;
|
||||
if (millis() - lastTempUpdate > 5000)
|
||||
{
|
||||
if(dht.read() != DHTLIB_OK)
|
||||
{
|
||||
ESP_LOGE("CUD IoT OS", "Failed to read temperature and humidity");
|
||||
return;
|
||||
}
|
||||
|
||||
float temp = dht.getTemperature();
|
||||
float hum = dht.getHumidity();
|
||||
char buffer[50];
|
||||
snprintf(buffer, 50, "%.2f", temp);
|
||||
espmega.iot->publishRelative("temperature", buffer);
|
||||
snprintf(buffer, 50, "%.2f", hum);
|
||||
espmega.iot->publishRelative("humidity", buffer);
|
||||
lastTempUpdate = millis();
|
||||
}
|
||||
espmega.loop();
|
||||
}
|
||||
|
||||
void send_stop_bytes(HardwareSerial &uart)
|
||||
{
|
||||
uart.write(0xFF);
|
||||
uart.write(0xFF);
|
||||
uart.write(0xFF);
|
||||
}
|
||||
|
||||
void handle_input_change(uint8_t pin, bool state)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
void handle_mqtt_message(char *topic, char *payload)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
25
src/main.hpp
Normal file
25
src/main.hpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* @file main.cpp
|
||||
* @author Siwat Sirichai (siwat@siwatinc.com)
|
||||
* @brief CU Demonstration Secondary School IoT 2024 Project Main File
|
||||
* @version 1.0
|
||||
* @date 2024-03-19
|
||||
*
|
||||
* @copyright Copyright (c) SIWAT SYSTEM 2024
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#include <ESPMegaProOS.hpp>
|
||||
#include <ETH.h>
|
||||
#include <ESPMegaDisplayOTA.hpp>
|
||||
#include <dhtnew.h>
|
||||
#include "config.hpp"
|
||||
|
||||
/***********************************************
|
||||
* Function Prototypes *
|
||||
***********************************************/
|
||||
void setup();
|
||||
void loop();
|
||||
void send_stop_bytes(HardwareSerial &uart);
|
||||
void handle_input_change(uint8_t pin, bool state);
|
||||
void handle_mqtt_message(char *topic, char *payload);
|
Loading…
Add table
Add a link
Reference in a new issue