使用REST

时间:2017-01-27 09:15:27

标签: c azure arduino azure-eventhub arduino-esp8266

我可以使用NodeMCU将事件发送到事件中心。我使用以下代码

https://www.hackster.io/stepanb/proof-of-concept-nodemcu-arduino-and-azure-event-hub-a33043

但我想发送批处理事件。我在这个页面上提到了

https://docs.microsoft.com/en-us/rest/api/eventhub/send-batch-events

这是NodeMCU发送单一事件的串行监视器的快照

single event

根据链接,我们必须更改"内容类型"到

Content-Type: application/vnd.microsoft.servicebus.json

以及

格式的有效载荷
data= "[{'Temperature':25.25 , 'Deviceid':esp3} , {'Temperature':30.30 , 'Deviceid':esp3}]";

这是批处理事件的串行监视器快照

batch events

我错过了什么吗?为了接收批处理事件,我需要对流分析进行任何更改。我是Azure和StackOverflow的新手。

这里是发送单个事件的代码

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"

// START: Azure Evet Hub settings
const char* KEY = "dhGE6MbbRLe6IPZs6dOHd3byQlEJ8YzqnW+uBAT7T/Q=";   // main event hub key 
const char* KEY_NAME = "RootManageSharedAccessKey";                 //  key name
const char* HOST = "rishieventhub2.servicebus.windows.net";         //  event hub name
const char* END_POINT = "/rishidata/messages";                      // name of eventhub created inside event hub
// END: Azure Evet Hub settings

// START: WiFi settings
const char* SSID = "Nokia";
const char* PASSWORD = "rishikesh";
// END: WiFi settings
String request;
String data;
String fullSas;
 WiFiClientSecure client;
void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println();

  // START: Naive URL Encode
  String url = "https://" + (String)HOST + (String)END_POINT;
  url.replace(":", "%3A");
  url.replace("/", "%2F");
  Serial.println(url);
  // END: Naive URL Encode

  // START: Create SAS
  // https://azure.microsoft.com/en-us/documentation/articles/service-bus-sas-overview/
  // Where to get secods since the epoch: local service, SNTP, RTC
  int expire = 1711104241;
  String stringToSign = url + "\n" + expire;

  // START: Create signature
  Sha256.initHmac((const uint8_t*)KEY, 44);
  Sha256.print(stringToSign);
  char* sign = (char*) Sha256.resultHmac();
  int signLen = 32;
  // END: Create signature

  // START: Get base64 of signature
  int encodedSignLen = base64_enc_len(signLen);
  char encodedSign[encodedSignLen];
  base64_encode(encodedSign, sign, signLen); 
  String encodedSas = (String) encodedSign;
  // Naive URL encode
  encodedSas.replace("=", "%3D");
  Serial.println(encodedSas);
  // END: Get base64 of signature

  // SharedAccessSignature
   fullSas = "sr=" + url + "&sig="+ encodedSas + "&se=" + expire +"&skn=" + KEY_NAME;
  // END: create SAS

  // START: Wifi connection
  Serial.print("connecting to ");
  Serial.println(SSID);

  WiFi.begin(SSID, PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  // END: Wifi connection

}
int temp=15;
void loop() {
   WiFiClientSecure client;
  if (!client.connect(HOST, 443)) {
    Serial.println("connection failed");
    return;
  }

  //data= "[{'Temperature':25.25 , 'Deviceid':'esp3'} , {'Temperature':30.30 , 'Deviceid':'esp3'}]";  // for batch events  
   data = "{'Temperature':25.25 , 'Deviceid':'esp3'}"; // for single events 
   request = String("POST ")+"https://rishieventhub2.servicebus.windows.net" + END_POINT + "?timeout=60&api-version=2014-01" + " HTTP/1.1\r\n" +
               "Host: " + HOST + "\r\n" +
               "Authorization: SharedAccessSignature " + fullSas + "\r\n" +                
               "Content-Type: application/atom+xml;type=entry;charset=utf-8\r\n" + 
               "Content-Length: " + data.length() + "\r\n\r\n" +
               data;
    Serial.println(request);
    client.print(request);

 delay(100);

发送单个事件的代码工作,我能够将值存储在表存储中,我可以看到使用azure表存储。 这是我尝试发送批处理事件的代码

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"

// START: Azure Evet Hub settings
const char* KEY = "dhGE6MbbRLe6IPZs6dOHd3byQlEJ8YzqnW+uBAT7T/Q=";   // main event hub key 
const char* KEY_NAME = "RootManageSharedAccessKey";                 //  key name
const char* HOST = "rishieventhub2.servicebus.windows.net";         //  event hub name
const char* END_POINT = "/rishidata/messages";                      // name of eventhub created inside event hub
// END: Azure Evet Hub settings

// START: WiFi settings
const char* SSID = "Nokia";
const char* PASSWORD = "rishikesh";
// END: WiFi settings
String request;
String data;
String fullSas;
 WiFiClientSecure client;
void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println();

  // START: Naive URL Encode
  String url = "https://" + (String)HOST + (String)END_POINT;
  url.replace(":", "%3A");
  url.replace("/", "%2F");
  Serial.println(url);
  // END: Naive URL Encode

  // START: Create SAS
  // https://azure.microsoft.com/en-us/documentation/articles/service-bus-sas-overview/
  // Where to get secods since the epoch: local service, SNTP, RTC
  int expire = 1711104241;
  String stringToSign = url + "\n" + expire;

  // START: Create signature
  Sha256.initHmac((const uint8_t*)KEY, 44);
  Sha256.print(stringToSign);
  char* sign = (char*) Sha256.resultHmac();
  int signLen = 32;
  // END: Create signature

  // START: Get base64 of signature
  int encodedSignLen = base64_enc_len(signLen);
  char encodedSign[encodedSignLen];
  base64_encode(encodedSign, sign, signLen); 
  String encodedSas = (String) encodedSign;
  // Naive URL encode
  encodedSas.replace("=", "%3D");
  Serial.println(encodedSas);
  // END: Get base64 of signature

  // SharedAccessSignature
   fullSas = "sr=" + url + "&sig="+ encodedSas + "&se=" + expire +"&skn=" + KEY_NAME;
  // END: create SAS

  // START: Wifi connection
  Serial.print("connecting to ");
  Serial.println(SSID);

  WiFi.begin(SSID, PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  // END: Wifi connection

}
int temp=15;
void loop() {
   WiFiClientSecure client;
  if (!client.connect(HOST, 443)) {
    Serial.println("connection failed");
    return;
  }

   data= "[{'Temperature':25.25 , 'Deviceid':'esp3'} , {'Temperature':30.30 , 'Deviceid':'esp3'}]";  // for batch events  
  // data = "{'Temperature':25.25 , 'Deviceid':'esp3'}"; // for single events 
   request = String("POST ")+"https://rishieventhub2.servicebus.windows.net" + END_POINT + "?timeout=60&api-version=2014-01" + " HTTP/1.1\r\n" +
               "Host: " + HOST + "\r\n" +
               "Authorization: SharedAccessSignature " + fullSas + "\r\n" +                
               "Content-Type: application/vnd.microsoft.servicebus.json" +"\r\n" + 
               "Content-Length: " + data.length() + "\r\n\r\n" +
               data;
    Serial.println(request);
    client.print(request);

 delay(100);

}

1 个答案:

答案 0 :(得分:0)

以上代码适用于发送批处理事件。要改变的一件事是,在流分析中,我们需要不同的密钥用于分区密钥和行密钥用于输出(表存储)。对于发送单个事件,分区和行键可以相同。