如何通过WIFI连接Arduino Yun和Processing

  • 日期:2018-04-16        来源:四川大学学术诚信与科学探索网         点击数:


  • 如何通过WIFI连接Arduino Yun和Processing

    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。

    (本文转自科学网)