查看: 2014|回复: 2

[原创] 【 钛极小龟】---TCP通讯(采集温湿度数据上传给TCP Server端)

[复制链接]
  • TA的每日心情
    无聊
    2022-4-28 09:50
  • 签到天数: 443 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2019-1-28 16:45:06 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 独活草 于 2019-1-28 16:45 编辑

        TCP 通讯是数据采集中经常使用到的数据传输方式之一。今天研究了下用小龟进行温湿度数据采集,通过TCP通讯传输给TCP Server端的实验。

    1、wifi配置

        由于小龟板卡上只有无线wifi 固件,所以使用TCP通讯前需要先在TiDevmanager 里给小龟配置上wifi 账户和密码。

    wifi.JPG


    2、效果图
        实验内容:采集温湿度数据,显示在OLED屏上,同时串口打印输出,同时通过 TCP 通讯传输给Server端。
    实物.jpg


    服务器端.JPG
                      借鉴官网的手法,用一个网络调试软件模拟出 TCP Server 端,与小龟(TCP client) 通讯。


    3、代码

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import tijos.framework.networkcenter.dns.TiDNS;
    import tijos.framework.platform.wlan.TiWiFi;
    import tijos.framework.sensor.dht.TiDHT;
    import tijos.framework.transducer.oled.TiOLED_UG2864;
    import tijos.framework.devicecenter.TiGPIO;
    import tijos.framework.devicecenter.TiI2CMaster;
    import tijos.framework.util.Delay;

    /**
    * TCP client 例程, 在运行时请设置正确的TCP Server IP地址
    *
    * @author WS
    */
    public class TcpClient {
            public static void main(String[] args) {
                    // TCP服务器IP及PORT
                    String host = "192.168.1.201";
                    int port = 8080;
                    Socket client = null;
                    TiGPIO gpio0 = null;
                    try {
                            // 启动WLAN及DNS
                            TiWiFi.getInstance().startup(10);
                            //TiDNS.getInstance().startup();

                            // Connect to the server with TCP
                            client = new Socket(host, port);
                            System.out.print("Connect : " + client.getRemoteSocketAddress() + " local " + client.getLocalSocketAddress());

                            OutputStream output = client.getOutputStream();

                            // Send data to the TCP server
                            output.write("Hello, this is client".getBytes());
                            output.flush();

                            // Get remote data from the server
                            InputStream input = client.getInputStream();
                            
                            byte[] buffer = new byte[1024];
                            
                            // GPIO资源分配,GPIO0的PIN3脚
                            gpio0 = TiGPIO.open(0, 3);
                            
                            // 创建温湿度传感器对象
                            TiDHT dht11 = new TiDHT(gpio0, 3);
                            
                            // I2C主机总线资源分配,I2C PORT0
                            TiI2CMaster i2cm0 = TiI2CMaster.open(0);
                            // I2C主机总线资源与屏幕对象绑定,屏幕地址:0x3C
                            TiOLED_UG2864 oled = new TiOLED_UG2864(i2cm0, 0x3C);
                            
                            // 屏幕开启并清屏
                            oled.turnOn();
                            oled.clear();

                            while (true) {
                                    int len = -1;
                                    len = input.read(buffer);

                                    if (len > 0) {
                                            System.out.println("message form server:" + new String(buffer, 0, len));

                                            // echo to the server
                                            output.write(buffer, 0, len);
                                            output.flush();
                                    }

                                    // 启动测量
                                    dht11.measure();

                                    String str1 = "TEMP: " + dht11.getTemperature() + "  C";
                                    String str2 = "HUMI: " + dht11.getHumidity() + "  %";
                                    System.out.println(str1);
                                    System.out.println(str2);

                                    output.write(str1.getBytes());
                                    output.flush();
                                    oled.print(1, 0, str1);

                                    output.write(str2.getBytes());
                                    output.flush();
                                    oled.print(2, 0, str2);

                                    // 延时2秒
                                    Delay.msDelay(2000);
                            }
                    } catch (IOException e) {
                            e.printStackTrace();
                    } finally {
                            try {
                                    client.close();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                    }
            }
    }


    4、总结
    ①new Socket对象是使用Java标准库:(import java.net.Socket;   client = new Socket(host, port); )
    ②发送数据给Server端使用的是Java I/O 标准库函数:output.write( )
    TCP 是用于应用程序之间的通信,使用小龟的TCP通讯时需要启动WLAN:TiWiFi.getInstance().startup(10);
    ----PS: 想去研究下Tiwifi 这个类详情,点进去看了看就头疼,官网的封装的函数说明写得不是很人性化。
                         

    回复

    使用道具 举报

  • TA的每日心情
    难过
    2021-2-27 22:16
  • 签到天数: 1568 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2019-1-30 14:08:43 | 显示全部楼层
    308.png
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2022-4-28 09:50
  • 签到天数: 443 天

    连续签到: 1 天

    [LV.9]以坛为家II

     楼主| 发表于 2019-1-31 09:14:48 | 显示全部楼层

    你的鼓励是我坚持的最大动力,谢谢
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 04:51 , Processed in 0.135004 second(s), 20 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.