ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Trying to publish data from DHT22 Sensor from Arduino to ROS using rosserial

asked 2020-04-22 03:01:25 -0500

SirMacdonald gravatar image

updated 2022-01-22 16:10:30 -0500

Evgeny gravatar image

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();

}
edit retag flag offensive close merge delete

Comments

delay(2000);

I can't be certain, but I believe delays of multiple seconds are too long for rosserial. I would suggest to try and reduce it and see whether things start working.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-22 04:01:24 -0500 )edit

I have tried both reducing and removing the delay, it doesn't seem to have any effect.

SirMacdonald gravatar image SirMacdonald  ( 2020-04-22 20:37:45 -0500 )edit
1

I don't see Serial.begin(9600) used anywhere in the rosserial tutorials. Did you add that yourself?

Also:

removing the delay

I don't believe removing it would be a good idea necessarily.

Unfortunately I don't use rosserial myself enough to provide any additional guidance.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-23 05:17:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-04-23 21:08:15 -0500

SirMacdonald gravatar image

I got it, I was combining it with the DHT22 Temperature sensor and thought it needed the Serial begin, thank you!

edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-04-22 03:01:25 -0500

Seen: 347 times

Last updated: Apr 22 '20