The code for sending the numbers is
#include <DHT22.h>
#include <stdio.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int dustValenvio = 0;
int dustPin=0;
int dustVal=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
// Cable de datos conectado en pin 4 de Arduino
#define DHT22_PIN 4
// Inicio de Setup DHT22
DHT22 myDHT22(DHT22_PIN);
const char inicionumero = '<';
const char finnumero = '>';
float enviodatosT = 0;
float enviodatosH = 0;
void setup(void)
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(ledPower,OUTPUT);
}
void loop(void)
{
valorsensor();
DHT22_ERROR_t errorCode;
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
enviodatosT = myDHT22.getTemperatureC()/1.0;
enviodatosH = myDHT22.getHumidity()/1.0;
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
break;
}
if(enviodatosT>0)
{
LCD();
Serial.print(inicionumero);
Serial.print(enviodatosT);
Serial.print(finnumero);
Serial.println();
Serial.print(inicionumero);
Serial.print(enviodatosH);
Serial.print(finnumero);
Serial.println();
Serial.print(inicionumero);
Serial.print(dustValenvio);
Serial.print(finnumero);
Serial.println();
}
delay(2000);
}
void LCD()
{
if(enviodatosT>0)
{
lcd.setCursor(0, 0);
lcd.print("T/H ");
lcd.print(enviodatosT);
lcd.print(" ");
lcd.print(enviodatosH);
lcd.setCursor(0, 1);
lcd.print(dustVal);
lcd.print(" ");
}
}
void valorsensor()
{
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);
dustValenvio=dustVal;
}
The sending number code works very well, but when i receive the numbers it's strange because it keeps showing the same old numbers all over.
this is the code for the receiver:
#include <SD.h>
#include <Wire.h>
#include "Rtc.h"
#include <DHT22.h>
#include <stdio.h>
enum { getTemperature, getHumedad, getDust };
int whichNumber = getTemperature;
float temperature, humidity, dust;
const char inicionumero = '<';
const char finnumero = '>';
char temperatura[10];
char humedad[10];
char polvo[10];
char temperatura2[10];
char humedad2[10];
char polvo2[10];
float dustValenvio = 0.0;
float a = 0.0;
int dustPin=0;
float dustVal=0.0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
// Cable de datos conectado en pin 4 de Arduino
#define DHT22_PIN 4
// Inicio de Setup DHT22
DHT22 myDHT22(DHT22_PIN);
float enviodatosT = 0;
float enviodatosH = 0;
const int chipSelect = 8;
char time[20];
char date[20];
Rtc rtc;
void setup()
{
Serial.begin(9600);
pinMode(ledPower,OUTPUT);
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
// Serial.println("FAILED");
// don't do anything more:
return;
}
Rtc rtc = Rtc(0,0,0,1,1,1,11,0xAA);
}
void processNumber (const long n)
{
float x = n/100.0;
switch (whichNumber)
{
case getTemperature:
temperature = x;
whichNumber = getHumedad;
break;
case getHumedad:
humidity = x;
whichNumber = getDust;
break;
case getDust:
dust = x;
whichNumber = getTemperature;
break;
}
dtostrf(temperature,1,2,temperatura2);
dtostrf(humidity,1,2,humedad2);
dtostrf(dust,1,2,polvo2);
}
void processInput ()
{
static float receivedNumber = 0;
static boolean negative = false;
byte c = Serial.read ();
switch (c)
{
case finnumero:
if (negative)
processNumber (- receivedNumber);
else
processNumber (receivedNumber);
// fall through to start a new number
case inicionumero:
receivedNumber = 0;
negative = false;
break;
case '0' ... '9':
receivedNumber *= 10;
receivedNumber += c - '0';
break;
case '-':
negative = true;
break;
}
}
void loop()
{
if(Serial.available())
{
processInput();
}
valorsensor();
DHT22_ERROR_t errorCode;
// El sensor solo puede leer cada 2 segundos y requiere un tiempo de inico de 2 segundos al menos.
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
enviodatosT = myDHT22.getTemperatureC()/1.0;
dtostrf(enviodatosT,1,2,temperatura);
enviodatosH = myDHT22.getHumidity()/1.0;
dtostrf(enviodatosH,1,2,humedad);
break;
}
loggerMode();
}
void loggerMode(){
// make a string for assembling the data to log:
String dataString = "";
for (int i=0;i<20;i++){
switch (i) {
case 1:
dataString += String(temperatura);
dataString += ";";
break;
case 2:
dataString += String(temperatura2);
dataString += ";";
break;
case 3:
dataString += String(humedad);
dataString += ";";
break;
case 4:
dataString += String(humedad2);
dataString += ";";
break;
case 5:
dataString += String(polvo);
dataString += ";";
break;
case 6:
dataString += String(polvo2);
break;
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
// get Time and Date
rtc.GetDate();
rtc.Time(time);
rtc.Date(date);
// add them to dataString
dataString += ";";
dataString += String(date);
dataString += ";";
dataString += String(time);
// write file
if(enviodatosT>0 && temperature>0 && humidity>0 && dust>0)
{
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
}
if(enviodatosT>0 && temperature>0 && humidity>0 && dust>0)
{
Serial.println(dataString);
}
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.csv");
}
}
void valorsensor()
{
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);
if(dustVal<125)
{
dustValenvio = 0.0;
}
else {
a=(dustVal*5)/1023;
dustValenvio=(a-0.611)/5.1720;
}
dtostrf(dustValenvio,1,2,polvo);
}
So, the receiver code works like this...it stores data to a microSD card in order. I have one module with a temperature/humidity sensor and a dust sensor, the other module (receiver one has them too), so in the datastring the data stores like this:
tempvalue1;tempvalue2;humidity1;humidity2;dust1;dust2;date;time
tempvalue1 = temperature from sensor in receiver module
tempvalue2 = temperature from sensor in sender module
humidity1 = humidity from sensor in receiver module
humidity2= humidity from sensor in sender module
dust1 = dust from sensor in receiver module
dust2 = dust from sensor in sender module
An image speak for itself...in the image you can see that the sender values don't update with time.
Sorry for my bad english, hoping you can help me,
regards.