完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
1、目的
对于一些通过wifi进行数据上报的硬件产品,目前市面上大多数WIFI模块支持http,但是不支持webservice,对于项目开发来说,有些情况是软件开发了webservice接口,希望硬件通过webservice接口把数据传上来,但是硬件希望通过http上传。这样就产生了一定的不协调。那么如何更快的使硬件数据传到webservice服务呢?一般有如下思路: (1)增加一个网关,接收http数据,然后通过webservice传到服务器。有成本 (2)写一个http server接收输入,然后写一个webservice转发数据到webservice服务。 (3)通过http组webservice包的方式,通过http的方式实现与webservice服务对接。 2、主要工作 (1)配置wif模块。 主要包括设置其工作在http模式(一般还支持透传)、设置服务器IP、端口、请求方式,以及url等参数。 (2)单片机里面实现webservice数据结构打包数据。 简单来说webservice就是对http通信的数据格式有了要求,那么我们用这样的格式来传输数据,就可以达到与webservice服务通信的效果。具体是什么样的数据格式呢?对于开发来说最好手动调试一下,这样方便定位可能遇到的问题。 如何调试: 搭建webservice服务。然后写一个HttpURLConnection发送webservice结构的数据包,实现交互。这样可以抓取其通信的数据包。如使用wireshark来抓http包。 网上例子有很多:基本上几分钟就可使用eclipse搭建出来: package service; //包不要引用错了 import javax.jws.WebService; import javax.xml.ws.Endpoint; //注解@WebService不能少 @WebService public class helloService { //该方法为客户端调用的方法,方法名任意 public String say(String name){ return "Hello " + name + " , this is SayHelloService !" ; } public static void main(String[] args) { //第一个参数是访问时的url,9091是任意一个不占用的端口 Endpoint.publish("http://localhost:9091/Service/SayHello", new helloService()); System.out.println("service success !"); } } 客户端: package client.copy; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class HttpUrlConnection { public static void main(String[] args) throws Exception { String urlString = "http://localhost:9091/Service/SayHello?wsdl";//wsdl文档的地址 URL url = new URL(urlString); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();//打开连接 //String soapActionString = "http://localhost:9000/HelloWorld/sayHelloWorldFrom";//Soap 1.1中使用 String xmlFile = "soap.xml";//要发送的soap格式文件 File fileToSend = new File(xmlFile); byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组 new FileInputStream(xmlFile).read(buf); //Content-Length长度会自动进行计算 httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); //httpConn.setRequestProperty("soapActionString",soapActionString);//Soap1.1使用 其实完全可以不需要 httpConn.setRequestMethod("POST"); httpConn.setDoOutput(true); httpConn.setDoInput(true); OutputStream out = httpConn.getOutputStream(); out.write(buf); out.close(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { System.out.println("成功"); InputStreamReader is = new InputStreamReader(httpConn.getInputStream()); BufferedReader in = new BufferedReader(is); String inputLine; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("result.xml")));// 将结果存放的位置 while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); bw.write(inputLine); bw.newLine(); } bw.close(); in.close(); } else{ System.out.println("失败"); //如果服务器返回的HTTP状态不是HTTP_OK,则表示发生了错误,此时可以通过如下方法了解错误原因。 InputStream is = httpConn.getErrorStream(); //通过getErrorStream了解错误的详情,因为错误详情也以XML格式返回,因此也可以用JDOM来获取。 InputStreamReader isr = new InputStreamReader(is,"utf-8"); BufferedReader in = new BufferedReader(isr); String inputLine; BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("result.xml")));// 将结果存放的位置 while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); bw.write(inputLine); bw.newLine(); } bw.close(); in.close(); } httpConn.disconnect(); } } 这样的话我们运行客户端代码,就可以通过http的方式,将数据传给webservice。这个过程一定要用抓包工具抓取来看一下,接下来的工作就是让单片机发送这样格式的数据包,然后通过wifi的http方式传到webservice服务器。 单片机要发什么样子的数据包呢?其实就是: {"fami_code":"1001","userId":"1001","roomId":"1","temprature":"29.17","time":"2019-05-16 12:30:55","humidity":"20.12","co2":"438.50","pm2.5":"16.00","nh3":"0.00","ch2o":"18.75","no2":"0.00","so2":"0.00","co":"0.00"} 将其转换为字符串HEX,然后通过串口发送给wifi模块即可。当然要替换掉上面中的数据内容为希望上传的传感器数据。 至于数据格式就需要和软件来协调了,比如存在哪些字段。 |
|
|
|
只有小组成员才能发言,加入小组>>
imx6ull 和 lan8742 工作起来不正常, ping 老是丢包
2722 浏览 0 评论
3348 浏览 9 评论
3026 浏览 16 评论
3522 浏览 1 评论
9128 浏览 16 评论
1255浏览 3评论
643浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
634浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2382浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1946浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-2-1 15:57 , Processed in 1.012673 second(s), Total 46, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号