-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhumiditySensor.cpp
More file actions
33 lines (33 loc) · 1.15 KB
/
Copy pathhumiditySensor.cpp
File metadata and controls
33 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <dht.h>
#include <ros.h> // this is the ros library
#include <std_msgs/UInt16.h> // message type integer 16 bits for publishing analog values
#define dataPin 8 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object
std_msgs::UInt16 &humid_msg;
ros::NodeHandle node_handle; //
ros::Publisher humidity_publisher("humidity", &humid_msg);
void setup() {
Serial.begin(9600);
node_handle.advertise(humidity_publisher);
}
void loop() {
int readData = DHT.read22(dataPin); // Reads the data from the sensor
//float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity
// Printing the results on the serial monitor
// Serial.print("Temperature = ");
// Serial.print(t);
// Serial.print(" *C ");
Serial.print(" Humidity = ");
Serial.print(h);
Serial.println(" % ");
if (h>75){
humid_msg.data = 2
}
else {
humid_msg.data = 0
}
humidity_publisher.publish( &humid_msg);
node_handle.spinOnce();
delay(2000); // Delays 2 seconds, as the DHT22 so that the sampling rate is 0.5Hz
}