Smart Home IoT Blynk導入 2 解決編
前回、Blynk導入にあたり、ArduinoのSketchをBlynk Exampleから生成したものの、コンパイルができず。
散々調べた結果できたので、続きから。
前回のエラーは
Arduino: 1.8.12 (Mac OS X), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
sketch_may06a:41:25: fatal error: ESP8266_Lib.h: No such file or directory
compilation terminated.
exit status 1
ESP8266_Lib.h: No such file or directory
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
要は、ライブラリ(BlynkESP8266_Lib.h)が足りないということ。
Manage Librariesのインストールでは入らないのであれば、マニュアルインストール。
まず、Blynk の最新版のzipをダウンロード。
zipを解凍
BlynkESP8266_libがあり、これはManager Librariesではインストールされていない。
ArduinoのlibrariesディレクトリにBlynkESP8266_libのフォルダ自体をコピー。
librariesディレクトリは
To find the location of your sketchbook folder, go to top menu in Arduino IDE:
Windows: File → Preferences
Mac OS: Arduino → Preferences
で確認。
これでインストール完了。無事コンパイルが通った。
しかし。アップロードすると、うまく動かない。
シリアルモニターをみると
11:47:49.537 -> connecting to ESP 1connecting to ESP 2[10]
11:47:49.609 -> ___ __ __
11:47:49.609 -> / _ )/ /_ _____ / /__
11:47:49.644 -> / _ / / // / _ \/ '_/
11:47:49.679 -> /____/_/\_, /_//_/_/\_\
11:47:49.715 -> /___/ v0.6.1 on Arduino Mega
11:47:49.750 ->
11:47:50.182 -> [629] Connecting to aterm-36be98-g
11:47:51.201 -> [1640] ESP is not responding
ESPのレスポンスがない???
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
Megaだけど、Mega2560はSerial1じゃないの?
過去のLession SketchをみるとSoftwareSerial部分は
#include "SoftwareSerial.h"
SoftwareSerial softserial(A9, A8); // RX, TX
なので、softwareSerialを変更。
softserial と EspSerialで文字列が違うので合わせる。
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
WARNING!
It's very tricky to get it working. Please read this article:
http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware
You’ll need:
- Blynk App (download from AppStore or Google Play)
- Arduino Mega 2560 board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include "ESP8266_Lib.h"
#include "BlynkSimpleShieldEsp8266.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "JoG2IGS8QpJHDbIshSYOPbyao_TZL7BB";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "aterm-36be98-g"; // your network SSID (name)
char pass[] = "5017be9e5e30d"; // your network password
// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1
// or Software Serial on Uno, Nano...
//#include "softwareserial.h"
//SoftwareSerial EspSerial(2, 3); // RX, TX
#include "SoftwareSerial.h"
//SoftwareSerial softserial(A9, A8); // A9 to ESP_TX, A8 to ESP_RX by default
//SoftwareSerial EspSerial(3,2); // RX, TX
SoftwareSerial EspSerial(A9, A8); // RX, TX
// Your ESP8266 baud rate:
//#define ESP8266_BAUD 38400
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void loop()
{
Blynk.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
delay(1000); // stop the code for 1 second
}
シリアルモニターで動いた!!!!!!!!!!!!!
14:14:32.814 -> [9]
14:14:32.814 -> ___ __ __
14:14:32.850 -> / _ )/ /_ _____ / /__
14:14:32.884 -> / _ / / // / _ \/ '_/
14:14:32.920 -> /____/_/\_, /_//_/_/\_\
14:14:32.920 -> /___/ v0.6.1 on Arduino Mega
14:14:32.990 ->
14:14:33.419 -> [599] Connecting to aterm-36be98-g
14:14:36.635 -> [3808] AT version:1.2.0.0(Jul 1 2016 20:04:45)
14:14:36.668 -> SDK version:1.5.4.1(39cb9a32)
14:14:36.704 -> Ai-Thinker Technology Co. Ltd.
14:14:36.738 -> Dec 2 2016 14:21:16
14:14:36.775 -> OK
14:14:37.730 -> [4907] Failed to enable MUX
14:14:42.931 -> [10106] +CIFSR:STAIP,"192.168.3.27"
14:14:42.965 -> +CIFSR:STAMAC,"cc:50:e3:e3:d6:02"
14:14:43.000 -> [10115] Connected to WiFi
14:14:53.439 -> [20604] Ready (ping: 59ms).
アプリをみるとオンラインになった。
ONを押すと黄色LEDが点灯
ONを話すと黄色LEDが点灯
お疲れ様でした。
How to Blynk with OSOYOO Smart Home IoT Learning Kit With MEGA2560