Ο κώδικας είναι αυτός.
Δέκτης.
Κώδικας:
#include <Wire.h>
#include <VirtualWire.h>
#include <LiquidCrystal.h>
const int numberOfAnalogPins = 4; // how many analog integer values to receive
int data[numberOfAnalogPins]; // the data buffer
int value[numberOfAnalogPins];// the number of bytes in the data buffer
const int dataBytes = numberOfAnalogPins * sizeof(int);
byte msgLength = dataBytes;
// initialize the library with the numbers of the interface pins
int i,count;
char s1[8];
float t;
float h;
int chk;
LiquidCrystal lcd(3,4,5,6,7,8);
void setup() {
// set up the LCD's number of columns and rows:
Serial.begin(9600); // Debugging only
lcd.begin(16, 2);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_rx_pin(2);
vw_rx_start(); // Start the receiver
lcd.setCursor(0,0);
lcd.print("code tmp Hmdty");
}
int j=0;
void loop() {
//Serial.print("Begin:");
if (vw_get_message((byte*)data, &msgLength)) { // Non-blocking
if(msgLength == dataBytes){
//for (int i = 0; i < numberOfAnalogPins; i++) {
h=data[1];
h=h/100;
t=data[2];
t=t/100;
chk=data[3];
Serial.print("Begin:");
Serial.print(data[0]);
Serial.print(" ");
Serial.print(t);
Serial.print(" ");
Serial.println(h);
if(data[0]==7938) {
lcd.setCursor(0,1);
lcd.print("OK");
lcd.print(chk);
lcd.print(" ");
lcd.print(t);
lcd.print(" ");
lcd.print(h);
}
}
}
}
Πομπός
Κώδικας:
#include "DHT.h"
#include <VirtualWire.h>
const int numberOfAnalogPins = 4;
int data[numberOfAnalogPins];
const int dataBytes = numberOfAnalogPins * sizeof(int);
#define DHTPIN 3 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
int id1=7938;
int chk=0;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// start serial port
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
vw_setup(2000); // Bits per sec
}
void loop(void) {
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
data[0]=id1;
data[1]=h*100;
data[2]=t*100;
data[3]=chk;
send((byte*)data, dataBytes);
chk=chk+2;
if(chk>59) chk=0;
}
void send (byte *data, int nbrOfBytes) {
//vw_setup(2000); // Bits per sec
vw_send(data, nbrOfBytes);
vw_wait_tx(); // Wait until the whole message is gone
}