Hello,
Im working on a project where on one side I have some sensors connected on UNO as well as SD module and on other side I jave UNO with tft display. My goal is to be able to display the current sensor data on UNO with tft as well as store the data in csv file on SD card on UNO with SD module and sensors. My plan to communicate between two UNOs if to send json file via serial communication using software serial. One of the sensors (pms5003) also uses serial comm to display and read data from it. I have tried each segment of project code solo and it works well by itself but when I connect everything together the program or rather UNO with SD module just starts to reset (Im checking this via Serial prints on serial monitor, I have put various serial prints with numbers or messages so I can check where the code stops working).
The code from first Arduino that has SD module and sensors is this:
#include <SoftwareSerial.h>
#include <Adafruit_CCS811.h>
#include <dht.h>
#include <SPI.h>
#include <SD.h>
#include <ArduinoJson.h>
dht DHT;
#define DHT22_PIN 5
Adafruit_CCS811 ccs;
File myFile;
int aqi;
int pm1=0, pm2_5=0, pm10=0;
float t,h;
int co2,voc;
SoftwareSerial pmsSerial (2, 3);
SoftwareSerial linkSerial(7, 6);
struct pms5003data {
uint16_t framelen;
uint16_t pm10_standard, pm25_standard, pm100_standard;
uint16_t pm10_env, pm25_env, pm100_env;
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
uint16_t unused;
uint16_t checksum;
};
struct pms5003data data;
boolean readPMSdata(Stream *s) {
if (! s->available()) {
return false;
}
// Read a byte at a time until we get to the special '0x42' start-byte
if (s->peek() != 0x42) {
s->read();
return false;
}
// Now read all 32 bytes
if (s->available() < 32) {
return false;
}
uint8_t buffer[32];
uint16_t sum = 0;
s->readBytes(buffer, 32);
// get checksum ready
for (uint8_t i=0; i<30; i++) {
sum += buffer[i];
}
for (uint8_t i=2; i<32; i++) {
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
}
Serial.println();
// The data comes in endian'd, this solves it so it works on all platforms
uint16_t buffer_u16[15];
for (uint8_t i=0; i<15; i++) {
buffer_u16[i] = buffer[2 + i*2 + 1];
buffer_u16[i] += (buffer[2 + i*2] << 8);
}
// put it into a nice struct :)
memcpy((void *)&data, (void *)buffer_u16, 30);
if (sum != data.checksum) {
Serial.println("Checksum failure");
return false;
}
// success!
return true;
}
void setup() {
Serial.begin(115200);
ccs.begin();
pmsSerial.begin(9600); //UART to sensor
linkSerial.begin(4800);
if (!SD.begin(4)) {
Serial.println(F("initialization failed!"));
while (1);
}
Serial.println(F("initialization done."));
delay(5000);
SD.remove("data.csv");
myFile = SD.open("data.csv", FILE_WRITE);
if (myFile) {
Serial.println("2");
myFile.print("TEMPERATURA");
myFile.print(",");
myFile.print("VLAGA");
myFile.print(",");
myFile.print("CO2");
myFile.print(",");
myFile.print("VOC");
myFile.print(",");
myFile.print("PM 1");
myFile.print(",");
myFile.print("PM 2.5");
myFile.print(",");
myFile.print("PM10");
myFile.print(",");
myFile.println("AQI");
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println(F("error opening data.csv"));
}
}
void loop() {
// put your main code here, to run repeatedly:
StaticJsonDocument<300> doc;
if(ccs.available())
{
if (!ccs.readData()){
Serial.println(F("4"));
int chk = DHT.read22(DHT22_PIN);
t = DHT.temperature;
h = DHT.humidity;
co2 = ccs.geteCO2();
voc = ccs.getTVOC();
aqi = analogRead(0);
pmsSerial.listen();
if (readPMSdata(&pmsSerial)) {
pm1 = data.pm10_standard;
pm2_5 = data.pm25_standard;
pm10 = data.pm100_standard;}
doc["temp"] = t;
doc["hum"] = h;
doc["co2"] = co2;
doc["voc"] = voc;
doc["pm1"] = pm1;
doc["pm2.5"] = pm2_5;
doc["pm10"] = pm10;
doc["aqi"] = aqi;
linkSerial.listen();
serializeJson(doc, linkSerial);
Serial.println(F("5"));
myFile = SD.open("data.csv", FILE_WRITE);
if (myFile) {
Serial.println(F("6"));
myFile.print(t);
myFile.print(",");
myFile.print(h);
myFile.print(",");
myFile.print(co2);
myFile.print(",");
myFile.print(voc);
myFile.print(",");
myFile.print(pm1);
myFile.print(",");
myFile.print(pm2_5);
myFile.print(",");
myFile.print(pm10);
myFile.print(",");
myFile.println(aqi);
myFile.close();
}
else {
Serial.println(F("Error opening file in setup."));
}
}}
Serial.println(F("7"));
delay(2000);
}
Ill need to make 2 posts because of lack of characters.