Hello! I seem to be having trouble utilizing the <SPI.h> library for my RTL8720DN. I’m creating an air sensor and continue to get various issues with my code. Here is my code:
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include “BME280.h”
#include “PMS5003.h”
#include <SD.h>
// WiFi credentials
char ssid = “LDEO-Wireless”; // Change to your open WiFi SSID
// NTP Client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “pool.ntp.org”, -14400, 60000); // -14400 seconds for EDT (UTC-4)
// Sensors
BME280 bme280;
PMS5003 pms5003(PB2, PB1); // RX, TX
const int chipSelect = 10;
void epochToDateTime(unsigned long epoch, int &year, int &month, int &day, int &hour, int &minute, int &second) {
second = epoch % 60;
epoch /= 60;
minute = epoch % 60;
epoch /= 60;
hour = epoch % 24;
epoch /= 24;
unsigned long days = epoch;
year = 1970;
while (true) {
bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
unsigned long daysInYear = leap ? 366 : 365;
if (days >= daysInYear) {
days -= daysInYear;
year++;
} else {
break;
}
}
static const int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (month = 0; month < 12; month++) {
int dim = daysInMonth[month];
if (month == 1) { // February
bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (leap) {
dim++;
}
}
if (days >= dim) {
days -= dim;
} else {
break;
}
}
month++;
day = days + 1;
}
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to WiFi
WiFi.begin(ssid);
Serial.println("Connecting to WiFi...");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize sensors
if (!bme280.begin()) {
Serial.println("BME280 initialization failed");
while (1);
}
pms5003.begin();
// Initialize NTP Client
timeClient.begin();
// Initialize SD card
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed");
while (1);
}
}
void loop() {
// Update NTP Client
timeClient.update();
// Get current time
unsigned long epochTime = timeClient.getEpochTime();
// Convert epoch time to local time
int year, month, day, hour, minute, second;
epochToDateTime(epochTime, year, month, day, hour, minute, second);
// Print current date and time
String dateTime = String(month) + "/" + String(day) + "/" + String(year) + " " + String(hour) + ":" + String(minute) + ":" + String(second);
Serial.print("Current NYC time: ");
Serial.println(dateTime);
// Read PMS5003 data
int pm10, pm25, pm100;
if (pms5003.read(pm10, pm25, pm100)) {
Serial.print("PM1.0: ");
Serial.print(pm10);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(pm25);
Serial.println(" ug/m3");
Serial.print("PM10: ");
Serial.print(pm100);
Serial.println(" ug/m3");
} else {
Serial.println("Failed to read PMS5003 data");
}
// Read BME280 data
float temperature, humidity;
bme280.read(temperature, humidity);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Log data to SD card
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(dateTime);
dataFile.print(", PM1.0: ");
dataFile.print(pm10);
dataFile.print(" ug/m3, PM2.5: ");
dataFile.print(pm25);
dataFile.print(" ug/m3, PM10: ");
dataFile.print(pm100);
dataFile.print(" ug/m3, Temperature: ");
dataFile.print(temperature);
dataFile.print(" °C, Humidity: ");
dataFile.print(humidity);
dataFile.println(" %");
dataFile.close();
Serial.println("Data logged to SD card");
} else {
Serial.println("Failed to open datalog.txt");
}
delay(1000); // Wait for 1 second before the next reading
}
and my Error(s) /Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino: In function ‘void epochToDateTime(long unsigned int, int&, int&, int&, int&, int&, int&)’:
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:51:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (days >= dim) {
~~^
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino: In function ‘void setup()’:
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:91:13: error: request for member ‘begin’ in ‘0’, which is of non-class type ‘int’
if (!SD.begin(chipSelect)) {
^~~~~
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino: In function ‘void loop()’:
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:140:5: error: ‘File’ was not declared in this scope
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);
^~~~
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:141:9: error: ‘dataFile’ was not declared in this scope
if (dataFile) {
^~~~~~~~
Multiple libraries were found for “NTPClient.h”
Used: /Users/trevd/Library/Arduino15/packages/realtek/hardware/AmebaD/3.1.7/libraries/NTPClient
Not used: /Users/trevd/Documents/Arduino/libraries/NTPClient
Multiple libraries were found for “PMS5003.h”
Used: /Users/trevd/Documents/Arduino/libraries/PMS5003
Not used: /Users/trevd/Documents/Arduino/libraries/GuL_Plantower
exit status 1
Compilation error: request for member ‘begin’ in ‘0’, which is of non-class type ‘int’
Please help! This compilation error has plagued me for two days. Here I’m trying to bypass utilizing the spi.h library. Is that okay? I need any help i can get, thank you!