#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include "DHT.h"
#include "debug.h"
//#include "ProfileTimer.h"
// Define which pins are used by the sensors
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT mydht(DHTPIN, DHTTYPE);
long temperTimeoutCount;
#define TEMPER_TIMEOUT_CONST 5000ul
typedef struct {
// DHT *sens;
float tempC;
float humid;
float heati;
} dhtMeas_t;
dhtMeas_t sensDHT22;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // initalize the display
/*
const int NUM_PARTICLES = 2; // number of particles - more than 20 particles will not fit into Arduino UNO RAM
float particle_x[NUM_PARTICLES]; // particle X position
float particle_y[NUM_PARTICLES]; // particle Y position
int particle_speed_x[NUM_PARTICLES]; // particle X speed
int particle_speed_y[NUM_PARTICLES]; // particle Y speed
float particle_size[NUM_PARTICLES]; // particle size (radius)
int init_spread = 15; // how much could be new particle away from the center
*/
int time_value = 0; // time value to animate some elements and update the charge value slower
int charge_value = 0; // charging value displayed in the middle of the screen
char charge_value_buffer[10]; // charging integer value converted to C style string
unsigned long millis_time; // fps
unsigned long millis_time_last; // fps
int fps; // actual FPS value
char strbuf[10]; // buffer for converting FPS to string
/*
void readDHT(DHT *dht, int *const current_temp) {
dht->read();
*current_temp = dht->readTemperature() * 10.0;
}
*/
/* =======================================================================
* = =
* =======================================================================
*/
void readDHT(dhtMeas_t* sns) {
sns->tempC = mydht.readTemperature();
//sns->humid = mydht.readHumidity();
// sns->heati = mydht.computeHeatIndex(sns->tempC, sns->humid, false);
/*
DHT *pdht = sns->sens;
pdht->read();
sns->tempC = pdht->readTemperature();
//sns->humid = sns->sens->readHumidity();
//sns->heati = sns->sens->computeHeatIndex(sns->tempC, sns->humid, false);
*/
}
/* =======================================================================
* = =
* =======================================================================
*/
void printDHT(dhtMeas_t* sns) {
char buf[10];
SerialPrintf("[DHT22] %s°C", dtostrf(sns->tempC,5,1,buf));
SerialPrintf(", %s%%", dtostrf(sns->humid,5,1,buf));
SerialPrintf(", %s°C\r\n", dtostrf(sns->heati,5,1,buf));
}
/* =======================================================================
* = =
* =======================================================================
*/
void setup(void)
{
Serial.begin(115200);
u8g2.begin(); // initialize u8g2 drawing
u8g2.setFont(u8g2_font_chargen_92_tr); // set U8G font
/*
for (int i=0; i < NUM_PARTICLES; i++) { // initialize all the particles
particle_x[i] = random(64-init_spread,64+init_spread); // set the position to the center of the screen +- spread
particle_y[i] = random(32-init_spread,32+init_spread); // set the position to the center of the screen +- spread
particle_speed_x[i] = random(-5, 6) * 3; // set some random speed for X
particle_speed_y[i] = random(-5, 6) * 3; // set some random speed for Y
particle_size[i] = 8; // set a big enough radius
}
*/
mydht.begin();
//sensDHT22.sens = &dht;
sensDHT22.tempC = 0.0;
sensDHT22.humid = 0.0;
sensDHT22.heati = 0.0;
SerialPrintf("[SYSRAM] Free list: %d ,free RAM: %d\r\n",freeListSize(),freeMemory());
//readDHT(&sensDHT22);
//printDHT(&sensDHT22);
}
/* =======================================================================
* = =
* =======================================================================
*/
void loop(void)
{
if(millis() - temperTimeoutCount > TEMPER_TIMEOUT_CONST) {
temperTimeoutCount = millis();
//readDHT(&sensDHT22);
printDHT(&sensDHT22);
}
time_value = time_value+1; // increment the time value
if (time_value % 10 == 0) { // only perform some calculations every Nth frame
// increment the charge value
charge_value = charge_value + 1;
if (charge_value > 100) {
charge_value = 0; // set charge value back to 0
}
//sprintf(charge_value_buffer, "%d%%", charge_value); // convert charge value to string with the % symbol
sprintf(charge_value_buffer, "%s°C", dtostrf(sensDHT22.tempC,5,1,strbuf)); // convert charge value to string with the % symbol
}
u8g2.clearBuffer(); // clear U8G2 drawing buffer
u8g2.setDrawColor(1); // white color
// draw particles --------------
/*
for (int i=0; i < NUM_PARTICLES; i++) { // go over all the particles
particle_x[i] = particle_x[i] + particle_speed_x[i]/10.0; // update the X position based on the speed
particle_y[i] = particle_y[i] + particle_speed_y[i]/10.0; // update the Y position based on the speed
particle_size[i] = particle_size[i] * 0.95; // make the size slightly smaller
u8g2.drawDisc(particle_x[i], particle_y[i], particle_size[i]); // draw the particle
// if the particle is outside the screen or small enough, re-initialize it again
if ((particle_x[i] > 128) || (particle_x[i] < 0) || (particle_y[i] > 64) || (particle_y[i] < 0) || (particle_size[i] < 0.5)) {
particle_x[i] = random(64-init_spread,64+init_spread); // set the position to the center of the screen +- spread
particle_y[i] = random(32-init_spread,32+init_spread); // set the position to the center of the screen +- spread
particle_speed_x[i] = random(-5, 6) * 3; // set some random speed for X
particle_speed_y[i] = random(-5, 6) * 3; // set some random speed for Y
particle_size[i] = 8; // set a big enough radius
}
}
*/
// draw big circle in the middle --------------
byte radius = round(25.0 + sin(time_value / 10.0)*2.0); // radius for the big circle in the middle
u8g2.drawDisc(64, 32, radius); // draw the big circle
// draw FPS --------------
u8g2.setFont(u8g2_font_6x10_tf); // set very small font
itoa(fps, strbuf, 10); // convert FPS number to string
u8g2.drawStr(0, 10, strbuf); // draw the FPS number
// draw big value in the center --------------
u8g2.setDrawColor(0); // black color
u8g2.setFont(u8g2_font_chargen_92_tr); // set U8G font
byte string_width = u8g2.getStrWidth(charge_value_buffer); // calculate the string width
u8g2.drawStr(64 - (string_width / 2), 40, charge_value_buffer); // draw the charge value string in the middle of the screen
u8g2.sendBuffer(); // send U8G2 buffer to the display
// calculate the FPS
millis_time_last = millis_time; // store last millisecond value
millis_time = millis(); // get millisecond value from the start of the program
fps = round(1000.0/ (millis_time*1.0-millis_time_last)); // calculate FPS (frames per second) value
}