博客
关于我
wemos D1 arduino物联网开发板应用笔记7-STA模式下TCP Client通信
阅读量:601 次
发布时间:2019-03-12

本文共 1431 字,大约阅读时间需要 4 分钟。

WeMos D1STA模式下TCP客户端通信实现

模块连接AP

路由器名字:lxy2305,密码:123456789a

代码示例:

WiFi.mode(WIFI_STA);  WiFi.begin(ssid, passwd);

Socket套接字实现TCP Client流程

Socket套接字用于TCP客户端通信,流程如下:

  • 连接AP并获取局域网IP
  • 使用WiFiClient库进行TCP通信
  • WiFiClient库简介

    WiFiClient库简化了Wemos D1的TCP通信开发流程,主要包含连接、通信、获取状态三类API。其功能包括:

    • 连接服务器
    • 发送数据
    • 接收数据
    • 获取连接状态

    实例代码

    #include 
    char* ssid = "lxy2305"; char* passwd = "123456789a"; const uint16_t port = 8089; const char* host = "192.168.1.7"; WiFiClient client; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, passwd); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println("WiFi connected, local IP address:"); Serial.println(WiFi.localIP()); delay(500); if (!client.connect(host, port)) { Serial.println("connection failed"); delay(5000); } else { Serial.println("connect to tcp server success."); client.println(String("hello tcp server")); } } void loop() { String recv_data = client.readStringUntil('\r'); Serial.println(recv_data); if (recv_data.compareTo("exit") == 0) { Serial.println("closing connection"); client.stop(); } delay(200); }

    运行步骤

  • 获取电脑IP地址

    • 在CMD控制台输入ipconfig获取当前IP地址
  • 使用网络助手创建TCP Server

    • 设置端口为8089
  • 下载运行

    • 上传代码到WeMos D1,查看串口输出
  • 结语

    注意事项:

    • 确保网络助手设置正确
    • 端口号可自定义
    • 代码获取及使用支持

    如有问题,可加入QQ交流群(906015840)或关注公众号“物联网客栈”。

    转载地址:http://aahtz.baihongyu.com/

    你可能感兴趣的文章
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>