Hi, i'm building a project with:
-Arduino Nano RP2040
-DHT11 sensor from Makeblock
-L80-M39 GPS
-2.4 TFT SPI 240 X 320 Touchscreen
In this project i have 4 pages where i display sensors data's. The DHT11 datas are always upgrading but not the GPS one's.
For the GPS i'm using the Adafruit library with the Hardware serial connection (TX and RX) [ No TinyGPS because my arduino can't work with Software Serial library ].
With the Adafruit example my Arduino keeps upgrading the datas.
Adafruit Example:
// Test code for Ultimate GPS Using Hardware Serial (e.g. GPS Flora or FeatherWing)
//
// This code shows how to listen to the GPS module via polling. Best used with
// Feathers or Flora where you have hardware Serial and no interrupt
//
// Tested and works great with the Adafruit GPS FeatherWing
// ------> https://www.adafruit.com/products/3133
// or Flora GPS
// ------> https://www.adafruit.com/products/1059
// but also works with the shield, breakout
// ------> https://www.adafruit.com/products/1272
// ------> https://www.adafruit.com/products/746
//
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada
#include <Adafruit_GPS.h>
// what's the name of the hardware serial port?
#define GPSSerial Serial1
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false
uint32_t timer = millis();
void setup()
{
//while (!Serial); // uncomment to have the sketch wait until Serial is ready
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
Serial.println("Adafruit GPS library basic parsing test!");
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// uncomment this line to turn on only the "minimum recommended" data
//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
// For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
// the parser doesn't care about other sentences at this time
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
// Request updates on antenna status, comment out to keep quiet
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
// Ask for firmware version
GPSSerial.println(PMTK_Q_RELEASE);
}
void loop() // run over and over again
{
float lati, longi;
// read data from the GPS in the 'main loop'
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) Serial.print(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trying to print out data
Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
if (GPS.hour < 10) { Serial.print('0'); }
Serial.print(GPS.hour, DEC); Serial.print(':');
if (GPS.minute < 10) { Serial.print('0'); }
Serial.print(GPS.minute, DEC); Serial.print(':');
if (GPS.seconds < 10) { Serial.print('0'); }
Serial.print(GPS.seconds, DEC); Serial.print('.');
if (GPS.milliseconds < 10) {
Serial.print("00");
} else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
Serial.print("0");
}
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
lati = GPS.latitude;
longi = GPS.longitude;
Serial.print("Location: ");
Serial.print(conv_coords(lati), 6); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(conv_coords(longi), 6); Serial.println(GPS.lon);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
}
float conv_coords(float in_coords)
{
//Initialize the location.
float f = in_coords;
// Get the first two digits by turning f into an integer, then doing an integer divide by 100;
// firsttowdigits should be 77 at this point.
int firsttwodigits = ((int)f)/100; //This assumes that f < 10000.
float nexttwodigits = f - (float)(firsttwodigits*100);
float theFinalAnswer = (float)(firsttwodigits + nexttwodigits/60.0);
return theFinalAnswer;
}
My Code:
//libraries
#include <Adafruit_GFX.h> // graphic library
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h> //Screen library
#include <XPT2046_Touchscreen.h> //Touch library
#include <Adafruit_GPS.h> //GPS library
#include <DFRobot_DHT11.h> //DHT11 Library
//Definitions
//Screen
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3872
#define TS_MAXY 3888
#define CS_PIN 6
#define TIRQ_PIN 2
XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts
#define TFT_DC 8
#define TFT_CS 10
#define TFT_RST 7
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define BOXSIZE 40
#define PENRADIUS 2
int oldcolor, currentcolo2r;
//GPS
#define GPSSerial Serial1
Adafruit_GPS GPS(&GPSSerial);
#define GPSECHO false
uint32_t timer = millis();
//DHT11
DFRobot_DHT11 DHT;
void setup() {
//Schermo
Serial.begin(115200);
Serial.println(F("Touch Paint!"));
//opening pins for screen
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
pinMode(TFT_CS, OUTPUT);
digitalWrite(TFT_CS, HIGH);
//touchsceen begin
ts.begin();
ts.setRotation(2);
//grafic screen begin
tft.begin();
if (!ts.begin()) {
Serial.println("Couldn't start touchscreen controller"); //if it's not able to start touch
while (1);
}
tft.fillScreen(ILI9341_BLACK); // black layer
//DHT11
#define DHT11_PIN 14 //DHT11 pin definition
//Drawing arrows on the bottom of the screen
tft.drawRect(0,240,120,80,ILI9341_WHITE);
tft.drawRect(120,240,120,80,ILI9341_WHITE);
tft.drawChar(20,253,'<',ILI9341_WHITE,0,8);
tft.drawChar(170,253,'>',ILI9341_WHITE,0,8);
tft.drawPixel(0,0,ILI9341_RED);
//GPS
Serial.println("Adafruit GPS library basic parsing test!");
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
GPSSerial.println(PMTK_Q_RELEASE);
}
int k = 0; //counter for change the page
bool F0 = false,F1 = true,F2 = true,F3 = true/*,F4 = true*/; //boolean variables used as flag for not repeating an operation
int t = 0, h = 0 ;
float conv_coords(float in_coords) // function to convert coordinates to decimal
{
//Initialize the location.
float f = in_coords;
// Get the first two digits by turning f into an integer, then doing an integer divide by 100;
// firsttowdigits should be 77 at this point.
int firsttwodigits = ((int)f)/100; //This assumes that f < 10000.
float nexttwodigits = f - (float)(firsttwodigits*100);
float theFinalAnswer = (float)(firsttwodigits + nexttwodigits/60.0);
return theFinalAnswer;
}
void loop() {
//Serial.print ("INIZIO LOOP\n");
//DHT11 temperature and humidity variables
DHT.read(DHT11_PIN);
t = DHT.temperature;
h = DHT.humidity;
//GPS
if(true){
float lati = 0, longi=0;
char c = 0;
timer = 0;
c = GPS.read();
if (GPSECHO)
if (c) Serial.print(c);
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trying to print out data
Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
if (GPS.hour < 10) { Serial.print('0'); }
Serial.print(GPS.hour, DEC); Serial.print(':');
if (GPS.minute < 10) { Serial.print('0'); }
Serial.print(GPS.minute, DEC); Serial.print(':');
if (GPS.seconds < 10) { Serial.print('0'); }
Serial.print(GPS.seconds, DEC); Serial.print('.');
if (GPS.milliseconds < 10) {
Serial.print("00");
} else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
Serial.print("0");
}
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
lati = GPS.latitude;
longi = GPS.longitude;
Serial.print("Location: ");
Serial.print(conv_coords(lati), 6); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(conv_coords(longi), 6); Serial.println(GPS.lon);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
}
// Page 0, Temperature visualization
if(k==0){
//Serial.print("Temperatura:\n");
//Serial.print(t);
//Serial.print("\n");
tft.fillRect(0,0,240,240,ILI9341_BLACK);
tft.drawLine(0,118,240,118,ILI9341_WHITE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(0, 220);
tft.print("Minima:");
tft.setCursor(0, 200);
tft.print("Massima:");
tft.setCursor(0, 180);
tft.print("Media:");
tft.setCursor(0, 160);
tft.print("Ora:");
//tft.print();
tft.print(" : ");
//tft.print();
tft.print(" : ");
//tft.print();
tft.setCursor(102, 160);
tft.setCursor(0, 140);
tft.print("Data:");
tft.setCursor(0, 120);
tft.print("Temperatura:");
tft.setCursor(150, 120);
tft.print(t); // Printing temperature
tft.print(" C");
}
// Page 1, Humidity visualization
if(k==1){
//Serial.print("Umidità\n");
tft.fillRect(0,0,240,240,ILI9341_BLACK);
tft.drawLine(0,118,240,118,ILI9341_WHITE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(0, 220);
tft.print("Minima:");
tft.setCursor(0, 200);
tft.print("Massima:");
tft.setCursor(0, 180);
tft.print("Media:");
tft.setCursor(0, 160);
tft.print("Ora:");
//tft.print();
tft.print(" : ");
//tft.print();
tft.print(" : ");
//tft.print();
tft.setCursor(102, 160);
tft.setCursor(0, 140);
tft.print("Data:");
tft.setCursor(0, 120);
tft.print("Umidita':");
//Serial.print("\n Umidità: ");
//Serial.print(h);
//Serial.print(" % \n");
tft.setCursor(130, 120);
tft.print(h); //printing humidity
tft.print(" %");
}
// Serial.print(event.temperature);
// Serial.print("\n");
if (ts.bufferEmpty()) {
return;
}
if (! ts.touched()) {
return;
}
// Touching coordinates
TS_Point p = ts.getPoint();
p.x = -(map(p.x, TS_MINX, TS_MAXX, 0, tft.width())-240);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
if(ts.touched()){ //if i'm touching the display
delay (150); // little delay to slow arduino to get the touch (i'm not very fast to touch it)
if((Avanti(p.x, p.y)) == true){ //if i'm touching foward button
Serial.print("Avanti cliccato\n");
if(k == 0 && F0 == false && F1 == true && F2 == true && F3 == true /*&& F4 == true*/){ //if I was in the 3 page i can only go in the 0 one
Serial.print("k = 0\n New k:");
k=1;
F0 = true;
F1 = false;
Serial.print(k);
Serial.print("\n");
}
else{
if (k == 1 && F0 == true && F1 == false && F2 == true && F3 == true/* && F4 == true*/){ //if I was in the 0 page i can only go in the 1 one
Serial.print("k = 0\n New k:");
k=2;
F1 = true;
F2 = false;
Serial.print(k);
Serial.print("\n");
}
else{
if (k == 2 && F0 == true && F1 == true && F2 == false && F3 == true /*&& F4 == true*/){ //if I was in the 1 page i can only go in the 2 one
Serial.print("k = 0\n New k:");
k=3;
F2 = true;
F3 = false;
Serial.print(k);
Serial.print("\n");
}
else{
if (k == 3 && F0 == true && F1 == true && F2 == true && F3 == false /*&& F4 == true*/){ //if I was in the 2 page i can only go in the 3 one
Serial.print("k = 0\n New k:");
k=0;
F3 = true;
F0 = false;
Serial.print(k);
Serial.print("\n");
}
/*else{
if (k == 4 && F0 == true && F1 == true && F2 == true && F3 == true && F4 == false){
Serial.print("k = 0\n New k:");
k=0;
F4 = true;
F0 = false;
Serial.print(k);
Serial.print("\n");
}
}*/
}
}
}
}
//Same as before but going backwards
if((Indietro(p.x, p.y)) == true){
Serial.print("Indietro cliccato\n");
if(k == 0 && F0 == false && F1 == true && F2 == true && F3 == true/* && F4 == true*/){
Serial.print("k = 0\n New k:");
k=3;
F0 = true;
F3 = false;
Serial.print(k);
Serial.print("\n");
}
else{
if (k == 1 && F0 == true && F1 == false && F2 == true && F3 == true/* && F4 == true*/){
Serial.print("k = 0\n New k:");
k=0;
F1 = true;
F0 = false;
Serial.print(k);
Serial.print("\n");
}
else{
if (k == 2 && F0 == true && F1 == true && F2 == false && F3 == true/* && F4 == true*/){
Serial.print("k = 0\n New k:");
k=1;
F2 = true;
F1 = false;
Serial.print(k);
Serial.print("\n");
}
else{
if (k == 3 && F0 == true && F1 == true && F2 == true && F3 == false/* && F4 == true*/){
Serial.print("k = 0\n New k:");
k=2;
F3 = true;
F2 = false;
Serial.print(k);
Serial.print("\n");
}
/*else{
if (k == 4 && F0 == true && F1 == true && F2 == true && F3 == true && F4 == false){
Serial.print("k = 0\n New k:");
k=3;
F4 = true;
F3 = false;
Serial.print(k);
Serial.print("\n");
}
}*/
}
}
}
}
}
}
//touching the foward button
bool Avanti (int x,int y){
if(120 < x && x < 240 && 230 < y && y < 320){
return true;
}
else{
return false;
}
}
//touching the backwards button
bool Indietro (int x,int y){
if(0 < x && x < 120 && 230 < y && y < 320){
return true;
}
else{
return false;
}
}
General photo
Temperature page
Humidity page
Serial monitor: