Skip to content

Commit

Permalink
wireless ROS_serial using NodeMCU
Browse files Browse the repository at this point in the history
  • Loading branch information
gsivaych committed Apr 10, 2019
1 parent 483efe6 commit da95e9f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nodeMCU_esp8266/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# [NodeMCU](https://en.wikipedia.org/wiki/NodeMCU)
- Know more about NodeMCU : [Project Page](https://www.nodemcu.com/index_en.html), [Github](https://github.com/nodemcu/nodemcu-devkit)
10 changes: 10 additions & 0 deletions nodeMCU_esp8266/nodeMCU_ROSserial_publisher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## NodeMCU Esp8266 ROS_serial Publisher
Upload [sketch](https://www.arduino.cc/en/tutorial/sketch) to NodeMCU from [Arduino IDE](https://www.arduino.cc/en/main/software).
Connect to wireless access point by ESP.

Echo msgs published by `chatter`, run each in separate terminals
```
$ roscore
$ rosrun rosserial_python serial_node.py tcp
$ rostopic echo /chatter
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <ESP8266WiFi.h>
#include <ros.h>
#include <std_msgs/String.h>

/* ESP AP Config. */
#ifndef APSSID
#define APSSID "ESP_AP"
#define APPSK "ROSserial@nodeMCU"
#endif

const char *ssid = APSSID;
const char *password = APPSK;

/* ROS */
IPAddress server(192,168,4,2);
const uint16_t serverPort = 11411;

ros::NodeHandle nh;
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char firstMsg[5] = "Hey,"; bool recievedFirstMsg = false;
char msgtxt[19] = "Message Recieved !";

void setup() {
ESP.eraseConfig();
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
delay(1000);

nh.getHardware()->setConnection(server, serverPort);
nh.initNode();
nh.advertise(chatter);
}

void loop() {
if (nh.connected()) {
if(!recievedFirstMsg){
str_msg.data = firstMsg;
chatter.publish( &str_msg );
recievedFirstMsg = true;
}
str_msg.data = msgtxt;
chatter.publish( &str_msg );
} else {
// Don't publish unless connected..
recievedFirstMsg = false;
}
nh.spinOnce();
delay(1000);
}
10 changes: 10 additions & 0 deletions nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## NodeMCU Esp8266 ROS_serial Subscriber
Upload [sketch](https://www.arduino.cc/en/tutorial/sketch) to NodeMCU from [Arduino IDE](https://www.arduino.cc/en/main/software) and open [Serial Monitor](https://www.arduino.cc/en/guide/environment#toc12).
Connect to wireless access point by ESP.

Publish msgs to `chatter_`, run each in separate terminals
```
$ roscore
$ rosrun rosserial_python serial_node.py tcp
$ rostopic pub /chatter_ std_msgs/String "data: '<your msg here>'" --once
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <ESP8266WiFi.h>
#include <ros.h>
#include <std_msgs/String.h>

/* ESP AP Config. */
#ifndef APSSID
#define APSSID "ESP_AP"
#define APPSK "ROSserial@nodeMCU"
#endif

const char *ssid = APSSID;
const char *password = APPSK;

/* ROS */
IPAddress server(192,168,4,2);
const uint16_t serverPort = 11411;

ros::NodeHandle nh;

void messageCb(const std_msgs::String& str_msg){
String txt;
txt = str_msg.data;
Serial.println(txt);
int blinkCount = txt.length();
for(int i=1;i<=blinkCount;i++){
digitalWrite(LED_BUILTIN, HIGH-digitalRead(LED_BUILTIN));
delay(200);
digitalWrite(LED_BUILTIN, HIGH-digitalRead(LED_BUILTIN));
delay(200);
}
}

ros::Subscriber<std_msgs::String> sub("chatter_", &messageCb);

void setup() {
ESP.eraseConfig();
WiFi.disconnect(true);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
delay(1000);

Serial.begin(9600);
pinMode(LED_BUILTIN,OUTPUT);

nh.getHardware()->setConnection(server, serverPort);
nh.initNode();
nh.subscribe(sub);
}

void loop() {
nh.spinOnce();
delay(1);
}

0 comments on commit da95e9f

Please sign in to comment.