M5Stick-C(ESP32機器)で取得した温度をメール送信するプログラムをArduinoで作っています。
温度を測り、ESP32 Mail Clientライブラリのサンプルを使ってOutlookにメールを送ることはできました。
が、いざ実行環境に入れると『Error sending Email, could not connect to server』とシリアルモニターに表示されて送信できません。
プログラムを改変して調べたところ、どうやらwifi.configで固定IPアドレスを使うとエラーになるようです。
これは何故でしょうか。どうすれば、wifi.configを使ったうえでメールの送信ができるようになるでしょうか?
下にプログラムを載せておきます。(wifi接続部とメール送信部分だけです)
c++
#include <WiFi.h> #include <WiFiClient.h> #include <WebServer.h> #include <ESPmDNS.h> #include <M5StickC.h> #include <SPI.h> #include <Ethernet.h> #include <ArduinoJson.h> #include <HTTPClient.h> #include <WiFiMulti.h> #include <Arduino.h> #include "ESP32_MailClient.h" #include "SD.h" //The Email Sending data object contains config and data to send SMTPData smtpData; //Callback function to get the Email sending status void sendCallback(SendStatus info); void setup() { Serial.begin(115200); Serial.println(); Serial.print("Connecting to AP"); wifi_connect(); Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP().toString().c_str()); Serial.println(); Serial.println("Sending email..."); smtpData.setLogin("outlook.office365.com", 587, "outlookのアカウント名", "outlookのパスワード"); smtpData.setSender("ESP32", "outlookのメールアドレス"); smtpData.setPriority("Normal"); smtpData.setSubject("ESP32 SMTP Mail Sending Test"); smtpData.setMessage("M5StickErrorの温度計からエラーが返りました。", false); smtpData.addRecipient("受信メールアドレス"); smtpData.addAttachFile("/binary_file.dat"); smtpData.addAttachFile("/text_file.txt"); smtpData.setSendCallback(sendCallback); //Start sending Email, can be set callback function to track the status if (!MailClient.sendMail(smtpData)){ Serial.println("Error sending Email, " + MailClient.smtpErrorReason()); } //Clear all data from Email object to free memory smtpData.empty(); } void wifi_connect() { WiFi.config("ipAddress", "gateway", "subnetmask", "DNS"); // Set fixed IP address WiFi.mode(WIFI_STA); WiFi.begin("WIFI_SSID", "WIFI_PASSWORD"); // 接続完了するまでループ while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); } Serial.println("WifiConnect!!"); } void loop() { } //Callback function to get the Email sending status void sendCallback(SendStatus msg) { //Print the current status Serial.println(msg.info()); //Do something when complete if (msg.success()) { Serial.println("----------------"); } }
お力添えの方、よろしくお願いいたします。
> 固定IPアドレスを使うとエラーになる
「固定」が問題なのか、それとも与えたIPアドレスがネットワーク上正しくないのかを切り分ける必要があると思います。
自動で割り振られたものと同じIPアドレスを固定で指定したときにはどうなりますか?
>自動で割り振られたものと同じIPアドレスを固定で指定したときにはどうなりますか?
その場合は送信できることもあります。が、できないことも間々あり、不明瞭です。
・接続出来なかった固定IPアドレスは、ローカルネットワークのネットマスクに含まれているものですか?
・DHCPサーバをいじれるなら、IPアドレスの発給範囲を制限してDHCPで割当の行われないIPアドレスを確保し、それを固定IPとしてESP32に与えたらどうでしょうか。
まだ回答がついていません
会員登録して回答してみよう