#include <WiFi.h>
#include <WiFiClientSecure.h>
// WiFi設定
const char* ssid = "ssiddesu";
const char* password = "passworddesu";
//wifi接続
int lpcnt;
int lpcnt2;
//asp.net webサーバ
const char* host = "host.nonamae.jp";
String atesaki="atesaki1@gmail.com,atesaki2@gmail.com";
String daimei="雨の通知";
String naiyou="雨が降り始めました \n2行目";
void setup()
{
// シリアルモニタの通信速度
Serial.begin(115200);
delay(500);
// ssidとpasswordを用いてWi-Fiに接続
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) { // 接続確認
delay(500); // 0.5秒毎にチェック
lpcnt += 1 ; //
if (lpcnt > 6) { // 6回目(3秒) で切断/再接続
WiFi.disconnect(true,true) ; //
WiFi.begin(ssid, password); //
lpcnt = 0 ; //
lpcnt2 += 1 ; // 再接続の回数をカウント
} //
if (lpcnt2 > 3) { // 3回 接続できなければ、
ESP.restart() ; // ソフトウェアリセット
} //
Serial.print("."); //
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
send_post(); //サーバにポスト送信
}
// post
void send_post() {
// HTTPSへアクセス(SSL通信)するためのライブラリ
WiFiClientSecure client;
// サーバー証明書の検証を行わずに接続する場合に必要
client.setInsecure();
Serial.println("Try");
//自宅aspサーバにSSL接続(ポート443:https)
if (!client.connect(host, 443)) {
Serial.println("Connection failed");
return;
}
Serial.println("Connected");
// リクエスト送信
String query = "送付先=" + atesaki + "&題名=" + daimei + "&内容=" + naiyou;
String request = String("") +
"POST /xxxx/xxxx/xxxx.aspx HTTP/1.1\r\n" + //post先を指定する
"Host: " + host + "\r\n" +
"Content-Length: " + String(query.length()) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
query + "\r\n";
client.print(request);
// 一定時間レスポンスがなければタイムアウト.
unsigned long timeout = millis();
while (client.available() == 0)
{
if (millis() - timeout > 5000)
{
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// レスポンスを1行ごとに表示 デバッグ用
while(client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("post完了");
}
void loop(){
}