#include <DHT.h>
#include <OneWire.h>
#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library for drawing on OLED display - needs to be installed in Arduino IDE first
#include <Wire.h> // wire library for IIC communication with the OLED display
// U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0); // set the OLED display to 128x64px, HW IIC, no rotation, used in WOKWI
U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0); // final display, 128x128px [page buffer, size = 128 bytes], HW IIC connection
#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
String agua;
String fora;
OneWire ds(3);
int center_x = 64;
int center_y = 64;
int center_x1 = 31;
int center_x2 = 97; // display x center, 64px for the 128x128px display
int center_y1 = 80; // display y center, 64px for the 128x128px display
void setup(void) {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
u8g2.begin(); // begin the u8g2 library
Wire.begin();
}
//--------------------- OLED ---------------*/
void draw_background() {
float xpos;
float ypos;
float xpos2;
float ypos2;
u8g2.setDrawColor(1);
u8g2.drawCircle(center_x1, center_y1, 30, U8G2_DRAW_ALL);
u8g2.drawCircle(center_x2, center_y1, 30, U8G2_DRAW_ALL);
for (int i=0; i<30; i++) { // draw 60 pixels around the circle
xpos = round(center_x1 + sin(radians(i * 3)) * 30); // calculate x pos based on angle and radius
ypos = round(center_y1 - cos(radians(i * 3)) * 30); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
for (int i=0; i<30; i++) { // draw 60 pixels around the circle
xpos = round(center_x2 + sin(radians(i * 3)) * 30); // calculate x pos based on angle and radius
ypos = round(center_y1 - cos(radians(i * 3)) * 30); // calculate y pos based on angle and radius
u8g2.drawPixel(xpos,ypos); // draw white pixel on position xpos and ypos
}
u8g2.setFont(u8g2_font_prospero_bold_nbp_tf);
u8g2.drawStr(20,20,"Temperatura");
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(20,65,"Agua");
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(86,65,"Fora");
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.setCursor( 15, 80);
u8g2.print(agua + " C");
u8g2.setCursor( 83, 80);
u8g2.print(fora + " C");
}
// ---------------------------- DS -----------------------
void loop () {
// DHT
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
fora = String(t);
// Check if any reads failed and exit early (to try again).
if (isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
byte i;
byte present = 0;
byte type_s;
byte data[9];
byte addr[8];
float celsius;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
agua = String(celsius);
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
u8g2.firstPage();
do {
draw_background();
} while ( u8g2.nextPage() );
}
Loading
ds18b20
ds18b20
Loading
grove-oled-sh1107
grove-oled-sh1107