Last active
August 29, 2020 05:16
-
-
Save ksasao/3186a84696dc9b725e10fb5a74aab153 to your computer and use it in GitHub Desktop.
M5Stack HTTPS Client with Azure Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5Stack.h> | |
#include <WiFiClientSecure.h> | |
WiFiClientSecure client; | |
const char* ssid = "your SSID"; // WiFi SSID | |
const char* password = "your WiFi password"; // WiFi PW | |
const char* host = "host name (e.g. example.azurewebsites.net)"; | |
const char* path = "url (e.g. /api/EnvMonitor?code=AAA==)"; | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.setTextColor(WHITE); | |
M5.Lcd.setTextSize(2); | |
if (wifiConnection()) { | |
String data = "{\"name\":\"M5Stack\"}"; | |
Serial.println("POST to https://" + String(host) + path); | |
String response = httpsPost(path, data); | |
Serial.println(response); | |
M5.Lcd.println(response); | |
} | |
} | |
void loop() { | |
} | |
boolean wifiConnection() { | |
WiFi.begin(ssid,password); | |
int count = 0; | |
Serial.print("Waiting for Wi-Fi connection"); | |
while ( count < 20 ) { | |
if (WiFi.status() == WL_CONNECTED) { | |
Serial.println(); | |
Serial.println("Connected!"); | |
return (true); | |
} | |
delay(500); | |
Serial.print("."); | |
count++; | |
} | |
Serial.println("Timed out."); | |
return false; | |
} | |
String httpsPost(String url, String data) { | |
if (client.connect(host, 443)) { | |
client.println("POST " + url + " HTTP/1.1"); | |
client.println("Host: " + (String)host); | |
client.println("User-Agent: M5Stack/1.0"); | |
client.println("Connection: close"); | |
client.println("Content-Type: application/json;"); | |
client.print("Content-Length: "); | |
client.println(data.length()); | |
client.println(); | |
client.println(data); | |
delay(10); | |
String response = client.readString(); | |
client.stop(); | |
int bodypos = response.indexOf("\r\n\r\n") + 4; | |
return response.substring(bodypos); | |
} | |
else { | |
return "ERROR"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for Azure Functions | |
#r "Newtonsoft.Json" | |
using System.Net; | |
using Newtonsoft.Json; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
dynamic str = await req.Content.ReadAsStringAsync(); | |
var obj = JsonConvert.DeserializeObject<Data>(str); | |
string name = obj?.name; | |
return name == null | |
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body") | |
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name); | |
} | |
public class Data | |
{ | |
public string name { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment