2回答

1收藏

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

物联网 物联网 2821 人阅读 | 2 人回复 | 2019-01-28

本帖最后由 独活草 于 2019-1-28 16:45 编辑

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

1、wifi配置

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



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



                  借鉴官网的手法,用一个网络调试软件模拟出 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 这个类详情,点进去看了看就头疼,官网的封装的函数说明写得不是很人性化。
                     

分享到:
回复

使用道具 举报

回答|共 2 个

倒序浏览

沙发

feixiang20

发表于 2019-1-30 14:08:43 | 只看该作者


板凳

独活草

发表于 2019-1-31 09:14:48 | 只看该作者


你的鼓励是我坚持的最大动力,谢谢
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

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