ArduinoIDEからサンプルプログラムを開いてenrollを書き込んでみます。これだけで指紋の登録ができる…と色々なサイトに書いてありますが、僕の場合は上手くいきませんでした…。プログラムを走らせてシリアルモニタを開いても「sensor not found.」と表示され、そもそも何も通信できていないようです。
// Adafruitのサンプルプログラムfingerprintをハードウェアシリアルに改変
// Serial.printlnは全部コメントアウトしています。
#include <Adafruit_Fingerprint.h>
const int led_pin = 16; // LEDはD0に接続
// On Leonardo/Micro or others with hardware serial, use those! #0 is green wire, #1 is white
// uncomment this line:
// #define mySerial Serial1
// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
// comment these two lines if using hardware serial
//SoftwareSerial mySerial(2, 3); // ハードウェアシリアルを使うのでコメントアウト
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial); // ハードウェアシリアル
void setup()
{
//Serial.begin(9600);
//while (!Serial); // For Yun/Leo/Micro/Zero/...
//delay(100);
//Serial.println("\n\nAdafruit finger detect test");
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
//Serial.println("Found fingerprint sensor!");
} else {
//Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
finger.getTemplateCount();
//Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
//Serial.println("Waiting for valid finger...");
pinMode(led_pin, OUTPUT);
}
void loop() // run over and over again
{
int num = getFingerprintIDez();
// 認証成功したらLEDを光らせる
if( num > 0 ){
digitalWrite(led_pin, HIGH);
delay(1000);
digitalWrite(led_pin, LOW);
}
delay(50); //don't ned to run this at full speed.
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
//Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
//Serial.println("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
//Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
//Serial.println("Imaging error");
return p;
default:
//Serial.println("Unknown error");
return p;
}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
//Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
//Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
//Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
//Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
//Serial.println("Could not find fingerprint features");
return p;
default:
//Serial.println("Unknown error");
return p;
}
// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
//Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
//Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
//Serial.println("Did not find a match");
return p;
} else {
//Serial.println("Unknown error");
return p;
}
// found a match!
//Serial.print("Found ID #"); Serial.print(finger.fingerID);
//Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
//Serial.print("Found ID #"); Serial.print(finger.fingerID);
//Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
#include "arduinoFFT.h"
#define SAMPLES 256 //Must be a power of 2
#define SAMPLING_FREQUENCY 8000 //Hz, must be less than 10000 due to ADC
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
void setup() {
Serial.begin(115200);
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
}
void loop() {
/*SAMPLING*/
for(int i=0; i<SAMPLES; i++)
{
microseconds = micros(); //Overflows after around 70 minutes!
vReal[i] = analogRead(A0); //ESP8266の場合は「A0」。普通のArduinoは「0」。
vImag[i] = 0;
while(micros() < (microseconds + sampling_period_us)){
}
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
/*PRINT RESULTS*/
Serial.println(peak); //Print out what frequency is the most dominant.
for(int i=2; i<(SAMPLES/2); i++)
{
/*View all these three lines in serial terminal to see which frequencies has which amplitudes*/
//Serial.print((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
//Serial.print(" ");
//Serial.println(vReal[i], 1); //View only this line in serial plotter to visualize the bins
}
delay(1); //Repeat the process every second OR:
//while(1); //ESPだと無限ループがエラーになるので注意
}
/* IRremoteESP8266: IRsendDemo - demonstrates sending IR codes with IRsend.
*
* Version 1.1 January, 2019
* Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009,
* Copyright 2009 Ken Shirriff, http://arcfn.com
*
* An IR LED circuit *MUST* be connected to the ESP8266 on a pin
* as specified by kIrLed below.
*
* TL;DR: The IR LED needs to be driven by a transistor for a good result.
*
* Suggested circuit:
* https://github.com/markszabo/IRremoteESP8266/wiki#ir-sending
*
* Common mistakes & tips:
* * Don't just connect the IR LED directly to the pin, it won't
* have enough current to drive the IR LED effectively.
* * Make sure you have the IR LED polarity correct.
* See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity
* * Typical digital camera/phones can be used to see if the IR LED is flashed.
* Replace the IR LED with a normal LED if you don't have a digital camera
* when debugging.
* * Avoid using the following pins unless you really know what you are doing:
* * Pin 0/D3: Can interfere with the boot/program mode & support circuits.
* * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere.
* * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere.
* * ESP-01 modules are tricky. We suggest you use a module with more GPIOs
* for your first time. e.g. ESP-12 etc.
*/
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <ir_Toshiba.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#define WIFI_SSID "Buffalo-G-4310"
#define WIFI_PWD "buf50317"
#define DNS_NAME "ir-sender"
#define HTML_HEADER "<!doctype html>"\
"<html><head><meta charset=\"UTF-8\"/>"\
"<meta name=\"viewport\" content=\"width=device-width\"/>"\
"</head><body>"
#define HTML_FOOTER "</body></html>"
ESP8266WebServer server(80);
const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2).
IRToshibaAC ac(kIrLed); // Set the GPIO to be used for sending messages.
void printState() {
// Display the settings.
Serial.println("Toshiba A/C remote is in the following state:");
Serial.printf(" %s\n", ac.toString().c_str());
// Display the encoded IR sequence.
unsigned char* ir_code = ac.getRaw();
Serial.print("IR Code: 0x");
for (uint8_t i = 0; i < kToshibaACStateLength; i++)
Serial.printf("%02X", ir_code[i]);
Serial.println();
}
void handle_root(){
String str = HTML_HEADER
"<h1>I'm IR sender!</h1>"
"<h2>Usage</h2>"
"<h3>/</h3>"
"このページです。<br>"
"<h3>/set?temp=22&fan=0&mode=a</h3>"
"デフォルトのパラメータを設定します。<br>"
"<h3>/state</h3>"
"デフォルトのパラメータを表示します。<br>"
"<h3>/on?temp=22&fan=0&mode=a または /on</h3>"
"エアコンONを送信します。クエリをつけるとそれをデフォルトのパラメータに設定して起動します。<br>"
"クエリをつけないと以前に設定したパラメータを使います。<br>"
"<h3>/off</h3>"
"エアコンOFFを送信します。<br>"
HTML_FOOTER;
server.send(200, "text/html", str);
}
void handle_set(){
for (int i = 0; i < server.args(); i++) {
if(server.argName(i) == "temp"){
ac.setTemp(server.arg(i).toInt());
}else if(server.argName(i) == "fan"){
ac.setFan(server.arg(i).toInt());
}else if(server.argName(i) == "mode"){
if(server.arg(i) == "a") ac.setMode(kToshibaAcAuto);
if(server.arg(i) == "h") ac.setMode(kToshibaAcHeat);
if(server.arg(i) == "c") ac.setMode(kToshibaAcCool);
if(server.arg(i) == "d") ac.setMode(kToshibaAcDry);
}
}
}
void handle_on(){
if(server.args() > 0)
handle_set();
ac.on();
ac.send();
String str = HTML_HEADER + ac.toString() + HTML_FOOTER;
server.send(200, "text/html", str);
}
void handle_off(){
ac.off();
ac.send();
String str = HTML_HEADER + ac.toString() + HTML_FOOTER;
server.send(200, "text/html", str);
}
void setup() {
ac.begin();
Serial.begin(115200);
delay(200);
WiFi.begin(WIFI_SSID, WIFI_PWD);
// Wait until WiFi is connected
Serial.println("");
while(WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin(DNS_NAME)) {
Serial.println("MDNS responder started");
}
// Setup WebServer Handlers
server.on("/", handle_root);
server.on("/on", handle_on);
server.on("/off", handle_off);
server.on("/set", handle_set);
server.on("/state", [](){
String str = HTML_HEADER + ac.toString() + HTML_FOOTER;
server.send(200, "text/html", str);
});
server.begin();
}
void loop() {
server.handleClient();
MDNS.update();
}
// ESP32 Touch Test
// Just test touch pin - Touch0 is T0 which is on GPIO 4.
void setup()
{
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Test");
}
void loop()
{
Serial.println(touchRead(T0)); // get value using T0
delay(1000);
}