Sensor + Relay + RTC + Display - Arduino Code

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Wed Nov 23, 2022 6:12 pm Yes. Your syntax is incorrect.

Maybe:

Code: Select all

    //digitalWrite(12, rel_hum >= 80);

if (rel_hum >=80) {
  digitalWrite(12, HIGH);
  Serial.println("Vent: ON");
} else if (rel_hum <=60) {
  digitalWrite(12, LOW);
  Serial.println("Vent: OFF");
}
Dave
Perfect! That fixed it. Thanks so much.

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

Well now I have the tft wired into the board again. Can you suggest any shortcuts for getting the serial monitor data to the display?

I've installed libraries and tried to paste the "graphicstest" example into my existing sketch just to see if I can get them to run together, but I'm just getting error messages.

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by Franklin97355 »

The error messages would help. Do you want just serial character data or graphics too?

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

Franklin97355 wrote: Sat Nov 26, 2022 12:38 pm The error messages would help. Do you want just serial character data or graphics too?
I only want serial data to display. Just haven't found any real clear explanations or examples yet.

I'll share error messages when I start working on the actual problem.

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

Well I have this update with initialization for the tft. Any hints for getting serial data to the display? I've tried to apply a few examples, but just getting stuck every time so far....

Code: Select all

#include <RTClib.h>

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

RTC_DS1307 rtc;

Adafruit_HTU21DF htu = Adafruit_HTU21DF();


#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>


  #define TFT_CS        10
  #define TFT_RST        9 // Or set to -1 and connect to Arduino RESET pin
  #define TFT_DC         8



Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);



void setup() {
  Serial.begin(9600);

      pinMode(12, OUTPUT); 

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
    
    tft.init(135, 240);           // Init ST7789 240x135


 


  }




#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
 
  }

}


void loop() {
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t");
    Serial.print("RH: "); Serial.print(rel_hum); Serial.println(" \%");
    delay(4000);





if (rel_hum >=80) {
  digitalWrite(12, HIGH);
  Serial.println("Vent: ON");
  
} else if (rel_hum <=60) {
  digitalWrite(12, LOW);
  Serial.println("Vent: OFF");
}



{
    DateTime now = rtc.now();

    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC); 
      Serial.print("\t");
    delay(3000);
}


}

Could something like this be in the ballpark?

Code: Select all

  if (Serial.available())
    tft.print((char)Serial.read());
I tried this in the loop, but it didn't anything for the display.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Often a you need to do a println at the end to flush the line out. Not sure if that's the case here but it's worth a try.

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

Well I've looked around the tft example some more and pared down the rest as much as possible, with text ("TEST") displaying how I want it. I added this in setup...

Code: Select all

  tft.setRotation(3);
  tft.setTextSize(2);
  tft.setCursor(30, 30);
  tft.fillScreen(ST77XX_BLACK);
drawtext("TEST", ST77XX_WHITE);
  delay(100);
  tft.invertDisplay(true);
  delay(1000);
...with this at the end...

Code: Select all

void drawtext(char *text, uint16_t color) {
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}
That example also has this short text that prints to the serial just once while "TEST" displays on the screen ...

Code: Select all

  Serial.print(F("Hello! ST77xx TFT Test"));
I've tried a few different things and can't even get this to the tft. Any other hints? IS this any closer...

Code: Select all

   if (Serial.available()) {
        tft.write(Serial.println()); 
  
    }

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Code: Select all

   if (Serial.available()) {
        tft.write(Serial.println()); 
  
    }
No, not that. Try

Code: Select all

tft.writeln();
Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Wed Nov 30, 2022 11:53 am

No, not that. Try

Code: Select all

tft.writeln();
Dave
Do you mean that by itself somewhere, or like this?...

Code: Select all

 if (Serial.available()) {
         tft.writeln(); 
    }
The compiler gave me a "class has no member named 'writeln'" error message for that.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

Let's back up a step. What are you trying to do with the display. The code you've been sharing seems to be trying to put on the tft what you type into the Serial console/monitor. Reading through the conversation I think you want the tft to echo what gets output to Serial. Is that right?

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Thu Dec 01, 2022 9:52 am Let's back up a step. What are you trying to do with the display. The code you've been sharing seems to be trying to put on the tft what you type into the Serial console/monitor. Reading through the conversation I think you want the tft to echo what gets output to Serial. Is that right?

Dave
Yes, that is correct. The sketch is doing everything else I need and I just want serial to display on the tft, clearing between each line of data. Here's what I have so far. Apologies I know it's messy....

Code: Select all

#include <RTClib.h>

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

RTC_DS1307 rtc;

Adafruit_HTU21DF htu = Adafruit_HTU21DF();


#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>


  #define TFT_CS        10
  #define TFT_RST        9 // Or set to -1 and connect to Arduino RESET pin
  #define TFT_DC         8



Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);



void setup() {
  Serial.begin(9600);

      pinMode(12, OUTPUT); 

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
    
    tft.init(135, 240);           



  tft.setRotation(3);
  tft.setTextSize(2);
  tft.setCursor(30, 30);
  tft.fillScreen(ST77XX_BLACK);

  tft.fillScreen(ST77XX_BLACK);
  //drawtext("TEST", ST77XX_WHITE);
  delay(1000);





  }




#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
 
  }

}


void loop()
{

  
      



    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Temp: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t");
    Serial.print("RH: "); Serial.print(rel_hum); Serial.println(" \%");
    delay(4000);





if (rel_hum >=80) {
  digitalWrite(12, HIGH);
  Serial.println("Vent: ON");
  
} else if (rel_hum <=60) {
  digitalWrite(12, LOW);
  Serial.println("Vent: OFF");
}






{
    DateTime now = rtc.now();

    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC); 
      Serial.print("\t");
    delay(3000);
    

     

    
}




}



void drawtext(char *text, uint16_t color) {
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);

    
      
    
    
 } 

I don't understand exactly what the third void function accomplishes and I'm sure it's part of my problem. I just left it in there because it was the only way I could get anything to the tft....

Code: Select all

void drawtext(char *text, uint16_t color) {
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

You probably want to close the block after

Code: Select all

if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
rather than after

Code: Select all

delay(1000);
BTW, Arduino has formatter to clean up your code.

Have a look at some of the examples for the tft. There will be some that output text that should give you need. Usually on a display like this, you set the cursor position, then output the text rather than having it scroll.

Reading through the GFX guide will be a help as well: https://learn.adafruit.com/adafruit-gfx ... cs-library

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Thu Dec 01, 2022 11:47 pm You probably want to close the block after

Code: Select all

if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
rather than after

Code: Select all

delay(1000);
BTW, Arduino has formatter to clean up your code.

Have a look at some of the examples for the tft. There will be some that output text that should give you need. Usually on a display like this, you set the cursor position, then output the text rather than having it scroll.

Reading through the GFX guide will be a help as well:

Code: Select all

https://learn.adafruit.com/adafruit-gfx-graphics-library
Dave
Actually the serial write breaks when I move that line down...I'll experiment with it some more.

I think I have a handle on the gfx library as far as getting text to display right.

Yep I've been through most of the tft examples and have not yet seen something like this wither serial data displaying on the tft. Anywhere else to look for examples?

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by dastels »

You won't display serial data on the display, you'll put data on the display as well (or instead of) printing it to serial.

Dave

User avatar
hydrophyte
 
Posts: 67
Joined: Tue Mar 23, 2021 12:52 am

Re: Sensor + Relay + RTC + Display - Arduino Code

Post by hydrophyte »

dastels wrote: Sat Dec 17, 2022 9:31 pm You won't display serial data on the display, you'll put data on the display as well (or instead of) printing it to serial.

Dave
Well I guess I had wondered about that. I had this idea in mind because I saw a couple projects that seemed to have taken approach for some reason.

How about, as a new starting point, setting up the tft graphics in setup...

Code: Select all

  tft.setRotation(3);
  tft.setTextSize(2);
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(30, 30);
  delay(1000);
With something like this in loop...

Code: Select all

   float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    tft.print("Temp: "); tft.print(temp); tft.println(" C");
    tft.print("\t");
    tft.print("RH: "); tft.print(rel_hum); tft.println(" \%");
There is also the question of getting the current time to display...

Code: Select all

    DateTime now = rtc.now();
    tft.print(now.hour(), DEC);
    tft.print(':');
    tft.print(now.minute(), DEC);
    tft.print(':');
    tft.print(now.second(), DEC); 
      tft.print("\t");

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”