I post here the codes:
TRANSMITTER
#include <VirtualWire.h>
#include <VirtualWire_Config.h>
int Sensor1Pin = A1;// The pins were sensor are attached
int Sensor2Pin = A2;
int Sensor3Pin = A3;
int Sensor4Pin = A4;
int ledPin = 13;
int Sensor1Data;// The variable were the data from each sensor
int Sensor2Data;// will be stored
int Sensor3Data;
int Sensor4Data;
char Sensor1CharMsg[21];// The string that we are going to send trought rf
void setup() {
// LED
pinMode(ledPin,OUTPUT);
// Sensor(s)
pinMode(Sensor1Pin,INPUT);
pinMode(Sensor2Pin,INPUT);
pinMode(Sensor3Pin,INPUT);
pinMode(Sensor4Pin,INPUT);
// VirtualWire setup
// vw_set_ptt_inverted(true); //
vw_set_tx_pin(4);// Set the Tx pin. Default is 12
vw_setup(2000); // Bits per sec
}
void loop() {
// Read and store Sensor Data
Sensor1Data = analogRead(Sensor1Pin);
Sensor2Data = analogRead(Sensor2Pin);
Sensor3Data = analogRead(Sensor3Pin);
Sensor4Data = analogRead(Sensor4Pin);
sprintf(Sensor1CharMsg, “%d,%d,%d,%d,”, Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);
// Turn on a light to show transmitting
vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
vw_wait_tx(); // Wait until the whole message is gone
// Turn off a light after transmission
delay(40);
}
RECEIVER
//HARDWARE: ARDUINO UNO + RTC I2C + Ethernet SHIELD W5100 + Radio Receiver 433MHz DI3
#include <DS3231.h>
#include <SPI.h>
#include <SD.h>
#include <VirtualWire.h>
#include <VirtualWire_Config.h>
const int chipSelect = 4;
// Sensors
int Sensor1Data;
int Sensor2Data;
int Sensor3Data;
int Sensor4Data;
char StringReceived[22];
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
void setup()
{
// Initialize the rtc object
rtc.begin();
// The following lines can be uncommented to set the date and time
// rtc.setDOW(THURSDAY); // Set Day-of-Week to SUNDAY
// rtc.setTime(10, 6, 30); // Set the time to 12:00:00 (24hr format)
// rtc.setDate(5, 10, 2017); // Set the date to January 1st, 2014
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.print(“Initializing SD card…”);
// see if the card is present and can be initialized:
if (!SD.begin(4))
{
Serial.println(“Card failed, or not present”);
// don’t do anything more:
return;
}
Serial.println(“card initialized.”);
//initialize Radio communication
// Bits per sec
vw_setup(2000);
vw_set_rx_pin(5);
// Start the receiver PLL running
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
//Taking the data from the control base
if (vw_get_message(buf, &buflen))
{
int i;
// Message with a good checksum received, dump it.
for (i = 0; i < buflen; i++)
{
// Fill Sensor1CharMsg Char array with corresponding chars from buffer.
StringReceived = char(buf*);*
* }*
* sscanf(StringReceived, “%d,%d,%d,%d,%d,%d”,&Sensor1Data, &Sensor2Data,&Sensor3Data,&Sensor4Data); // Converts a string to an array*
* Serial.println(Sensor1Data);*
* //Serial.println(Sensor2Data);*
* //Serial.println(Sensor3Data);*
* //Serial.println(Sensor4Data); *
* }*
* memset( StringReceived, 0, sizeof( StringReceived));// This line is for reset the StringReceived*
* // Disable Ethernet by setting chip select high.*
* // digitalWrite(10, HIGH); *
* // pinMode(10, OUTPUT);*
* // initialize SD here*
* //FILE WRITING*
// 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.txt”, FILE_WRITE);
_ // if the file is available, write to it:_
_ if (dataFile) {_
_ dataFile.print(rtc.getDateStr());_
_ dataFile.print(",");_
_ dataFile.print(rtc.getTimeStr());_
_ dataFile.print(",");_
_ dataFile.print(Sensor1Data,2);_
_ // dataFile.print(",");_
_ // dataFile.print(x,2);_
_ dataFile.println(",");_
_ dataFile.close();_
_ // print to the serial port too:_
_ Serial.println(Sensor1Data);_
_ Serial.println(rtc.getTimeStr());_
_ // Serial.println(x);_
_ //delay(100);_
_ }_
_ // if the file isn’t open, pop up an error:_
else {
_ Serial.println(“error opening datalog.txt”);_
_ }*_
}