diff --git a/nodeMCU_esp8266/README.md b/nodeMCU_esp8266/README.md new file mode 100644 index 0000000..5aa22a5 --- /dev/null +++ b/nodeMCU_esp8266/README.md @@ -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) diff --git a/nodeMCU_esp8266/nodeMCU_ROSserial_publisher/README.md b/nodeMCU_esp8266/nodeMCU_ROSserial_publisher/README.md new file mode 100644 index 0000000..3f2577d --- /dev/null +++ b/nodeMCU_esp8266/nodeMCU_ROSserial_publisher/README.md @@ -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 +``` diff --git a/nodeMCU_esp8266/nodeMCU_ROSserial_publisher/nodeMCU_ROSserial_publisher.ino b/nodeMCU_esp8266/nodeMCU_ROSserial_publisher/nodeMCU_ROSserial_publisher.ino new file mode 100644 index 0000000..c6ee407 --- /dev/null +++ b/nodeMCU_esp8266/nodeMCU_ROSserial_publisher/nodeMCU_ROSserial_publisher.ino @@ -0,0 +1,52 @@ +#include +#include +#include + +/* 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); +} diff --git a/nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/README.md b/nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/README.md new file mode 100644 index 0000000..90e19a3 --- /dev/null +++ b/nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/README.md @@ -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: ''" --once +``` diff --git a/nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/nodeMCU_ROSserial_subscriber.ino b/nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/nodeMCU_ROSserial_subscriber.ino new file mode 100644 index 0000000..d53b10c --- /dev/null +++ b/nodeMCU_esp8266/nodeMCU_ROSserial_subscriber/nodeMCU_ROSserial_subscriber.ino @@ -0,0 +1,53 @@ +#include +#include +#include + +/* 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 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); +}