0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

wemosでenebular

Posted at

概要

wemosでenebularを叩いてみた。
postで受信ポイントにhttpリクエストを送った。

サンプルコード

# include <ESP8266WiFi.h>
# include <WiFiClientSecure.h>

const char * ssid = "takahatalib";
const char * password = "***";

bool Https_Get_access(String host, String url, String argument) {
	BearSSL::WiFiClientSecure client;
	client.setTimeout(500);
	client.setInsecure();
	const int httpPort = 443;
	const char * host2 = host.c_str();
	if (!client.connect(host2, httpPort))
	{
		Serial.println("connection failed");
		return false;
	}
	client.print(String("POST ") + url + " HTTP/1.1\r\n" 
		+ "Host: " + host + "\r\n" 
		+ "User-Agent: ESP8266/1.0\r\n" 
		+ "x-api-key:test-api-key\r\n"
		+ "content-type:application/x-www-form-urlencoded\r\n"
		+ "content-length:32\r\n"
		+ "Connection: close\r\n"
		+ "\r\n"
		+ argument + "\r\n"
		+ "\r\n");
	unsigned long timeout = micros();
	while (client.available() == 0)
	{
		if (micros() - timeout > 5000000)
		{
			Serial.println(">>> Client Timeout !");
			client.stop();
			return false;
		}
	}
	while (client.available())
	{
		String line = client.readStringUntil('\r');
		Serial.print(line);
	}
	return true;
}
void setup() {
	Serial.begin(115200);
	delay(10);
	Serial.print("Connecting to ");
	Serial.println(ssid);
	WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED)
	{
		delay(500);
		Serial.print(".");
	}
	Serial.println("");
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());
}
void loop() {
	String host = "***.herokuapp.com";
	String url = "/sensordata";
	String argument = "roomname=house1&temperature=25.7";
	Https_Get_access(host, url, argument);
	while(1)
	{
		delay(5000);
	}
}






以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?