lovesvidon的个人博客分享 http://blog.sciencenet.cn/u/lovesvidon

博文

【经验】如何通过WIFI连接Arduino Yun和Processing

已有 3127 次阅读 2018-2-5 12:41 |个人分类:写着玩|系统分类:科研笔记

COMMUNICATION BETWEEN PC AND ARDUINO YUN VIA WIFI:
The core codes of setting SSHD is from eecharlie. Please see:
https://forum.processing.org/two/discussion/328/connecting-processing-to-arduino-yun-yun

这里稍微解释以下核心的processing code
第一步,在你的processing项目中包含可以利用的SSH协议包,比如这里使用的JSch。关于JSch的使用可以参考:

http://www.jcraft.com/jsch/

第二步,利用SSH成功登陆Arduino Yun的根服务器,所以需要在写processing code时设定:

String user ="root";

String password ="******";

String host ="192.168.240.1";int port=22;


如果你没有更改Arduino Yun的根服务器地址,那么默认地址应该是192.168.240.1,不论怎样,搞清楚你的host
第三步,按照协议写好输入流与输出流的code:

Session session = jsch.getSession(user, host, port);

session.setPassword(password);

//设定user, host port和密码

session.setConfig("StrictHostKeyChecking","no");

// less than maximally secure

session.connect();

//尝试连接根服务器    

System.out.println("Connection established.");

Channel channel = session.openChannel("exec");

channel.setInputStream(null);

((ChannelExec) channel).setCommand("telnet localhost 6571");

//连接成功后用telnet命令调出传输数据     channel.connect();



下面主要是如何将接受的数据流转换成可应用的double型数据的问题。
注意一下,这里考虑的是在Arduino Yun里使用Console.printl(Ver)直接传输数据变量。如果你在Arduino Yun里就建立好传输规则,规定好每个变量传输的格式,那么就可以在processing里规定好每个流有多少字符,每个字符的作用,这样可以大幅度提升传输效率。这里所给出的是在Arduino Yun里没有传输规则的情形,所以每个变量的字符长度不一样,这样每次循环只能设定为每个流有一个字符,这样就出现了接受不同步的现象。同样,下面也给出了解决方案。

Results and further input will be handled by the streams.
Each loop transfers one byte data. Each charactor occupied one byte.
If the Arduino Yun sends numbers, for example -5.63, to this clinet, the complete stream is following:
(The data from Arduino alwasys keeps two decimal places like -5.63, 4.57, 18.71 if you use fuction Console.println().)
Loop count |First Loop|Second Loop|Third Loop|Fourth Loop|Fifth Loop|Sixth Loop|Seventh Loop|
Charactors |   empty  |    -      |    5     |     .     |    6     |     3    |    '\n'    |
The ideas to transfer the stream to float numbers are following:
1. Starting after receiving '\n'.
2. Receiving all numbers and put them into one array.
3. Transfer elements in array to a decimal number.
4. Determining the sign.

DATA TRANSFER SYNCHRONIZATION
In Arduino Yun, the program loop time is approx 110ms.  
在Arduino Yun里精确设定好循环时间。
The client needs receive 7 data and each data needs at least 7 cycles.
设定好需要传输的变量个数。
So, to keep the synchronism for receiving and sending, the client program needs finish 49 cycles in 110ms.
The defult value of framerate for processing is 60/s. Here, the framerate should be larger than 445/s.

计算好processing的framerate。


ArduinoYunToProcessing.pdf



https://blog.sciencenet.cn/blog-3189881-1098518.html

上一篇:三极管和MOS小信号电路的快速分析方法
下一篇:【python爬虫】抓取B站视频相关信息(一)
收藏 IP: 36.57.157.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 01:41

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部