【超小型 Linux 开发套件Quantum Tiny Linux】tinygl+mpu6050角度3D展示与UDP上报数据
<div class='showpostmsg'><p>完整代码见附件</p><div></div>
<p> </p>
<p>视频</p>
<p>1260e26ee8040763e9cc1cfdb342c2a2<br />
</p>
<p>b站视频</p>
<p><iframe allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1gf8TerEDt&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p>视频号</p>
<p><a href="https://mp.weixin.qq.com/s/oZflzaJbMZgLB_kjkFYBcQ">tinygl+mpu6050角度3D展示与UDP上报数据 (qq.com)</a></p>
<p> </p>
<p>前面我们实现了角度数据的获取,显示,我们还可以将数据传输到云端,如果超过范围实现告警,实现地震,塌方,位移等地质灾害得预警。</p>
<p > </p>
<p >板在wifi可以连接外网,我们使用udp协议上报数据,</p>
<p >增加udp.c udp.h 实现相关发送接口</p>
<p > </p>
<p >Udp.c如下</p>
<pre>
<code class="language-cpp">#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* socket文件描述符 */
int sock_fd = -1;
struct sockaddr_in addr_serv;
int udp_init(char* ip, int port)
{
if(sock_fd >= 0)
{
return 0;
}
/* 建立udp socket */
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_fd < 0)
{
perror("socket");
return -1;
}
/* 设置address */
memset(&addr_serv, 0, sizeof(addr_serv));
addr_serv.sin_family = AF_INET;
addr_serv.sin_addr.s_addr = inet_addr(ip);
addr_serv.sin_port = htons(port);
return 0;
}
int udp_send(char* buffer, int len)
{
int send_num;
send_num = sendto(sock_fd, buffer, len, 0, (struct sockaddr *)&addr_serv, sizeof(addr_serv));
if(send_num < 0)
{
perror("sendto error:");
return -1;
}
}
int udp_deinit(void)
{
if(sock_fd >= 0)
{
close(sock_fd);
sock_fd = -1;
}
}</code></pre>
<p>Udp.h如下</p>
<pre>
<code class="language-cpp">#ifndef UDP_H
#define UDP_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
int udp_init(char* ip, int port);
int udp_send(char* buffer, int len);
int udp_deinit(void);
#ifdef __cplusplus
}
#endif
#endif </code></pre>
<p>上报数据</p>
<pre>
<code class="language-cpp"> snprintf(text,sizeof(text),"x:%f",th);
glDrawText((GLubyte *)text, 0, 0, 0xFF0000);
udp_send(text, strlen(text));
snprintf(text,sizeof(text),"y:%f",th);
glDrawText((GLubyte *)text, 0, 12, 0x00FF00);
udp_send(text, strlen(text));
snprintf(text,sizeof(text),"z:%f",th);
glDrawText((GLubyte *)text, 0, 24, 0x0000FF);
udp_send(text, strlen(text));</code></pre>
<p>完整代码见附件</p>
<p > </p>
<p > </p>
<p >编译</p>
<p >gcc tinygl/src/*.c cube.c lcd.c mpu6050.c mpu6050_itf.c udp.c -I tinygl/include/ -o cube -lm</p>
<p > </p>
<p >运行</p>
<p >先连接wifi,</p>
<p >确认服务端ip地址 我这里是192.168.31.236</p>
<p > </p>
<p >sudo python ~/WorkSpace/System/net/connect_wifi.py CMCC-bdgQ cqmygysdss</p>
<p > </p>
<p >./cube 192.168.31.236 8243</p>
<p > </p>
<p > </p>
<p>测试效果见测试</p>
<p > </p>
<p > </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]