/*
Temperature receiver
Displays temperature and Humidity internal and external
Carlo Stramaglia
04-Feb-2024
https://www.youtube.com/@CarloStramaglia
*/
#include <RCSwitch.h>
#include <DHT_U.h>
#include <U8glib.h>
#include <OneButton.h>
#define DHTPIN 3
#define DHTTYPE DHT22
RCSwitch mySwitch = RCSwitch();
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
OneButton button(A1, true);
int tmp;
int checkavail=0;
int EstInt=0; // If 0 show internal while if 1 show external temperature
char temp[7];
char umid[7];
char tempe[7];
char umide[7];
//float flt = 2.75;
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
u8g.setColorIndex(1);
// button.attachClick(click);
//attachInterrupt(digitalPinToInterrupt(5), checkTicks, CHANGE);
button.attachLongPressStop(longPressStop);
dht.begin();
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
Serial.println(F("------------------------------------"));
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 2000;
}
void loop() {
button.tick();
if (mySwitch.available()) {
tmp = mySwitch.getReceivedValue();
if (mySwitch.getReceivedBitlength() == 24) {
Serial.print("External Temperature: ");
checkavail=0; // Reset the value
dtostrf(float(tmp)/10, 2, 1, tempe);
Serial.print("Arriving: ");
Serial.println(tmp);
if (tmp < 100 and tmp >= 0)
tempe[3]=byte(0x20);
tempe[4]=byte(0x20);
if (tmp < (-32758+100)) {
tempe[3]=byte(0x20);
tmp=(tmp+32768);
dtostrf(float(tmp)/10, 2, 1, tempe);
tempe[4] = tempe[3]; // Added 4 Feb 2024
tempe[3] = tempe[2];
tempe[2] = tempe[1];
tempe[1] = tempe[0];
tempe[0] = '-';
tempe[4]=byte(0x20);
}
if (tmp < 0 and tmp >= (-32758+100)) {
tmp=(tmp+32768);
dtostrf(float(tmp)/10, 2, 1, tempe);
tempe[4] = tempe[3]; // Added 4 Feb 2024
tempe[3] = tempe[2];
tempe[2] = tempe[1];
tempe[1] = tempe[0];
tempe[0] = '-';
//tempe[4]=byte(0x20);
}
//tempe[4]=byte(0xb0);
tempe[5]='C';
tempe[6]='\0';
Serial.println(tempe);
}
else {
Serial.print("External Humidity: ");
dtostrf(float(tmp)/10, 2, 1, umide);
Serial.print(umide);
Serial.println("%");
umide[4]=' ';
umide[5]='%';
umide[6]='\0';
}
mySwitch.resetAvailable();
}
else {
checkavail++;
if (checkavail==100) {
String notav = "NOT AV";
strncpy(tempe, notav.c_str(), sizeof(tempe));
tempe [sizeof(tempe) - 1] = 0;
strncpy(umide, notav.c_str(), sizeof(umide));
umide [sizeof(umide) - 1] = 0;
checkavail=0;
}
}
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Internal Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
dtostrf(event.temperature, 2, 1, temp);
temp[4]=byte(0xb0);
temp[5]='C';
temp[6]='\0';
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Internal Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
dtostrf(event.relative_humidity, 2, 1, umid);
umid[4]=' ';
umid[5]='%';
umid[6]='\0';
}
if (EstInt==0) {
u8g.firstPage();
do {
draw("In Temp:", 0, 14);
draw(temp, 70, 14);
draw("In Humi:", 0, 32);
draw(umid, 70, 32);
} while( u8g.nextPage() );
}
else {
u8g.firstPage();
do {
draw("Ex Temp:", 0, 14);
draw(tempe, 70, 14);
draw("Ex Humi:", 0, 32);
draw(umide, 70, 32);
} while( u8g.nextPage() );
}
}
void longPressStop() {
Serial.println("Button click.");
u8g.firstPage();
do {
draw("Please Wait...", 0, 23);
} while( u8g.nextPage() );
if (EstInt == 0)
EstInt=1;
else
EstInt=0;
}
void draw(char* parola, int posx, int posy) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);
//u8g.setFont(u8g_font_osb21);
u8g.drawStr( posx, posy, parola);
}
Comments