I am using ROS Melodic and have followed the steps for the Hello World Tutorial, it works. I have also tried uploading a sample code for the DHT11 Sensore, it sends data but not exaclty what I want.
My code can be uploaded. However, when I run the rosrun rosserial_pyhon serial_node.py /dev/ttyACM0. It shows the message: " Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino".
#include "DHT.h"
#include <ros.h>
#include <sensor_msgs/Temperature.h>
#include <sensor_msgs/RelativeHumidity.h>
ros::NodeHandle nh;
sensor_msgs::Temperature temp_msg;
sensor_msgs::RelativeHumidity humidity_msg;
ros::Publisher pub_temp("temperature", &temp_msg);
ros::Publisher pub_humidity("humidity", &humidity_msg);
//Constants
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
int chk;
int hum; //Stores humidity value
int temp; //Stores temperature value
void setup()
{
nh.initNode();
nh.advertise(pub_temp);
nh.advertise(pub_humidity);
Serial.begin(9600);
dht.begin();
}
void loop()
{
begn:delay(2000);
//Read data and store it to variables hum and temp
humidity_msg.relative_humidity = dht.readHumidity();
temp_msg.temperature= dht.readTemperature();
temp_msg.header.stamp = nh.now();
humidity_msg.header.stamp = nh.now();
//Print temp and humidity values to serial monitor
pub_temp.publish( &temp_msg );
pub_humidity.publish( &humidity_msg );
delay(2000); //Delay 2 sec.
nh.spinOnce();
}