2022
我们一起努力

嵌入式单片机stm32串口通讯实验,怎么把数据传到服务器

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服务通信的效果。具体是什么样的数据格式呢?对于开发来说最好手动调试一下,这样方便定位可能遇到的问题。

嵌入式单片机stm32串口通讯实验,怎么把数据传到服务器

如何调试:

搭建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服务器。

单片机要发什么样子的数据包呢?其实就是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://serve/"><soapenv:Header/><soapenv:Body><ser:syncRecord><arg0>
{"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"}
</arg0></ser:syncRecord></soapenv:Body></soapenv:Envelope>

将其转换为字符串HEX,然后通过串口发送给wifi模块即可。当然要替换掉上面中的数据内容为希望上传的传感器数据。

至于数据格式就需要和软件来协调了,比如存在哪些字段。

文章知识点与官方知识档案匹配,可进一步学习相关知识

赞(0)
文章名称:《嵌入式单片机stm32串口通讯实验,怎么把数据传到服务器》
文章链接:https://www.fzvps.com/51999.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!