eps32和ros2之稳稳点亮一个LED灯(IO4)
2023-03-31 10:33:34 时间
源码:
#include <ros2arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
//#include <WebServer.h>
//#include <ESPmDNS.h>
#define SSID "***"
#define SSID_PW "***"
#define AGENT_IP "***"
#define AGENT_PORT *** //AGENT port number
#define LED 4
#define PUBLISH_FREQUENCY 1 //hz
char 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 clients
if (client) { // if you get a client,
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (c == '
') { // 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 4 on. cslg<br>");
client.print("Click <a href="/L">here</a> to turn the LED on pin 4 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 != '
') { // 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, LOW); // GET /H turns the LED on
// sprintf(msg->data, "灯亮了");
ledflag=1;
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED, HIGH); // GET /L turns the LED off
// sprintf(msg->data, "灯灭了");
ledflag=0;
}
}
}
// close the connection:
client.stop();
}
}
需要自己依据网络情况配置。
默认灯亮,显示欢乐esp32和ros2。
点击off,效果如下:
点击on
显示1s一次,有滞后,再关闭。
多次测试后:
稳定工作10分钟,问题不大。最多测试1小时。
-后续,esp32做成ros2机器人遥控器-
相关文章
- 金融服务领域的大数据:即时分析
- 影响大数据、机器学习和人工智能未来发展的8个因素
- 从0开始构建一个属于你自己的PHP框架
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- SEO公司使用大数据优化其模型的5种方法
- 关于Web Workers你需要了解的七件事
- 深入理解HTTPS原理、过程与实践
- 增强分析:数据和分析的未来
- PHP协程实现过程详解
- AI专家:大数据知识图谱——实战经验总结
- 关于PHP的错误机制总结
- 利用数据分析量化协同过滤算法的两大常见难题
- 怎么做大数据工作流调度系统?大厂架构师一语点破!
- 2019大数据处理必备的十大工具,从Linux到架构师必修
- OpenCV中的KMeans算法介绍与应用
- 教大家如果搭建一套phpstorm+wamp+xdebug调试PHP的环境
- CentOS下三种PHP拓展安装方法
- Go语言HTTP Server源码分析
- Go语言HTTP Server源码分析
- 2017年4月编程语言排行榜:Hack首次进入前五十