Is my data collected properly in code?

My setup is an Arudino with a Wifi shield connected to a Grove shield:

station.jpg

My sketch basically does:

setup() - call an alarm every 30 minutes & wdt_enable(WDTO_8S)
loop() - wdt_reset() & test condition if flag isSet and timelapse > certain value… then call SampleUV, connect to wifi & report to cloud.

SampleUV is actually a concatenated call where SampleUVAlarm samples UV and then calls SampleMQ2Alarm (which samples MQ2) & calls printCO2 (which samples CO2 & temp).

So this sampling processes gets fired by the loop() test for the flag & timestamp. If true, loop() calls SampleUVAlarm (takes about 1 second), SampleMQ2Alarm (takes about 2 seconds) and printCO2()-dataReceive() (take about 3 seconds). So this the whole sampling process may take about 5-7seconds.

Then comes a pat and wifiStuff() gets called, this takes no more than about 5 seconds, and another pat.

Finally reportToCloud() opens the site, takes the int variables and converts them to string, concatenates them into a data var, resets all data vars to 0, posts the data string to the website & closes.

The box with the CO2-Temp sensor and the UV sensor sticks out of my wall. Its not the best place since it only gets direct UV light ~11-12pm noon. But let’s assume this is what I want, since UV radiation is strongest at midday anyway.

UV readings are off because I would expect readings to be around 5000-8000-11000 at midday. Im only getting around 1200 and 1600 at midday. And Im getting some false spikes at midnight and 3am for example, of 1500 and 1600.

What Im looking for is a problem with my code. To see if garbage data can be getting in there or if the way I’ve wired it has anything to do with the issue?

#include <TimeAlarms.h>
#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
#include <avr/wdt.h>

boolean initialiseSensorFlag;
unsigned long initialiseSensorTimestamp;

long previousMillis = 0;
long interval = 1000;

unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

SoftwareSerial wifiSerial(2,3); //WIFI SHIELD 

#define DEBUG 1                       
const int pinRx = 8;                  
const int pinTx = 7;                  
SoftwareSerial sensor(pinTx,pinRx);  

const unsigned char cmd_get_sensor[] = 
{
    0xff, 0x01, 0x86, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x79
};
unsigned char dataRevice[9];          
int temperature;                      
int CO2PPM;                           
float uvindex;                        
float mq2ratio;                       

WiFly wifly;                          //WIFI
String data;                         

const char mySSID[] = "mywifi";
const char myPassword[] = "mykey";
const char site[] = "santiapps.com";

void terminal();

void setup(){ 
  Serial.begin(9600);
  Alarm.timerRepeat(1800, MainAlarm); 
  Serial.println("Alarms set!");
  wdt_enable(WDTO_8S);
}

void loop(){
  Alarm.delay(10); 
  if (wifly.moduleCrashed==true){
    Serial.println("moduleCrashed");
    wdt_enable(WDTO_8S);
    while (true);
  }
  
  wdt_reset();
  
  if(initialiseSensorFlag == true && millis() - initialiseSensorTimestamp > 3000){
    initialiseSensorFlag = false;    
    SampleUVAlarm(); // samples UV & MQ2
    wdt_reset();
    wifiStuff(); // connect to Wifi
    wdt_reset();
    reportToCloud(); // post to web
  }
}

void wifiStuff(){
   char buf[32];
   Serial.print("Free memory: ");
   Serial.println(wifly.getFreeMemory(),DEC);

   wifiSerial.begin(9600);
   if (!wifly.begin(&wifiSerial, &Serial)) {
       Serial.println("Failed to start wifly");
  terminal();
   }
   if (!wifly.isAssociated()) {
  Serial.println("Joining network");
  wifly.setSSID(mySSID);
        wifly.setPassphrase(myPassword);
  wifly.enableDHCP();

  if (wifly.join()) {
      Serial.println("Joined wifi network");
  } else {
      Serial.println("Failed to join wifi network");
      terminal();
  }
   } else {
       Serial.println("Already joined network");
   }
   
   wifly.setDeviceID("Wifly-WebClient2");
   
   Serial.print("DeviceID: ");
   Serial.println(wifly.getDeviceID(buf, sizeof(buf)));
   
   if (wifly.isConnected()) {
       Serial.println("Old connection active. Closing");
  wifly.close();
   }
}

void initialiseSensor(){
  sensor.begin(9600);
  Serial.begin(9600);
  Serial.println("*******");
}

void MainAlarm(){
  Serial.println("Main Alarm...");
  Serial.println(wifly.moduleCrashed);
  initialiseSensor();
  initialiseSensorFlag = true;
  initialiseSensorTimestamp = millis();
}

void reportToCloud() {
  data = "";
  Serial.println("Reporting to cloud...");
   if (wifly.available() > 0) {
  char ch = wifly.read();
  Serial.write(ch);
  if (ch == 'n') { 
      Serial.write('r');
  }
   }
   
   if (wifly.open(site, 80)) {
       Serial.print("Connected to ");
  Serial.println(site);
  
    // Set data to send
    static char outstr1[15];
    static char outstr2[15];
    static char outstr3[15];
    static char outstr4[15];

    String dataString1 = dtostrf(uvindex, 8, 2, outstr1);
    String dataString2 = dtostrf(mq2ratio, 8, 2, outstr2);
    String dataString3 = dtostrf(CO2PPM, 8, 2, outstr3);
    String dataString4 = dtostrf(temperature, 8, 2, outstr4);
    data = String("uvindex=" + dataString1 + "&mq2=" + dataString2 + "&age=" + dataString3 + "&name=" + dataString4);    
    Serial.print(data); //name = temp && age = co2
    //Reset all values
    uvindex = 0;
    mq2ratio = 0;
    CO2PPM = 0;
    temperature = 0;
    
 wifly.println("POST /weatherdata/data_collect.php HTTP/1.0");
  wifly.println("Host: www.myserver.com"); // SERVER ADDRESS HERE TOO
        wifly.println("Content-Type: application/x-www-form-urlencoded" );
        wifly.print("Content-Length: ");
        wifly.println(data.length());
  wifly.println();
        wifly.print(data);
        Serial.println("Posted successfully");
   } else {
       Serial.println(">>Failed to connect");
   }

   if (Serial.available() > 0) {
  wifly.write(Serial.read());
   }
   
   wifly.close();
}

void terminal(){
   while (1) {
  if (wifly.available() > 0) {
      Serial.write(wifly.read());
  }
  if (Serial.available() > 0) {
      wifly.write(Serial.read());
  }
   }
}

void SampleUVAlarm (){
  Serial.println("Sampling UV");
  int sensorValue = 0;
  long  sum = 0;
  uvindex = 0;
  for(int i=0;i<1024;i++)
   {  
      sensorValue=analogRead(A0);
      sum=sensorValue+sum;
      delay(2);
   }   
 sum = sum >> 10;
 Serial.print("The UV voltage value:");
 Serial.print(sum*4980.0/1023.0);
 Serial.print(" mV");
 delay(20);
 Serial.print("UVIndex is: ");
 uvindex = (307*(sum*4980.0/1023.0))/200; //mW/m2 -> W/m2
 Serial.println(uvindex);
 Serial.print("n");
 SampleMQ2Alarm();
}

void SampleMQ2Alarm() {
  Serial.println("Sampling MQ2");
  float sensor_volt = 0;
  float RS_gas = 0; 
  float ratio = 0; 
  mq2ratio = 0;
  int sensorValue = analogRead(A1);
  sensor_volt=(float)sensorValue/1024*5.0;
  RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL
 ratio = RS_gas/0.08;  // ratio = RS/R0 
 
  Serial.print("MQ2 Sensor_volt = ");
  Serial.println(sensor_volt);
  Serial.print("RS_ratio = ");
  Serial.println(RS_gas);
  Serial.print("Rs/R0 = ");
  Serial.println(ratio);
  mq2ratio = ratio;
  Serial.print("nn");
  delay(1000);
  printCO2(); // sample co2
}
void printCO2() {
  Serial.println("printing co2...");
  if(dataRecieve())
  {
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print("  CO2: ");
    Serial.print(CO2PPM);
    Serial.println("");
  }
  delay(500);
}

bool dataRecieve(void) {
  byte data[9];
  int i = 0;
  CO2PPM = 0;
  temperature = 0;
  
  for(i=0; i<sizeof(cmd_get_sensor); i++)
  {
    sensor.write(cmd_get_sensor[i]);
  }
  delay(500);
 if(sensor.available())
  {
    while(sensor.available())
    {
      for(int i=0;i<9; i++)
      {
        data[i] = sensor.read();
      }
    }
  }
  
  #if DEBUG
  for(int j=0; j<9; j++)
  {
    Serial.print(data[j]);
    Serial.print(" ");    
  }
  Serial.println("");
  #endif
  
  if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3] 
    + data[4] + data[5] + data[6] + data[7]))) != data[8])
  {
    Serial.println("false returned");
    return false;
  }

  CO2PPM = (int)data[2] * 256 + (int)data[3];
  temperature = (int)data[4] - 40;  
  Serial.println(temperature);
  return true;
}

Have you tested the components individually? If not get each device working separately first then attempt to combine everything.