2021-08-16
Electronics, M5Stick-C, Wifi
M5Stick-Cをルータに接続する
M5Stick-Cをルータに接続するのに色々試してみたので, まとめます.
Simplest ver.
接続するだけの超シンプル版
wifi_test_simplest.ino
#include "WiFi.h"
const char* ssid = "myssid";
const char* pw = "mypw1234";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pw);
}
void loop() {
Serial.println(WiFi.localIP());
}
Simple with timeout
接続できなかった場合のタイムアウトを追加 タイムアウトは10秒
wifi_test_simple.ino
#include "WiFi.h"
const char* ssid = "myssid";
const char* pw = "mypw1234";
void setup() {
Serial.begin(115200);
Serial.println("WiFi begin");
WiFi.begin(ssid, pw);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
int limitTime = 10000;
if ( limitTime < millis() ) {
Serial.println("break");
break;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println(WiFi.localIP());
}
}
void loop() {
}
Connect and display the IP into LCD on M5Stick-C
M5Stick-C のLCD画面にIPを表示させる
wifi_test_display-IP.ino
#include <M5StickC.h>
#include "WiFi.h"
IPAddress ip;
const char* ssid = "myssid";
const char* pw = "mypw1234";
int count = 0;
void setup() {
Serial.begin(115200);
//M5 setting
M5.begin();
M5.Lcd.fillScreen(BLACK);
M5.Axp.ScreenBreath(8); // [(0,)7,12]
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setRotation(1);
M5.Lcd.println("Connecting...");
// start connecting a specific wifi
Serial.println("Start WiFi setting: ");
WiFi.begin(ssid, pw);
// check the connection
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
// check time out for break;
int limitTime = 10000;
if ( limitTime < millis() ) {
Serial.println("break");
break;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println(WiFi.localIP());
}
}
void loop() {
count++;
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
// display loop count
M5.Lcd.print("Count:");
M5.Lcd.println(String(count));
// check connection and display it
if(WiFi.status() == WL_CONNECTED) {
ip = WiFi.localIP();
M5.Lcd.println(ip);
}
else {
M5.Lcd.println("No connetion");
}
delay(1000);
}
Connect WiFi without designation
codeに直接SSID, PWを書くのは安全ではない, という場合の対応策. 前回にネットワークに接続していた場合は, 無指定のcodeをM5Stick-Cに書き込んでも, そのネットワークに自動で接続してくれる. つまり, 究極的には下記でも接続ができる.
#include "WiFi.h"
void setup() {
WiFi.begin();
}
void loop() {
}
LCD画面にdisplayする場合との組み合わせは下記:
wifi_test_no-designation_display-IP.ino
#include <M5StickC.h>
#include "WiFi.h"
IPAddress ip;
int count = 0;
void setup() {
Serial.begin(115200);
//M5 setting
M5.begin();
M5.Lcd.fillScreen(BLACK);
M5.Axp.ScreenBreath(8); // [(0,)7,12]
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(2);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setRotation(1);
M5.Lcd.println("Connecting...");
// start connecting a specific wifi
Serial.println("Start WiFi setting: ");
WiFi.begin();
// check the connection
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
// check time out for break;
int limitTime = 10000;
if ( limitTime < millis() ) {
Serial.println("break");
break;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println(WiFi.localIP());
}
}
void loop() {
count++;
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
// display loop count
M5.Lcd.print("Count:");
M5.Lcd.println(String(count));
// check connection and display it
if(WiFi.status() == WL_CONNECTED) {
ip = WiFi.localIP();
M5.Lcd.println(ip);
}
else {
M5.Lcd.println("No connetion");
}
delay(1000);
}
References
https://lang-ship.com/blog/work/esp32-wi-fi-setting/#GitHub-2
https://www.arduino.cc/en/Reference/WiFiLocalIP
https://forum.arduino.cc/index.php?topic=228884.0
https://lang-ship.com/blog/work/m5stickc-display/#i-8
https://lang-ship.com/blog/work/m5stickc-axp192-figure/
https://lang-ship.com/reference/unofficial/M5StickC/Class/AXP192/