Ben bezig met een weerstation en plantenkas besturing in 1. nu heb ik een UNO in de kas voor metingen en besturing. deze zend de waardes via rs485 naar een mega wat mijn display binnen is. nu loopt de mega met ongeveer 4 uur vast en de uno ook maar volgens mij nog sneller. bij de mega met display vermoed ik dat het met de rtc te maken heeft maar kom er niet uit. bij de UNO dacht ik door de combinatie van serial comunicatie naar de mega maar ook veel debug serial prints. maar deze uitschakelen heeft totaal geen invloed.
wie kan mij helpen? het word allemaal nog veel verder uitgeboud maar met deze vastlopers er in kan ik wel doorgaan maar word het alleen maar moeilijker de problemen op te sporen natuurlijk.
Dit is de mega met display:
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <TFT_HX8357.h>
TFT_HX8357 tft = TFT_HX8357(); // Invoke custom library
int buttonVal = 0;
float buttonCount = 0;
float previoushumkas = 10;
float previoustempkas = 10;
float previoushumbuiten = 0.1;
float previoustempbuiten = 0.1;
float humbuiten = 00.0;
float tempbuiten = 00.0;
float humkas = 00.0;
float tempkas = 00.0;
float bodem;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
float tempC = 00.0;
boolean newData = false;
//============
//Receiver code
byte greenLED = 12;
char cString[20];
byte chPos = 0;
byte ch = 0;
char dataStr[6];
ThreeWire myWire(8,9,3); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
void setup()
{
pinMode(2,OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(A0,INPUT_PULLUP);
Serial.begin(9600); //setup software serial
Serial1.begin(9600);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
delay(100);
digitalWrite(2, LOW);
pinMode(greenLED, OUTPUT);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
printUI();
Serial.begin(9600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
//============
void loop()
{
//digitalWrite(4, HIGH);
//delay(5000);
//digitalWrite(5, HIGH);
//delay(5000);
//digitalWrite(6, HIGH);
//delay(5000);
//digitalWrite(7, HIGH);
//delay(500);
//digitalWrite(7,LOW);
//digitalWrite(4, LOW);
//delay(5000);
//digitalWrite(5, LOW);
//delay(5000);
//digitalWrite(6, LOW);
//
digitalWrite(2, LOW);
recvWithStartEndMarkers();
if (newData == true)
{
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
printKasTemperature();
printKasHumidity();
printRemoteTemperature();
printRemoteHumidity();
time();
//Button();
}
//============
void Button()
{
//buttonVal = digitalRead(A0);
//if ((buttonVal = HIGH) && (buttonCount <3));
//{buttonCount +1;
//digitalWrite(4,LOW);
//}
//if ((buttonVal = HIGH) && (buttonCount == 3));
// {buttonCount = 0;
// digitalWrite(4,HIGH);
// }
Serial.println (buttonCount);
// Serial.println(buttonVal);
buttonVal = digitalRead(A0);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonVal == HIGH && buttonCount <=3) {
// turn LED on:
digitalWrite(4, HIGH);
buttonCount = buttonCount +1;
} else{
// turn LED off:
digitalWrite(4, LOW);
}
if (buttonVal == HIGH && buttonCount ==3) {
// turn LED on:
digitalWrite(4, HIGH);
buttonCount = 0;
} else{
// turn LED off:
digitalWrite(4, LOW);
}
// Serial.println(buttonState);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false)
{
rc = Serial1.read();
delay(20);
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
//============
void parseData()
{
Serial.print(F("Received: "));
Serial.println(tempChars);
// split the data into its parts
char* strtokIndx; // this is used by strtok() as an index
// strtokIndx = strtok(tempChars,","); // get the first part - the string
// strcpy(humbuiten, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
humbuiten = atof(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
tempbuiten = atof(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
humkas = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
tempkas = atof(strtokIndx); // convert this part to a floa
strtokIndx = strtok(NULL, ",");
bodem = atof(strtokIndx); // convert this part to a floa
}
//============
void showParsedData()
{
Serial.print("humbuiten ");
Serial.println(humbuiten);
Serial.print("tempbuiten ");
Serial.println(tempbuiten);
Serial.print("humkas ");
Serial.println(humkas);
Serial.print("bodem vocht ");
Serial.println(bodem);
}
void printUI()
{
tft.drawRoundRect(5,5,470,71,5,TFT_WHITE); //datum en tijd
tft.drawRoundRect(6,6,470,71,5,TFT_WHITE);
tft.drawRoundRect(5,90,220,225,5,TFT_WHITE); //remote
tft.drawRoundRect(6,91,220,225,5,TFT_WHITE);
tft.drawRoundRect(250,90,220,225,5,TFT_WHITE); //indoor
tft.drawRoundRect(251,91,220,225,5,TFT_WHITE);
tft.fillRect(26,90,180,40,TFT_GREEN); //remote
tft.fillRect(270,90,180,40,TFT_CYAN); //indoor
tft.setCursor(62,100);
tft.setTextColor(TFT_BLACK);
tft.setTextSize(3);
tft.print("Buiten");
tft.setCursor(312,100);
tft.setTextColor(TFT_BLACK);
tft.setTextSize(3);
tft.print("Kas");
tft.setCursor(162,165);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(6);
tft.print("%");
tft.setCursor(412,165);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(6);
tft.print("%");
tft.setCursor(162,230);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(6);
tft.print("C");
tft.setCursor(145,230);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(2);
tft.print("o");
tft.setCursor(412,230);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(6);
tft.print("C");
tft.setCursor(395,230);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(2);
tft.print("o");
}
void time ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
//Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
//delay(5000); // five seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
// Serial.print(datestring);
//}
//void printTime()
//{
String dateAndTime = datestring;
//String dateAndTime = datestring+" "+hours;
tft.setTextSize(2);
char charBuf[25];
dateAndTime.toCharArray(charBuf, 25);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawCentreString(charBuf,240,25,2);
//void loop();
}
//void readSensor()
//{
// previousIndoorTemperature = indoorTemperature;
// previousIndoorHumidity = indoorHumidity;
//
// indoorHumidity = dht.readHumidity();
// indoorTemperature = dht.readTemperature();
// Serial.println(indoorTemperature);
// Serial.println(indoorHumidity);
//}
void printKasTemperature()
{
String temperature;
if(tempkas != previoustempkas)
{
if(humkas == 0.0 && tempkas == 0.0) //We just booted up
{
temperature = "---";
}else
{
temperature = String(tempkas,1);
}
tft.fillRect(270,232,120,40,TFT_BLACK);
tft.setCursor(270,234);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(5);
tft.print(temperature);
previoustempkas = tempkas;
}
}
void printRemoteHumidity()
{
String humidity;
if(humbuiten != previoushumbuiten)
{
if(humbuiten == 0.0 && tempbuiten == 0.0) //We just booted up
{
humidity = "---";
}else
{
humidity = String(humbuiten,1);
}
tft.fillRect(20,167,120,40,TFT_BLACK);
tft.setCursor(20,167);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(5);
tft.print(humidity);
previoushumbuiten = humbuiten;
}
}
void printRemoteTemperature()
{
String temperature;
if(tempbuiten != previoustempbuiten)
{
if(humbuiten == 0.0 && tempbuiten == 0.0) //We just booted up
{
temperature = "---";
}else
{
temperature = String(tempbuiten,1);
}
tft.fillRect(20,232,120,40,TFT_BLACK);
tft.setCursor(20,234);
tft.setTextColor(TFT_GREEN);
tft.setTextSize(5);
tft.print(temperature);
previoustempbuiten = tempbuiten;
}
}
void printKasHumidity()
{
if(humkas != previoushumkas)
{
String humidity = String(humkas,1);
tft.fillRect(270,167,120,40,TFT_BLACK);
tft.setCursor(270,167);
tft.setTextColor(TFT_CYAN);
tft.setTextSize(5);
tft.print(humidity);
previoushumkas = humkas;
}
}
en dit de zender en kas besturing een UNO:
#include <DHT.h>
//Constants
const int raamopen_pin = 7;
const int raamdicht_pin = 8;
const int ventilator_pin = 9;
#define DHTbuitenPIN 2 // what pin we're connected to
#define DHTbuitenTYPE DHT22 // DHT 22 (AM2302)
DHT dhtbuiten(DHTbuitenPIN, DHTbuitenTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
#define DHTkasPIN 13 // what pin we're connected to
#define DHTkasTYPE DHT22 // DHT 22 (AM2302)
DHT dhtkas(DHTkasPIN, DHTkasTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
int LDRInput = A0; //Set Analog Input A0 for LDR.
int thresholdUp = 400;
int thresholdDown = 250;
int soilPin = A1;
int soilPower = 3;//Variable for Soil moisture Power
unsigned long motorMillis = millis();
unsigned long raamMillis = millis();
unsigned long dnMillis = millis();
unsigned long tempMillis;
unsigned long bodemMillis = millis();
unsigned long serialMillis;
// funties
void temperaturen();
void display(String currState);
void dagnacht();
void raamstate(String currState);
void serial(); //com
//void motor();
void bodemvocht();
//Variables
int motorloop;
float ms;
int chk;
float humbuiten; //Stores humidity value
float tempbuiten; //Stores temperature value
float humkas; //Stores humidity value
float tempkas; //Stores temperature value
float dn;
float rs = 0;
float tempC = 0; //com
float bodem;
char text[20]; //com
char charVal[6]; //com
void setup()
{
pinMode(LDRInput, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite (7, HIGH);
digitalWrite (8, HIGH);
digitalWrite (9, HIGH);
digitalWrite (10, HIGH);
digitalWrite (11, HIGH);
digitalWrite (12, HIGH);
pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
Serial.begin(9600);
dhtbuiten.begin();
dhtkas.begin();
}
void loop()
{
temperaturen();
dagnacht();
raamstate();
serial();
bodemvocht();
}
void temperaturen()
{
static unsigned long tempMillis = millis();
if (millis() - tempMillis > 5000)
{ //Read data and store it to variables hum and temp
humbuiten = dhtbuiten.readHumidity();
tempbuiten = dhtbuiten.readTemperature();
humkas = dhtkas.readHumidity();
tempkas = dhtkas.readTemperature();
tempMillis = millis();
}
}
void dagnacht()
{
//static unsigned long dnMillis = millis();
if (millis() - dnMillis > 10000)
{
int value = analogRead(LDRInput); //Reads the Value of LDR(light).
// Serial.println("LDR value is :"); //Prints the value of LDR to Serial Monitor.
// Serial.println(value);
if (value < 400)
{
dn = 1;
// Serial.println(dn); //The LED turns ON in Dark.
// Serial.println("nacht");
}
else
{
dn = 2;
// Serial.println(dn);
// Serial.println("dag"); //The LED turns OFF in Light.
}
dnMillis = millis();
}
}
void raamstate()
{
static unsigned long raamMillis = millis();
static uint16_t raamdelay;
enum class raamstate:uint8_t
{
open, //default 0
dicht, // default 1
ventilatie, // default 2
reset, // default 3 }
};
static raamstate currState = raamstate::open;
switch (currState)
{
case raamstate::dicht:
// Serial.println ("raam dicht");
if ((millis() - raamMillis > 5000) && (tempkas > 27) && (dn == 2) && (rs != 1))
{
rs = 1;
// Serial.println("Raam gaat open");
digitalWrite(9, LOW);
digitalWrite(10, LOW);
// motorloop = 1;
// Serial.println("ventilator aan");
digitalWrite(11, LOW);
delay(120000);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
//Serial.println("Raam open");
currState = raamstate::open;
raamMillis = millis();
}
else if ((millis() - raamMillis > 5000) && (humkas > 50) && (dn == 2) && (rs != 1))
{
rs = 1;
// Serial.println("raam gaat naar ventilatie");
digitalWrite(9, LOW);
digitalWrite(10, LOW);
//motorloop = 2;
// Serial.println("ventilator aan");
digitalWrite(11, LOW);
delay(50000);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
// Serial.println("Raam op ventilatie");
currState = raamstate::ventilatie;
raamMillis = millis();
}
else if ((millis() - raamMillis > 5000) && (humkas > 50) && (tempbuiten > 25) && (dn == 1 && rs != 1))
{
rs = 1;
// Serial.println("raam gaat naar ventilatie");
digitalWrite(9, LOW);
digitalWrite(10, LOW);
// motorloop = 2;
delay(50000);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
//Serial.println("Raam op ventilatie");
currState = raamstate::ventilatie;
raamMillis = millis();
}
else if ((millis() - raamMillis > 5000) && (tempkas > 30) && (tempbuiten > 20) && (dn == 1 && rs != 1))
{
rs = 1;
// Serial.println("raam gaat open");
digitalWrite(9, LOW);
digitalWrite(10, LOW);
// motorloop = 1;
delay(120000);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
//Serial.println("Raam open");
currState = raamstate::open;
raamMillis = millis();
}
break;
case raamstate::open:
// Serial.println ("raam open");
if ((millis() - raamMillis > 5000) && (tempkas < 23) && (rs != 2))
{
rs = 2;
// Serial.println("raam gaat dicht");
digitalWrite(7, LOW);
digitalWrite(8, LOW);
// motorloop = 3;
// Serial.println("ventilator uit");
digitalWrite(11, HIGH);
delay(130000);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
// Serial.println("Raam dicht");
raamMillis = millis();
currState = raamstate::dicht;
}
else
{
if ((millis() - raamMillis > 5000) && (dn == 1) && (rs != 2) && (tempbuiten < 20))
{
rs = 2;
// Serial.println("raam gaat dicht ivm nacht");
digitalWrite(7, LOW);
digitalWrite(8, LOW);
// motorloop = 3;
// Serial.println("ventilator uit");
digitalWrite(11, HIGH);
delay(130000);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
// Serial.println("Raam dicht");
raamMillis = millis();
currState = raamstate::dicht;
}
}
break;
case raamstate::ventilatie:
//Serial.println("ventilatie");
if ((millis() - raamMillis > 15000) && (humkas < 50) && (tempkas < 25) && (rs != 3))
{
rs = 3;
// Serial.println("raam van ventilatie naar dicht");
digitalWrite(7, LOW);
digitalWrite(8, LOW);
// motorloop = 3;
// Serial.println("ventilator uit");
digitalWrite (11, HIGH);
delay(51000);
digitalWrite (7, HIGH);
digitalWrite (8, HIGH);
//Serial.println("Raam dicht");
currState = raamstate::dicht;
raamMillis = millis();
}
else if ((millis() - raamMillis > 5000) && (humkas > 50) && (tempkas > 23) && (dn == 2) && (rs != 3))
{
// Serial.println("raam van ventilatie naar open");
digitalWrite(9, LOW);
digitalWrite(10, LOW);
// motorloop = 1;
// Serial.println("ventilator aan");
digitalWrite(11, LOW);
delay(97000);
digitalWrite (9, HIGH);
digitalWrite (10, HIGH);
//Serial.println("Raam open");
currState = raamstate::open;
raamMillis = millis();
}
else if ((millis() - raamMillis > 15000) && (dn == 1) && (rs != 3) && (tempbuiten < 20))
{
rs = 3;
// Serial.println("raam gaat dicht ivm nacht");
digitalWrite(7, LOW);
digitalWrite(8, LOW);
// motorloop = 3;
Serial.println("ventilator uit");
digitalWrite(11, HIGH);
delay(51000);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
//Serial.println("Raam dicht");
raamMillis = millis();
currState = raamstate::dicht;
}
break;
case raamstate::reset:
// Serial.println ("raam reset");
if ((millis() - raamMillis > 5000) && (rs != 2))
{
rs = 2;
// Serial.println("raam gaat dicht");
digitalWrite(7, LOW);
digitalWrite(8, LOW);
// Serial.println("ventilator uit");
digitalWrite(11, HIGH);
delay(130000);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
raamMillis = millis();
currState = raamstate::dicht;
}
break;
}
}
void serial()
{static unsigned long serialMillis = millis();
if (millis() - serialMillis > 15000)
{
digitalWrite (13, HIGH);
delay(2000);
Serial.print("<");
Serial.print(humbuiten);
Serial.print(",");
Serial.print(tempbuiten);
Serial.print(",");
Serial.print(humkas);
Serial.print(",");
Serial.print(tempkas);
Serial.print(",");
Serial.print(bodem);
Serial.print(">");
serialMillis = millis();
delay(2000);
digitalWrite (13, LOW);
}
}
void bodemvocht()
{
String DisplayWords;
static unsigned long bodemMillis = millis();
if (millis() - bodemMillis > 25000)
{
int sensorValue;
digitalWrite(soilPower, HIGH);//turn D7 "On"
delay(10);//wait 10 milliseconds
sensorValue = analogRead(soilPin);//Read the SIG value form sensor
digitalWrite(soilPower, LOW);//turn D7 "Off"
//Serial.write("Water Level: ");
//Serial.print(sensorValue); //Using .print instead of .write for values
bodem = sensorValue;
bodemMillis = millis();
if (sensorValue <= thresholdDown){
DisplayWords = "Dry, Water it!";
// Serial.print(DisplayWords);
bodemMillis = millis();
} else if (sensorValue >= thresholdUp){
DisplayWords = "Wet, Leave it!";
// Serial.print(DisplayWords);
bodemMillis = millis();
} else {
// Serial.print(DisplayWords);
bodemMillis = millis();
}
}
//delay(500); //wait for half a second, so it is easier to read
}