一乐电子

一乐电子百科

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

快捷登录

手机号码,快捷登录

搜索
查看: 2251|回复: 1
收起左侧

[arduino] “黑云”能干啥----跑几个演示示例

[复制链接]
发表于 2015-3-29 23:22 | 显示全部楼层 |阅读模式
本帖最后由 文句子 于 2015-3-29 23:22 编辑

经过一番甚至几番的折腾,Arduino黑云算是在您手中诞生了吧。强调了一堆术语,这个东西究竟有什么好玩的地方呢?
我觉得没有什文字比直接用几个Dem演示更形象不过的了。


同你往日玩Arduino Uno几乎没有任何区别:打开ide,加载你需要用到的库,完成代码,最后编译上传到目标板。
只是你现在手里的Arduino具备了Bridge接口,
因此现在的目标板可以操作的资源至少翻番了!
与openwrt交互,直接完成网络应用,甚至访问AR9331上扩展的某些硬件资源……

Console-
控制台通讯版的点灯
本例的演示目的:
1、体会一下bridge正常运行的一些特征
2、通过与console即ar9331控制台的通讯方式,控制arduino uno的硬件资源。

  1. #include <Console.h>

  2. const int ledPin = 13; // the pin that the LED is attached to
  3. int incomingByte;      // a variable to read incoming serial data into

  4. void setup() {
  5.   // initialize serial communication:
  6.   Bridge.begin(115200);
  7.   Console.begin();

  8.   while (!Console){
  9.     ; // wait for Console port to connect.
  10.   }
  11.   Console.println("You're connected to the Console!!!!");
  12.   Console.println("u can control the LED(13) by Type H or L  ");
  13.   // initialize the LED pin as an output:
  14.   pinMode(ledPin, OUTPUT);
  15. }

  16. void loop() {
  17.   // see if there's incoming serial data:
  18.   if (Console.available() > 0) {
  19.     // read the oldest byte in the serial buffer:
  20.     incomingByte = Console.read();
  21.     // if it's a capital H (ASCII 72), turn on the LED:
  22.     if (incomingByte == 'H') {
  23.       digitalWrite(ledPin, HIGH);
  24.     }
  25.     // if it's an L (ASCII 76) turn off the LED:
  26.     if (incomingByte == 'L') {
  27.       digitalWrite(ledPin, LOW);
  28.     }
  29.   }
  30.   delay(100);
  31. }
复制代码
点击“upload

注意:硬件上还要观察一下Uno的串口通讯指示灯,应该是类似长亮的状态。这表示我们BRIDGE运行正常
2015-03-29_222313.png
然后打开“Serial Monitor”工具,

2015-03-29_222233.png


剩下的事情简单了,就像代码里写的:输入“H”+回车就是亮灯,输入“L”+回车就是灭灯。

思考:
你觉得你敲入的字符是怎么一步一步到达Uno的呢?

Process-访问因特网上的资源

asciilogo.txtArduino官网上有一个文本文件"
本例的演示目的:
1、Process类的简单使用
2、通过cURL实现互联网使用
  1. #include <Process.h>
  2. #include <Console.h>

  3. void setup() {
  4.   // Initialize Bridge
  5.   Bridge.begin(115200);

  6.   // Initialize Serial
  7. Console.begin();

  8.   // Wait until a Serial Monitor is connected.
  9.   while (!Console){
  10.     ;
  11.   }

  12.   // run various example processes
  13.   runCurl();
  14. }

  15. void loop() {
  16.   // Do nothing here.
  17. }

  18. void runCurl() {
  19.   // Launch "curl" command and get Arduino ascii art logo from the network
  20.   // curl is command line program for transferring data using different internet protocols
  21.   Process p;        // Create a process and call it "p"
  22.   p.begin("curl");  // Process that launch the "curl" command
  23.   p.addParameter("http://arduino.cc/asciilogo.txt"); // Add the URL parameter to "curl"
  24.   p.run();      // Run the process and wait for its termination

  25.   // Print arduino logo over the Serial
  26.   // A process output can be read with the stream methods
  27.   while (p.available()>0) {
  28.     char c = p.read();
  29.    Console.print(c);
  30.   }
  31.   // Ensure the last bit of data is sent.
  32. //  Serial.flush();
  33. }
复制代码
upload

打开Serial Monitor
2015-03-29_225500.png
思考:有没有想到除了这个logo字符图,还可以是什么?乐联,yeelink的api行么?

Bridge-WEB浏览器版的点灯


本例的演示目的:
1、YunServer和YunClient类的使用
  1. /*
  2.   Arduino Yún Bridge example

  3. This example for the Arduino Yún shows how to use the
  4. Bridge library to access the digital and analog pins
  5. on the board through REST calls. It demonstrates how
  6. you can create your own API when using REST style
  7. calls through the browser.

  8. Possible commands created in this shetch:

  9. * "/arduino/digital/13"     -> digitalRead(13)
  10. * "/arduino/digital/13/1"   -> digitalWrite(13, HIGH)
  11. * "/arduino/analog/2/123"   -> analogWrite(2, 123)
  12. * "/arduino/analog/2"       -> analogRead(2)
  13. * "/arduino/mode/13/input"  -> pinMode(13, INPUT)
  14. * "/arduino/mode/13/output" -> pinMode(13, OUTPUT)

  15. This example code is part of the public domain

  16. http://arduino.cc/en/Tutorial/Bridge

  17. */

  18. #include <Bridge.h>
  19. #include <YunServer.h>
  20. #include <YunClient.h>

  21. // Listen to the default port 5555, the Yún webserver
  22. // will forward there all the HTTP requests you send
  23. YunServer server;

  24. void setup() {
  25.   // Bridge startup
  26.   pinMode(13, OUTPUT);
  27.   digitalWrite(13, LOW);
  28.   Bridge.begin(115200);
  29.   digitalWrite(13, HIGH);

  30.   // Listen for incoming connection only from localhost
  31.   // (no one from the external network could connect)
  32.   server.listenOnLocalhost();
  33.   server.begin();
  34. }

  35. void loop() {
  36.   // Get clients coming from server
  37.   YunClient client = server.accept();

  38.   // There is a new client?
  39.   if (client) {
  40.     // Process request
  41.     process(client);

  42.     // Close connection and free resources.
  43.     client.stop();
  44.   }

  45.   delay(50); // Poll every 50ms
  46. }

  47. void process(YunClient client) {
  48.   // read the command
  49.   String command = client.readStringUntil('/');

  50.   // is "digital" command?
  51.   if (command == "digital") {
  52.     digitalCommand(client);
  53.   }

  54.   // is "analog" command?
  55.   if (command == "analog") {
  56.     analogCommand(client);
  57.   }

  58.   // is "mode" command?
  59.   if (command == "mode") {
  60.     modeCommand(client);
  61.   }
  62. }

  63. void digitalCommand(YunClient client) {
  64.   int pin, value;

  65.   // Read pin number
  66.   pin = client.parseInt();

  67.   // If the next character is a '/' it means we have an URL
  68.   // with a value like: "/digital/13/1"
  69.   if (client.read() == '/') {
  70.     value = client.parseInt();
  71.     digitalWrite(pin, value);
  72.   }
  73.   else {
  74.     value = digitalRead(pin);
  75.   }

  76.   // Send feedback to client
  77.   client.print(F("Pin D"));
  78.   client.print(pin);
  79.   client.print(F(" set to "));
  80.   client.println(value);

  81.   // Update datastore key with the current pin value
  82.   String key = "D";
  83.   key += pin;
  84.   Bridge.put(key, String(value));
  85. }

  86. void analogCommand(YunClient client) {
  87.   int pin, value;

  88.   // Read pin number
  89.   pin = client.parseInt();

  90.   // If the next character is a '/' it means we have an URL
  91.   // with a value like: "/analog/5/120"
  92.   if (client.read() == '/') {
  93.     // Read value and execute command
  94.     value = client.parseInt();
  95.     analogWrite(pin, value);

  96.     // Send feedback to client
  97.     client.print(F("Pin D"));
  98.     client.print(pin);
  99.     client.print(F(" set to analog "));
  100.     client.println(value);

  101.     // Update datastore key with the current pin value
  102.     String key = "D";
  103.     key += pin;
  104.     Bridge.put(key, String(value));
  105.   }
  106.   else {
  107.     // Read analog pin
  108.     value = analogRead(pin);

  109.     // Send feedback to client
  110.     client.print(F("Pin A"));
  111.     client.print(pin);
  112.     client.print(F(" reads analog "));
  113.     client.println(value);

  114.     // Update datastore key with the current pin value
  115.     String key = "A";
  116.     key += pin;
  117.     Bridge.put(key, String(value));
  118.   }
  119. }

  120. void modeCommand(YunClient client) {
  121.   int pin;

  122.   // Read pin number
  123.   pin = client.parseInt();

  124.   // If the next character is not a '/' we have a malformed URL
  125.   if (client.read() != '/') {
  126.     client.println(F("error"));
  127.     return;
  128.   }

  129.   String mode = client.readStringUntil('\r');

  130.   if (mode == "input") {
  131.     pinMode(pin, INPUT);
  132.     // Send feedback to client
  133.     client.print(F("Pin D"));
  134.     client.print(pin);
  135.     client.print(F(" configured as INPUT!"));
  136.     return;
  137.   }

  138.   if (mode == "output") {
  139.     pinMode(pin, OUTPUT);
  140.     // Send feedback to client
  141.     client.print(F("Pin D"));
  142.     client.print(pin);
  143.     client.print(F(" configured as OUTPUT!"));
  144.     return;
  145.   }

  146.   client.print(F("error: invalid mode "));
  147.   client.print(mode);
  148. }
复制代码
操作:
upload

打开浏览器(注意使用兼容的浏览器,推荐chrome),登陆到黑云的路由管理界面
如下图,修改响应动作的url
2015-03-29_230743.png 2015-03-29_230931.png

应该获得以下结果
2015-03-29_231300.png



说明:
由于gpgkey的细节工作留了个尾巴,因此我们现在的访问url采用的是在luci的token后添加功能url的形式
实际上gpgkey应该是就是跨过luci的token,采用隐式的url认证,如官方说明的一样:IP+功能url 即可





发表于 2016-6-15 14:16 | 显示全部楼层
请教楼主这个功能只能在局域网执行对吗?有没可能远程以太网也能实现呢?

另外菜鸟请教,arduino只是一种编程语言对吗?能在51单片机上使用这个来编程吗?

本版积分规则

QQ|一淘宝店|手机版|商店|电子DIY套件|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2024-5-5 13:37 , Processed in 0.065150 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表