Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* ©2018 by Daniel Eichhorn - ThingPulse (https://thingpulse.com)
*/
#include <WiFi.h>
#include <HTTPClient.h>
#define IOT_HUB_NAME "IOT HUB NAME"
#define DEVICE_NAME "DEVICE NAME"
#define SAS_TOKEN "SAS TOKEN"
const char* ssid = "yourssid"; // your network SSID (name of wifi network)
const char* password = "yourpassword"; // your network password
// Created with openssl s_client -showcerts -connect thingpulse.azure-devices.net:443
// and by picking the root certificate
const char* root_ca= \
"-----BEGIN CERTIFICATE-----\n" \
"MIIFtDCCBJygAwIBAgIQCLh6UBu+nNotFk0+OVG/VTANBgkqhkiG9w0BAQsFADBa\n" \
"MQswCQYDVQQGEwJJRTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJl\n" \
"clRydXN0MSIwIAYDVQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTE2\n" \
"MDUyMDEyNTEyOFoXDTI0MDUyMDEyNTEyOFowgYsxCzAJBgNVBAYTAlVTMRMwEQYD\n" \
"VQQIEwpXYXNoaW5ndG9uMRAwDgYDVQQHEwdSZWRtb25kMR4wHAYDVQQKExVNaWNy\n" \
"b3NvZnQgQ29ycG9yYXRpb24xFTATBgNVBAsTDE1pY3Jvc29mdCBJVDEeMBwGA1UE\n" \
"AxMVTWljcm9zb2Z0IElUIFRMUyBDQSAxMIICIjANBgkqhkiG9w0BAQEFAAOCAg8A\n" \
"MIICCgKCAgEAjvPxhHV3vL7JpPUWpVMrUGCZ3Nh92SS14XJJN0j+2oaTo30dmksQ\n" \
"TXd5fmWpfG424kfUNknQzCQCJxTirnHN2Vd0PBBWGZJKh2L545CNXt7RQRsaph9A\n" \
"oiwejlXXJthoQqvsDd7dXmGVs6xsgc6o4K2vX8qm5FFoLif9VCpxpMy7fpLx9lNR\n" \
"BTHQGYKwymPQ8koAC830aUv0WpZWOSbJnUsKYzQygKUE5eoot8EAwG0a8CjUSo+A\n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"7XhjtXPAHB874Kf/f0lRlTK40Jup5c+WA4GA1UphGP7Easbff0FGIpyAZusPQqDk\n" \
"86Qho5jQenT2jOjD0iuqK84RWRlE51wHCULr1/0VTblvbEQ1Joe6oztosIHnIMl/\n" \
"EwLzzKufHJVQy65kgLuHCl3OpmuyfeM9NuIpUbcl/NAJ47CtxGIuPn6FJrL2r/dt\n" \
"MXPGGZipcpMCzsoLPTzs2XDogPUWq3hqh03GgTeoCnaBBqjvF2B8cBATPDjXM0zk\n" \
"N2UI+5Gz6BZ2YSpl9ViUs0UB78BPA3u4\n" \
"-----END CERTIFICATE-----\n";
WiFiClientSecure client;
void sendRequest(String iothubName, String deviceName, String sasToken, String message) {
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
String url = "https://" + iothubName + ".azure-devices.net/devices/" + deviceName + "/messages/events?api-version=2016-11-14";
http.begin(url, root_ca); //Specify the URL and certificate
http.addHeader("Authorization", sasToken);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(message);
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end(); //Free the resources
}
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
}
void loop() {
sendRequest(IOT_HUB_NAME, DEVICE_NAME, SAS_TOKEN, "{temperature: 24.5}");
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.