esp32与ros2的开关灯
单片机开关灯是必须的,如何告知ros2,这里用主题方式实现。需要先阅读:
- esp32与ros2的欢乐启程https://blog.csdn.net/ZhangRelay/article/details/120229431?spm=1001.2014.3001.5501
开关灯的示例如下:
#include <WiFi.h>const char* ssid = "yourssid";
const char* password = "yourpasswd";WiFiServer server(80);void setup()
{Serial.begin(115200);pinMode(5, OUTPUT); // set the LED pin modedelay(10);// We start by connecting to a WiFi networkSerial.println();Serial.println();Serial.print("Connecting to ");Serial.println(ssid);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}Serial.println("");Serial.println("WiFi connected.");Serial.println("IP address: ");Serial.println(WiFi.localIP());server.begin();}int value = 0;void loop(){WiFiClient client = server.available(); // listen for incoming clientsif (client) { // if you get a client,Serial.println("New Client."); // print a message out the serial portString currentLine = ""; // make a String to hold incoming data from the clientwhile (client.connected()) { // loop while the client's connectedif (client.available()) { // if there's bytes to read from the client,char c = client.read(); // read a byte, thenSerial.write(c); // print it out the serial monitorif (c == '\n') { // if the byte is a newline character// if the current line is blank, you got two newline characters in a row.// that's the end of the client HTTP request, so send a response:if (currentLine.length() == 0) {// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)// and a content-type so the client knows what's coming, then a blank line:client.println("HTTP/1.1 200 OK");client.println("Content-type:text/html");client.println();// the content of the HTTP response follows the header:client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>");client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>");// The HTTP response ends with another blank line:client.println();// break out of the while loop:break;} else { // if you got a newline, then clear currentLine:currentLine = "";}} else if (c != '\r') { // if you got anything else but a carriage return character,currentLine += c; // add it to the end of the currentLine}// Check to see if the client request was "GET /H" or "GET /L":if (currentLine.endsWith("GET /H")) {digitalWrite(5, HIGH); // GET /H turns the LED on}if (currentLine.endsWith("GET /L")) {digitalWrite(5, LOW); // GET /L turns the LED off}}}// close the connection:client.stop();Serial.println("Client Disconnected.");}
}
这是示例代码,一定要注意!LED端口要与电路板一致,本开发板要修改为2。
pinMode(2, OUTPUT); // set the LED pin mode
还有:
if (currentLine.endsWith("GET /H")) {digitalWrite(2, HIGH); // GET /H turns the LED on}if (currentLine.endsWith("GET /L")) {digitalWrite(2, LOW); // GET /L turns the LED off}
这和ROS2有啥关系?完全没有啊,下面直接插入ROS2相关代码,源码如下:
#include <ros2arduino.h>#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
//#include <WebServer.h>
//#include <ESPmDNS.h>#define SSID "ESP32LoveROS2"
#define SSID_PW "66666666"
#define AGENT_IP "172.20.10.2"
#define AGENT_PORT 2021 //AGENT port number#define LED 2
#define PUBLISH_FREQUENCY 1 //hzchar ledflag=2;void publishString(std_msgs::String* msg, void* arg)
{(void)(arg);static int cnt = 0;if(ledflag==2){sprintf(msg->data, "欢乐的esp32和ros2 %d", cnt++);}else if(ledflag==0){sprintf(msg->data, "欢乐的esp32灯灭了 %d", cnt++);}else if(ledflag==1){sprintf(msg->data, "欢乐的esp32灯亮了 %d", cnt++);}
}class StringPub : public ros2::Node
{
public:StringPub(): Node("esp32_pub_node"){ros2::Publisher<std_msgs::String>* publisher_ = this->createPublisher<std_msgs::String>("esp32_chatter");this->createWallFreq(PUBLISH_FREQUENCY, (ros2::CallbackFunc)publishString, nullptr, publisher_);}
};WiFiUDP udp;WiFiServer server(80);void setup()
{pinMode(LED, OUTPUT);WiFi.begin(SSID, SSID_PW);while(WiFi.status() != WL_CONNECTED);server.begin();ros2::init(&udp, AGENT_IP, AGENT_PORT);}void loop()
{static StringPub StringNode;ros2::spin(&StringNode);WiFiClient client = server.available(); // listen for incoming clientsif (client) { // if you get a client,String currentLine = ""; // make a String to hold incoming data from the clientwhile (client.connected()) { // loop while the client's connectedif (client.available()) { // if there's bytes to read from the client,char c = client.read(); // read a byte, thenif (c == '\n') { // if the byte is a newline character// if the current line is blank, you got two newline characters in a row.// that's the end of the client HTTP request, so send a response:if (currentLine.length() == 0) {// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)// and a content-type so the client knows what's coming, then a blank line:client.println("HTTP/1.1 200 OK");client.println("Content-type:text/html");client.println();// the content of the HTTP response follows the header:client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 2 on. cslg<br>");client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 2 off. cslg<br>");client.print("Click <a href=\"/Blink\">here</a> to turn the LED on pin 2 off. cslg<br>");// The HTTP response ends with another blank line:client.println();// break out of the while loop:break;} else { // if you got a newline, then clear currentLine:currentLine = "";}} else if (c != '\r') { // if you got anything else but a carriage return character,currentLine += c; // add it to the end of the currentLine}// Check to see if the client request was "GET /H" or "GET /L":if (currentLine.endsWith("GET /H")) {digitalWrite(LED, HIGH); // GET /H turns the LED on// sprintf(msg->data, "灯亮了");ledflag=1;}if (currentLine.endsWith("GET /L")) {digitalWrite(LED, LOW); // GET /L turns the LED off// sprintf(msg->data, "灯灭了");ledflag=0;}}}// close the connection:client.stop();}
}
如果不能理解这段代码,先阅读本文最初的那个链接。
效果是如下的:
后续,小乌龟和机器人都会加入进来^_^
esp32与ros2的开关灯相关推荐
- micro-ROS之esp32与ros2资料(freertos)
重中之重:micro.ros.org/docs/tutorials/core/overview/ 所有案例都是流畅稳定运行的. 比arduino+esp32+ros2稳定性好很多哦. eps32复位重 ...
- ros2订阅esp32发布的电池电压数据-补充
ros2订阅esp32发布的电池电压数据 电池电压数据能订阅但是不显示,数据QoS不匹配,需要修改. 默认: 需要使用的是外部机器人通过wifi传递的数据,设置: // create publishe ...
- 如何使用esp32从零制作一个ROS2的teleop遥控器(cmd_vel)
使用课程所用镜像,硬件连线如下图所示: 使用io口34和36,对应的arduino端口如下列表: static const uint8_t A0 = 36; static const uint8_t ...
- micro-ros arduino esp32 ros2 笔记
micro-ros micro-ros arduino 22-05-25 github.com/micro-ROS/micro_ros_arduino/releases v2.0.5 humble g ...
- 物联网开发笔记(31)- 使用Micropython开发ESP32开发板之手机扫二维码远程控制开关灯(1)
一.目的 我们分3节讲述远程控制.这一节在我们的240x240的oled屏幕上显示二维码,然后用手机扫二维码,从开发板的TCP服务器上返回字符串. 二.环境 ESP32 + 240x240的oled彩 ...
- 物联网开发笔记(32)- 使用Micropython开发ESP32开发板之手机扫二维码远程控制开关灯(2)
一.目的 上一节我们测试了远程控制的环境是好的,这一节在我们的240x240的oled屏幕上显示二维码,然后用手机扫二维码,远程控制LED灯的状态. 二.环境 ESP32 + 240x240的oled ...
- 手机端(APP点灯blinker)-PC端(Node-red)-设备端(ESP32)-客户端(MQTTX客户端)四者之间的通信——通过MQTT通信(上)
手机端(APP点灯blinker)-PC端(Node-red)-设备端(ESP32)-客户端(MQTTX客户端)四者之间的通信--通过MQTT通信(上) 前言: 本次实验是通过MQTT来进行手机端-设 ...
- ROS2机器人实验报告提示01➡入梦⬅
有学生说清醒的时候是不会选择学ROS的,太痛苦了,那索性把入门改称"入梦"吧. *注意-提供镜像使用galactic版本,实验报告为foxy,需自行修改哦! 这一节,所需内容都已经 ...
- WebServer应用示例:不到100行代码玩转Siri语音控制 | ESP32轻松学(Arduino版)
ESP32轻松学系列文章目录: ESP32 概述与 Arduino 软件准备 蓝牙翻页笔(PPT 控制器) B 站粉丝计数器 Siri 语音识别控制 LED 灯 Siri 语音识别获取传感器数据 本期 ...
最新文章
- matlab振动频谱分析是不是要,VB和Matlab混编实现振动信号的频谱分析
- jqprintsetup已经安装还会提示_英雄联盟PBE服务器安装指南 抢先体验新模式“云顶之弈”不用等...
- RHEL4-SFTP配置
- uwp应用在debug模式下运行正常,编译为release版本的时候抛出异常
- Mysql 更改密码详解及设置免密登录
- zz:测试还是开发?
- 497.非重叠矩形中的随机点
- 在android 采用 android junit test 测试注意
- 怎样屏蔽掉“网页对话框”
- 12.UniT:Multimodal Multitask Learning with a Unified Transformer
- sketchup如何给模型配置地理坐标
- UE4读取BackBuffer缓冲区贴图(屏幕表面)
- QQ企业邮箱发送邮件
- 程序员平时如何学习提高技术
- 如何确定一台电脑配置的高低
- 按“window+E”键出现【找不到应用程序】或【explore.exe找不到】的解决方法
- 服务器开发——定时器
- 英文中常见连读规律总结
- C/C++关于string.h头文件和string类
- Neo4j3-Neo4j基础操作(中)
热门文章
- Semantic Kernel 入门系列
- 深度盘点:机器学习、深度学习面试知识点3W字汇总
- 中毒了 电脑都是.php,Linux_电脑中毒Linux find命令快速查找中毒文件教程, 电脑中毒是不可避免的,L - phpStudy...
- 电脑中毒了老是自动安装软件怎么办
- 利用树莓派3和RTL-SDR V3搭建一个低成本的QRP小功率监测点
- python如何安装pil库_Python安装PIL库
- 2021年全球与中国临时起搏器行业市场规模及发展前景分析
- 谷歌获批GAN专利,一整套对抗训练网络被收入囊中
- 医院信息系统的业务功能详解
- 电池的寿命(c语言)