我正在尝试根据两个Arduinos建造我自己的气象站。设置是这样的:在房子的外面,Arduino传输数据。此时连接了两个传感器。温度和湿度。在房子内部,接收器将连接到某种显示器并显示接收的数据。两个Arduinos之间的通信通过RF链接和VirtualWire发生。
到目前为止,我设法读取传感器,传输数据,并在串行监视器中打印接收到的数据。它看起来是正确的:
22.1, 46.0,
22.1, 46.0,
22.1, 46.0,
22.1, 46.0,
22.1是温度,46.0是湿度。我的问题是我需要一些方法来分离数据,以便在LCD上很好地显示它们。一个例子是将22.1存储在一种数据类型中,将46.0存储在另一种数据类型中。不知道我该怎么做?有什么建议吗?
收件人代码:
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600);
//VirtualWire Setup
vw_setup(2000);
vw_set_rx_pin(10);
vw_rx_start();
}
void loop()
{
uint8_t buflen = VW_MAX_MESSAGE_LEN; //Maximum length of the message
uint8_t buf[buflen]; // The buffer that holds the message
if (vw_get_message(buf, &buflen))
{
buf[buflen] = '\0';
Serial.println((char *)buf);
}
}
发射器代码:
#include <VirtualWire.h>
#include <DHT.h>
//Defining DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float H = 0;
int tempPin = 0;
float calTemp;
char tString[24];
char hString[24];
char msg[27];
void setup()
{
Serial.begin(9600);
//VirtualWire setup
vw_set_tx_pin(12);
vw_setup(2000);
dht.begin();
}
void loop() {
H = dht.readHumidity();
analogReference(INTERNAL);
temp(analogRead(tempPin)); //Calculating temperature in celcius
analogReference(DEFAULT);
sendData();
delay(100);
}
void temp(int value) {
long temperature = 0;
//Reading the sensor 20 times and calculating the average reading
int aRead = 0;
for (int i = 0; i < 20; i++) {
aRead = aRead+value;
}
aRead = aRead / 20;
//Converting the temperature to celsius
temperature = ((100*1.1*aRead)/1024)*10;
// prints a value of 123 as 12.3
long hele = temperature / 10;
float deci = (temperature % 10)/10.0;
calTemp = hele + deci;
}
int sendData (){
//Converting temperature to a string
dtostrf(calTemp, 5, 1, tString);
//Converting humidity to a string
dtostrf(H, 5, 1,hString);
//Combining to one string
sprintf(msg, "%s, %s,", tString, hString);
Serial.print("message to send: [");
Serial.print(msg);
Serial.println("]");
//Sending the string
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
}