Štěpán Bechynský
Published

Proof of Concept – NodeMCU, Arduino and Azure Event Hub

Using ESP8266 to show how to use HTTPS via Azure Event Hub

IntermediateProtip9,581
Proof of Concept – NodeMCU, Arduino and Azure Event Hub

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1

Software apps and online services

Microsoft Azure
Microsoft Azure
NodeMCU firmware
NodeMCU firmware

Story

Read more

Schematics

Proof of Concepts – NodeMCU, Arduino and Azure Event Hub

Source code for Arduino IDE with ESP support.

Code

Sample code

Arduino
Put your settings instead of ***
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"

// START: Azure Evet Hub settings
const char* KEY = "***";
const char* KEY_NAME = "***";
const char* HOST = "***.servicebus.windows.net";
const char* END_POINT = "/***/publishers/***/messages";
// END: Azure Evet Hub settings

// START: WiFi settings
const char* SSID = "***";
const char* PASSWORD = "***";
// END: WiFi settings

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 = 1511104241;
  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
  String 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

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  if (!client.connect(HOST, 443)) {
    Serial.println("connection failed");
    return;
  }

  // https://msdn.microsoft.com/en-us/library/azure/dn790664.aspx
  String data = "{message: 'Hello'}";
  String request = String("POST ") + END_POINT + " 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);

  String response = "";
  while (client.connected()) {
    response += client.readStringUntil('\n');
  }

  Serial.println();
  Serial.print("Response code: ");
  Serial.println(response.substring(9, 12));
}

void loop() {
}

Credits

Štěpán Bechynský

Štěpán Bechynský

8 projects • 30 followers
Internet of Things, Arduino, Microsoft, ex-Microsoft MVP

Comments