Sending rostopic form esp32 through husarnet

asked 2019-07-13 11:50:34 -0500

khasreto gravatar image

I know that it's not so popular but maybe someone is using it. I'm trying to publish topic through husarnet to my robot, I've successfully made that in LAN but my application needs to run in different wifi networks.

Husarnet between devices with ros installed works perfectly and it don't needs any rosserial_node to be working so my question is how to subscribe topics from esp to device with ros.

Here is code of great working code in the same network:

#include <ros.h>
#include <std_msgs/String.h>
#include <WiFi.h>

#include <my_pkg/EspTrigger.h>

#define PUB_FREQ 5
#define ID 1

const char* ssid = "**********";
const char* password = "*********";
IPAddress server(***,***,***,***); // ip of your ROS server
IPAddress ip_address;
int status = WL_IDLE_STATUS;



const int motionSensor = 27;

// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

WiFiClient client;

class WiFiHardware {

  public:
  WiFiHardware() {};

  void init() {
    // do your initialization here. this probably includes TCP server/client setup
    client.connect(server, 11411);
  }

  // read a byte from the serial port. -1 = failure
  int read() {
    // implement this method so that it reads a byte from the TCP connection and returns it
    //  you may return -1 is there is an error; for example if the TCP connection is not open
    return client.read();         //will return -1 when it will works
  }

  // write data to the connection to ROS
  void write(uint8_t* data, int length) {
    // implement this so that it takes the arguments and writes or prints them to the TCP connection
    for(int i=0; i<length; i++)
      client.write(data[i]);
  }

  // returns milliseconds since start of program
  unsigned long time() {
     return millis(); // easy; did this one for you
  }
};

void setupWiFi()
{
  WiFi.begin(ssid, password);
  Serial.print("\nConnecting to "); Serial.println(ssid);
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  if(i == 21){
    Serial.print("Could not connect to"); Serial.println(ssid);
    while(1) delay(500);
  }
  Serial.print("Ready! Use ");
  Serial.print(WiFi.localIP());
  Serial.println(" to access client");
}






ros::NodeHandle_ <WiFiHardware> nh;
simulation::EspTrigger esp_msg;
//std_msgs::Bool bool_msg;
ros::Publisher motion_trigger("motion_trigger", &esp_msg);
volatile bool move = 0;

void IRAM_ATTR detectsMovement() {
  Serial.println("MOTION DETECTED!!!");
  ::move = 1;



}

void setup() {

  Serial.begin(115200);
  setupWiFi();
  delay(2000);
  pinMode(motionSensor, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
  nh.initNode();
  nh.advertise(motion_trigger);
}

void loop() {
  now = millis();
   if((now - lastTrigger) > (1000/PUB_FREQ)) {
    Serial.println("Motion stopped...");

  startTimer = false;
  esp_msg.move = ::move;
  esp_msg.id = ID;
  motion_trigger.publish( &esp_msg );
  ::move = 0;
  lastTrigger = millis();
  nh.spinOnce(); 
  }void loop() {
  now = millis();
   if((now - lastTrigger) > (1000/PUB_FREQ)) {
    Serial.println("Motion stopped...");

  startTimer = false;
  esp_msg.move = ::move;
  esp_msg.id = ID;
  motion_trigger.publish( &esp_msg );
  ::move = 0;
  lastTrigger = millis();
  nh.spinOnce(); 
  }
}
}

To subscribe this topic i need to launch rosserial node:

rosrun rosserial_python serial_node.py tcp

My attempts to run this by husarnet are:

#include <ros.h>
#include <std_msgs/String.h>
#include <WiFi.h>
#include <Husarnet.h>
#include <simulation/EspTrigger.h>

#define PUB_FREQ 1
#define ID 1

const char* ssid = "******";
const char* password = "******";

int status = WL_IDLE_STATUS;



const int motionSensor ...
(more)
edit retag flag offensive close merge delete