65.9K
CodeProject 正在变化。 阅读更多。
Home

Azure 与 Edison 的连接以及使用 Windows Phone 应用对其进行控制

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2016年2月18日

CPOL

7分钟阅读

viewsIcon

28520

如何使用 Azure 移动服务控制您的 Edison。

获取全新的英特尔® 物联网开发套件,这是一个完整的硬件和软件解决方案,让开发者能够利用英特尔® Galileo 和英特尔® Edison 开发板创建令人兴奋的新解决方案。访问英特尔® 物联网开发者专区

引言

本文将指导您完成 Edison 与 Windows Azure 连接并进行一些很酷的操作(如控制它)的过程。此示例将仅打开和关闭连接到 Edison 开发板上任意数字引脚的 LED,该开发板与 Arduino 扩展板配对使用。让我们看看它是如何真正工作的。Azure 移动服务将充当 Edison 和控制器(此处使用 Windows Phone 应用程序)之间的桥梁。Edison 将从移动服务的表中读取属性值。Windows Phone 应用程序将更新表中的值,从而我们将根据值更改 Edison 代码。

先决条件

假设读者对 Windows Phone 应用程序开发有基本了解,并且可以将应用程序与 Azure 移动服务连接。如果您不了解如何为应用程序创建后端,请参阅此链接

所需物品(硬件)

  1. 带 Arduino 扩展板的英特尔 Edison
  2. 一个 LED

所需软件

  1. Visual Studio
  2. Arduino IDE

所需服务

1:Azure 移动服务

首先,让我们先创建一个 Azure 移动服务。关于创建 Azure 移动服务的文章有很多。按照此链接创建一个移动服务,其中包含一个名为 controltable 的表。该表将包含一个名为“status”的列和一个名为“name”的列。status 列的值将为 1 或 0。而 name 的值将设置为 Edison/Arduino。

现在我们将开发一个 Windows Phone 应用程序,它将添加/修改属性的值。下图显示了 Azure SQL 表的屏幕截图。请注意,“device”属性的值无关紧要。如果您愿意,甚至可以排除此属性。

下一节将介绍 Windows Phone 应用程序的开发。

Windows Phone 应用

我们的应用将仅包含两个按钮:一个按钮和一个按钮。应用的屏幕截图如下所示。

你可以忽略注销按钮,实际上,我当时正在尝试添加一些功能。那么这些按钮会做什么呢?它们将更新“status”属性的值。当我们按下开按钮时,“status”的值将为 1,否则为 0。

不过,有一个小问题。如果什么都没有创建,该更新什么呢?为此,我们将部署应用程序两次。第一次,我们将创建表并分配一些默认值。下一次,我们将只更新之前更新过的值。

现在,我们的应用程序已准备就绪。测试应用程序并检查“status”的值是否正在更新。一旦它工作了,那么你就完成了。应用程序已准备就绪。下一部分将重点介绍 Edison 代码。

爱迪生代码

让我们进入 Edison。在为 Edison 启动代码之前,请按照此处提到的初始步骤配置您的 Edison。连接 Edison 后,记下您的端口号。然后打开您的 Arduino IDE 并从开发板中选择 Intel Edison。如果您找不到 Intel Edison 选项,则需要从开发板管理器选项下载必要的文件。

打开 Arduino IDE 后,将提前提到两个函数:void `setup()` 和 void `loop()`。Edison 内置了 Wi-Fi。我们将使用 Wi-Fi 将其连接到互联网。因此,我们的第一个操作是包含一个 Wi-Fi 库。转到 Sketch->include library->Wi-Fi。之后,让我们添加此代码以将其连接到 Wi-Fi。

#include <SPI.h>
#include <WiFi.h>
WiFiClient client;


char ssid[] = "networkssid"; //  your network SSID (name) 
char pass[] = "password";    // your net ork password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;

void setup()
{
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 

  String fv = WiFi.firmwareVersion();
  if( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");
  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);  
  }
}

以上代码负责通过 Wi-Fi 将 Edison 连接到互联网。此外,我们将 PIN 13 设置为输出模式,当前状态为关闭。我们将使用 IDE 的串行监视器来监视过程。现在,让我们转到 `void loop()`。从 Azure 检索数据是通过 http get 方法完成的。

void loop()
{
  send_request();
  wait_response();
  read_response();
  end_request();
  delay(100);
}

这些函数将从 Azure 表中检索数据。但在深入研究这些方法之前,我们需要添加一些全局变量来将我们的 Edison 连接到 Azure 服务。添加这些全局变量。

const char* server= "YourAzureService.azure-mobile.net";
const char* table_name= "TableName";
const char* ams_key="YourApplicationKey";
char buffer[64];

应用程序密钥可以在您的 Azure 门户中管理密钥按钮处找到。现在,我们将在 `void loop()` 中编写方法的代码。

void send_request()
{
  Serial.println("\nconnecting...");

  if (client.connect(server, 80))
 {

    sprintf(buffer, "GET /tables/%s HTTP/1.1", table_name);
    client.println(buffer);

    // Host header
    sprintf(buffer, "Host: %s", server);
    client.println(buffer);

    // Azure Mobile Services application key
    sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key);
    client.println(buffer);

    // JSON content type
    client.println("Content-Type: application/json");

    // Content length
    client.print("Content-Length: ");
    client.println(strlen(buffer));

    // End of headers
    client.println();

    // Request body
    client.println(buffer);
    
  } 
else 
{
    Serial.println("connection failed");
  }
}

我们执行了一个 HTTP 请求并调用了 GET,其中我们之前指明了表的名称、服务器名称和代码密钥,这允许正确检索数据。然后我们指定需要以何种格式检索数据,并将其指定为 JSON。让我们编写 `wait_response()` 的代码

void wait_response()
{
  while (!client.available()) 
  {
    if (!client.connected()) 
    {
      return;
    }
  }
}

然后我们需要读取检索到的数据。由于它是 JSON 格式,我们需要解析 JSON 字符串以获取我们所需的值。下面是示例字符串的示例。

{"id":"2492D996-C471-48F0-B3C9-F33E3B37477F","status":"0","name":"arduino"}

存在一个名为 ArduinoJson 的高效库。它将完成大部分解析工作。但是检索到的 JSON 字符串用 '[' 和 ']' 括起来。这些必须删除才能使库工作。因此,首先您需要在代码中包含该库并添加以下全局变量并添加

#include <ArduinoJson.h>
#define RESPONSE_JSON_DATA_LINENNO 10
int charIndex=0;
StaticJsonBuffer<200> jsonbuffer;

然后在你的 `read_response` 方法中编写以下代码()。

void read_response()
{
  boolean bodyStarted;
  int jsonStringLength;
  int jsonBufferCntr=0;
  int numline=RESPONSE_JSON_DATA_LINENNO;
  //Ignore the response except for the 10th line
  while (client.available()) 
  {
    //Serial.println("Reading:");
    char c = client.read();  
    if (c == '\n')
    {
      numline -=1;
    }
    else 
    {
      if (numline == 0 && (c!='[')&& (c!=']') )
      {
        buffer[jsonBufferCntr++] = c; 
        buffer[jsonBufferCntr] = '\0'; 
      }
    }
  }
  Serial.println("Received:");
  Serial.println(buffer);
  Serial.println("");
  parse();
}

上面的代码将读取响应,而解析方法负责解码字符串。`parse()` 方法如下所示。在 `Parse()` 方法中,我们将更改 PIN13 的状态。

void parse()
{
  StaticJsonBuffer<150> jsonbuffer;
  JsonObject& root = jsonbuffer.parseObject(buffer);
  if(!root.success())
  {
    Serial.println("PARSING FAILED!!!");
    return;
  }
  int f= root["status"];
  Serial.println("Decoded: ");
  Serial.println(f);
  if(f==0)
    digitalWrite(13,LOW);
  else
    digitalWrite(13,HIGH);
}

这里,在上面的代码中,f 存储了状态属性的值。然后,我们检查 f 的值并最终将 PIN 设置为 HIGH 或 LOW。

有关 ArduinoJson 库的详细文档,请访问此链接。但是,此库有一个小问题。稍后将讨论。现在,我们将编写其余方法的代码。

void end_request()
{
  client.stop();
}

现在,您会看到,当您编译代码时,很可能会遇到一些错误。这些错误需要消除。在进一步操作之前,请查看此问题。`WString.h` 文件缺少一些代码行。差异可以在这里看到。您需要更新位于此处的文件。

C:\Users\Username\AppData\Roaming\Arduino15\packages\Intel\hardware\i686\1.6.2+1.0\cores\arduino\WString.h

更新后,您的错误将得到解决。编译代码后,将其烧录到 Edison 中,然后就完成了。将代码烧录到 Edison 中后,取一个 LED,将 LED 的长腿连接到 PIN13,短腿连接到 Gnd。可选的 233 欧姆电阻器可以与长腿串联。因此,我们通过 Windows Azure 控制的 Windows Phone Edison 已准备就绪。Edison 运行时的串行窗口截图如下所示。

第一阶段是 Edison 尝试连接到 Wi-Fi 网络。

下一阶段是 Edison 尝试连接到 Azure 移动服务。连接后,它获取 JSON 数据,然后由 JSON 库解码,然后我们获得解码结果。解码结果负责 Edison 上 PIN13 的状态。

结论

因此,我们在这篇文章中学习了如何使用 Azure 移动服务控制您的 Edison。移动服务充当一个桥梁。Windows Phone 应用程序更改 Azure 表中的数据,最终控制了 Edison。本文没有涵盖 Windows Phone 开发,但查看链接会为您提供所需的信息。整个 Edison 代码已作为文本文件上传。视频的 YouTube 链接是这里

© . All rights reserved.