here the code as text.
To watch the serial port I m using a program called "device monitoring studio" which shows the communication of the com-port.
//
#include <UTFT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <dht.h>
#define DHT11_PIN 7 // DHT11 data pin
#define heat_pin 3
OneWire ds(2);
dht DHT;
extern uint8_t SmallFont[]; // Declare which fonts we will be using
// Usage: myGLCD(, SDA, SCL, CS, RST[, RS]);
// connection of the TFT pins to ARDUINO pins
// GND
// BL +3.3
// VCC
// CLK 13
// DIN 11
// DC 9
// CS 10
// RST 8
UTFT myGLCD(ST7735, 11, 13, 10, 8, 9);
// used variables
String inputString = "";
bool stringComplete = false;
int delta = 2; // temperature delta mirrow vs. dew point in °C
int scope = 0; // index for the DS18x20 (mirror/scope temperature
int chk = 0; // for the DHT11 requests
float temperatureAir, humidity, temperatureScope, dewPoint;
// heating on/off
int heating_on = 0;
void setup()
{
Serial.begin(19200);
pinMode(heat_pin, OUTPUT);
// Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(SmallFont); //SmallFont
int buf[158];
int x, x2;
int y, y2;
int r;
//Clear the screen and draw
myGLCD.clrScr();
delay(100);
myGLCD.setColor(30, 30, 30);
myGLCD.fillRect(0, 0, 159, 13);
myGLCD.setColor(30, 30, 30);
myGLCD.fillRect(0, 114, 159, 127);
myGLCD.setColor(0, 0, 255);
myGLCD.setBackColor(30, 30, 30);
myGLCD.print("Dew Controller v0.1", CENTER, 1);
myGLCD.setBackColor(30, 30, 30);
myGLCD.setColor(0, 0, 255);
}
void check_heating( float TS, float TA) {
// 127 = 50%
// 191 = 75%
// 255 = 100&
float diff = TS - (TA + delta);
if (TS < (TA + delta)) {
if (diff < -2.00) {
analogWrite(heat_pin, 255);
}
else if (diff < -1.25) {
analogWrite(heat_pin, 200);
}
else if (diff < -0.75) {
analogWrite(heat_pin, 170);
}
else if (diff < -0.45) {
analogWrite(heat_pin, 65);
}
else {
analogWrite(heat_pin, 35);
}
heating_on = 1;
} else if (diff < 0.15) {
analogWrite(heat_pin, 20);
heating_on = 1;
}
else {
analogWrite(heat_pin, LOW);
heating_on = 0;
}
}
double calcDewPoint(double celsius, double humidity)
{
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP / 0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// Fields are in order:
// temperature ch 1
// temperature ch 2
// temperature ambient
// relative humidity
// dew point
// output ch 1
// output ch 2
// output ch 3
// calibration ch 1
// calibration ch 2
// calibration ambient
// threshold ch 1
// threshold ch 2
// auto mode
// outputs ch 2 & 3 linked
// aggressivity
// "##" + String(temperatureScope) + "/11.11/" + String(temperatureAir) + "/" + String(humidity) + "/" + String(dewPoint) + "/0/0/0/0/0/0/2/2/0/0/4**"
void loop()
{
chk = DHT.read11(DHT11_PIN);
temperatureAir = DHT.temperature;
humidity = DHT.humidity;
temperatureScope = getTemperature();
dewPoint = calcDewPoint(temperatureAir, humidity) ;
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(90, 14, 159, 100);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(0, 0, 255);
myGLCD.print("Temperatur: ", 5, 15);
myGLCD.print(String(temperatureAir) + "$C", RIGHT, 15);
myGLCD.print("Feuchte: ", 5, 27);
myGLCD.print(String(humidity) + " %", RIGHT, 27);
myGLCD.print("Taupunkt: ", 5, 39);
myGLCD.print(String(dewPoint) + "$C", RIGHT, 39);
myGLCD.print("Teleskop: ", 5, 51);
myGLCD.print(String(temperatureScope) + "$C", RIGHT, 51);
myGLCD.print("Delta: ", 5, 63);
if ((temperatureScope - dewPoint) < 0 ) {
myGLCD.setBackColor(0, 0, 255);
myGLCD.setColor(255, 255, 255);
}
else {
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(0, 0, 255);
}
myGLCD.print(String(temperatureScope - dewPoint) + "$C", RIGHT, 63);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setColor(0, 0, 255);
myGLCD.print("Heizung: ", 5, 87);
check_heating(temperatureScope, dewPoint); //ändern in Dew point
if (heating_on == 0) myGLCD.print("off ", RIGHT, 87);
if (heating_on == 1) myGLCD.print(" on ", RIGHT, 87);
if (stringComplete) {
Serial.println("##" + String(temperatureScope) + "/11.11/" + String(temperatureAir) + "/" + String(humidity) + "/" + String(dewPoint) + "/0/0/0/0/0/0/2/2/0/0/4**");
// clear the string:
inputString = "";
stringComplete = false;
}
}
delay(1000);
}
void serialEvent()
{
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
if (inputString.length() == 6) {
stringComplete = true;
}
}
}
float getTemperature() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
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*, 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;*
- }*
- delay(300);*
- 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 = ds.read();_
_ // Serial.print(data*, 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;
// fahrenheit = celsius * 1.8 + 32.0;
return celsius;
}*_