-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-https.ino
103 lines (86 loc) · 3.08 KB
/
simple-https.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* A simple HTTPS IBM IoT example for testing connection
* his is a modified version of the sketch that goes with the developerWorks recipe: https://developer.ibm.com/recipes/tutorials/use-http-to-send-data-to-the-ibm-iot-foundation-from-an-esp8266/
* This sketch is modified to show how to use a secure https connection between the ESP32 and the Watson IoT Platform.
* Improved with guide by Abhishek Ghosh, https://thecustomizewindows.com/
* Open Serial Monitor to see output
* Also check log on IBM IoT dashboard
* ESP32 will connect once & get disconnected
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <base64.h>
#define USE_SERIAL Serial
// <------- CHANGE PARAMETERS BELOW THIS LINE ------------>
const char* ssid = "your hotspot";
const char* password = "abcdefgh";
#define ORG "abishek678"
#define DEVICE_TYPE "yourtype"
#define DEVICE_ID "your id"
#define TOKEN "your token"
#define EVENT "myEvent"
// <------- CHANGE PARAMETERS ABOVE THIS LINE ------------>
String urlPath = "/api/v0002/device/types/" DEVICE_TYPE "/devices/" DEVICE_ID "/events/" EVENT;
String urlHost = ORG ".messaging.internetofthings.ibmcloud.com";
int urlPort = 8883;
String authHeader;
void setup() {
Serial.begin(115200); Serial.println();
initWifi();
Serial.println("View the published data on Watson at: ");
if (ORG == "quickstart") {
Serial.println("https://quickstart.internetofthings.ibmcloud.com/#/device/" DEVICE_ID "/sensor/");
} else {
Serial.println("https://" ORG ".internetofthings.ibmcloud.com/dashboard/#/devices/browse/drilldown/" DEVICE_TYPE "/" DEVICE_ID);
}
if (ORG == "quickstart") {
authHeader = "";
} else {
authHeader = "Authorization: Basic " + base64::encode("use-token-auth:" TOKEN) + "\r\n";
}
}
void loop() {
doWiFiClientSecure();
delay(10000);
}
void doWiFiClientSecure() {
WiFiClientSecure client;
Serial.print("connect: "); Serial.println(urlHost);
while ( ! client.connect(urlHost.c_str(), urlPort)) {
Serial.print(".");
}
Serial.println("Connected");
String postData = String("{ \"d\": {\"aMessage\": \"") + millis()/1000 + "\"} }";
String msg = "POST " + urlPath + " HTTP/1.1\r\n"
"Host: " + urlHost + "\r\n"
"" + authHeader + ""
"Content-Type: application/json\r\n"
"Content-Length: " + postData.length() + "\r\n"
"\r\n" + postData;
client.print(msg);
Serial.print(msg);
Serial.print("\n*** Request sent, receiving response...");
while (!!!client.available()) {
delay(50);
Serial.print(".");
}
Serial.println();
Serial.println("Got response");
while(client.available()){
Serial.write(client.read());
}
Serial.println(); Serial.println("closing connection");
client.stop();
}
void initWifi() {
Serial.print("Connecting to: "); Serial.print(WiFi.SSID());
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
}