这里是认知学习的第二堂课,主要内容是使用不同的传感器进行一些数据的采集,有关于arduino IDE的安装请参照第一堂课的内容–ARDUINO 入门教程(一)简介.。感谢博主 @qq_2414466154.。
温湿度(HTU21DF)
土壤湿度(Capacitive)
数字压强(GY-68 BMP180)
光传感器
RFID(rc522)
const int AOUT = A0; //定义一个信号输入接口,将传感器上的AOUT接到A0
int inputValue = 0;
void setup() {
pinMode(AOUT, INPUT);
digitalWrite(AOUT,LOW);
Serial.begin(9600);
}
void loop() {
inputValue = analogRead(AOUT);
Serial.println(inputValue);
delay(500);
}
#include
#include
#include
/*#include
#include "Adafruit_HTU21DF.h"
#include //头文件
Adafruit_BMP085 bmp;*/
#define SS_PIN 4
#define RST_PIN 9 //定义引脚
/*MAX44009 light;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();*/
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
/*const int analogInPin = A0;*/
long now;
byte nuidPICC[4];
int flag=1;
String num="";
int sensorValue = 0;
void setup() {
Serial.begin(115200);
/*Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}*/
/*gy-40*/
Wire.begin();
delay(500);
/* if(light.begin())
{
Serial.println("Could not find a valid MAX44009 sensor, check wiring!");
while(1);
}*/
/*BMP085*/
/* if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}*/
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
Serial.print("1");
}
}
void loop() {
/*if(millis()-now>2000){
flag++;
now=millis();
switch(flag){
case 1:
num="{\"params\":{\"Temp\":"; //温度
num+=htu.readTemperature();
num+="}}";
Serial.println(num);
num="";
num="{\"params\":{\"Humidity\":"; //湿度
num+=htu.readHumidity();
num+="}}";
Serial.println(num);
num="";
break;
case 2:
num="{\"params\":{\"Light\":"; //光强
num+=light.get_lux();
num+="}}";
Serial.println(num);
num="";
break;
case 3:
num="{\"params\":{\"Pressure\":"; //气压
num+=bmp.readPressure();
num+="}}";
Serial.println(num);
num="";
break;
case 4:
sensorValue = analogRead(analogInPin); //土壤湿度
num="{\"params\":{\"Soil\":";
num+=sensorValue;
num+="}}";
Serial.println(num);
num="";
break;
default: flag=0;break;
}
}*/
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printHex(byte *buffer, byte bufferSize) {
num="{\"params\":{\"Rfid\":\""; //rfid
for (byte i = 0; i < bufferSize; i++) {
num+=(String)buffer[i];
//Serial.print(buffer[i], HEX);
}
num+="\"}}";
Serial.print(num);
num="";
}
/*#include
#include */
#include
/*#include */
#include "Adafruit_HTU21DF.h"
#include //头文件
Adafruit_BMP085 bmp;
/*#define SS_PIN 4
#define RST_PIN 9 //定义引脚
MAX44009 light;*/
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
/*MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
const int analogInPin = A0;*/
long now;
byte nuidPICC[4];
int flag=1;
String num="";
int sensorValue = 0;
void setup() {
Serial.begin(115200);
Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}
/*gy-40*/
Wire.begin();
delay(500);
/* if(light.begin())
{
Serial.println("Could not find a valid MAX44009 sensor, check wiring!");
while(1);
}*/
/*BMP085*/
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
/* SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
Serial.print("1");
}*/
}
void loop() {
if(millis()-now>2000){
flag++;
now=millis();
switch(flag){
case 1:
num="{\"params\":{\"Temp\":"; //温度
num+=htu.readTemperature();
num+="}}";
Serial.println(num);
num="";
num="{\"params\":{\"Humidity\":"; //湿度
num+=htu.readHumidity();
num+="}}";
Serial.println(num);
num="";
break;
/* case 2:
num="{\"params\":{\"Light\":"; //光强
num+=light.get_lux();
num+="}}";
Serial.println(num);
num="";
break;*/
case 2:
num="{\"params\":{\"Pressure\":"; //气压
num+=bmp.readPressure();
num+="}}";
Serial.println(num);
num="";
break;
/* case 4:
sensorValue = analogRead(analogInPin); //土壤湿度
num="{\"params\":{\"Soil\":";
num+=sensorValue;
num+="}}";
Serial.println(num);
num="";
break;*/
default: flag=0;break;
}
}
/* if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printHex(byte *buffer, byte bufferSize) {
num="{\"params\":{\"Rfid\":\""; //rfid
for (byte i = 0; i < bufferSize; i++) {
num+=(String)buffer[i];
//Serial.print(buffer[i], HEX);
}
num+="\"}}";
Serial.print(num);
num=""*/;
}
#include
#include
#include
/*#include */
#include "Adafruit_HTU21DF.h"
/*#include //头文件
Adafruit_BMP085 bmp;*/
#define SS_PIN 4
#define RST_PIN 9 //定义引脚
/*MAX44009 light;*/
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
/*const int analogInPin = A0;*/
long now;
byte nuidPICC[4];
int flag=0;
String num="";
int sensorValue = 0;
void setup() {
Serial.begin(115200);
/*Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}*/
/*gy-40
Wire.begin();
delay(500);
if(light.begin())
{
Serial.println("Could not find a valid MAX44009 sensor, check wiring!");
while(1);
} */
/*BMP085*/
/* if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}*/
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
Serial.print("1");
}
}
void loop() {
if(millis()-now>2000){
flag++;
now=millis();
/* switch(flag){
case 1:*/
num="{\"params\":{\"Temp\":"; //温度
num+=htu.readTemperature();
num+="}}";
Serial.println(num);
num="";
num="{\"params\":{\"Humidity\":"; //湿度
num+=htu.readHumidity();
num+="}}";
Serial.println(num);
num="";
/* break;
case 2:
num="{\"params\":{\"Light\":"; //光强
num+=light.get_lux();
num+="}}";
Serial.println(num);
num="";
break;
case 3:
num="{\"params\":{\"Pressure\":"; //气压
num+=bmp.readPressure();
num+="}}";
Serial.println(num);
num="";
break;
case 4:
sensorValue = analogRead(analogInPin); //土壤湿度
num="{\"params\":{\"Soil\":";
num+=sensorValue;
num+="}}";
Serial.println(num);
num="";
break;
default: break;
}*/
flag=0;
}
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printHex(byte *buffer, byte bufferSize) {
num="{\"params\":{\"Rfid\":\""; //rfid
for (byte i = 0; i < bufferSize; i++) {
num+=(String)buffer[i];
Serial.print(buffer[i], HEX);
}
num+="\"}}";
Serial.print(num);
num="";
}
注意:所有传感器的vcc均使用3.3v!
rc522 | arduino |
---|---|
sck | 52 |
sda | 4 |
mosi | 51 |
miso | 50 |
rst | 9 |
各种传感器 | 扩展版 |
---|---|
VCC | 3.3v |
GND | GND |
AOUT | A0 |
SDA | SDA |
SCA | SCA |
接线时注意接口插实,以免接触不良;
接线时注意尽量避免杜邦线的交错缠绕,为出错修后改做准备;
打开arduino IDE编译环境
点击 工具 下 管理库
在搜索框输入传感器型号,在这里直接输入头文件名称即可;
选择版本点击 安装
#include
#include
#include
#include
#include "Adafruit_HTU21DF.h"
#include //头文件
Adafruit_BMP085 bmp;
#define SS_PIN 4
#define RST_PIN 9 //定义引脚
MAX44009 light;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
const int analogInPin = A0;
long now;
byte nuidPICC[4];
int flag=1;
String num="";
int sensorValue = 0;
void setup() {
Serial.begin(115200);
Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}
/*gy-40*/
Wire.begin();
delay(500);
if(light.begin())
{
Serial.println("Could not find a valid MAX44009 sensor, check wiring!");
while(1);
}
/*BMP085*/
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
Serial.print("1");
}
}
void loop() {
if(millis()-now>2000){
flag++;
now=millis();
switch(flag){
case 1:
num="{\"params\":{\"Temp\":"; //温度
num+=htu.readTemperature();
num+="}}";
Serial.println(num);
num="";
num="{\"params\":{\"Humidity\":"; //湿度
num+=htu.readHumidity();
num+="}}";
Serial.println(num);
num="";
break;
case 2:
num="{\"params\":{\"Light\":"; //光强
num+=light.get_lux();
num+="}}";
Serial.println(num);
num="";
break;
case 3:
num="{\"params\":{\"Pressure\":"; //气压
num+=bmp.readPressure();
num+="}}";
Serial.println(num);
num="";
break;
case 4:
sensorValue = analogRead(analogInPin); //土壤湿度
num="{\"params\":{\"Soil\":";
num+=sensorValue;
num+="}}";
Serial.println(num);
num="";
break;
default: flag=0;break;
}
}
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
printHex(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printHex(byte *buffer, byte bufferSize) {
num="{\"params\":{\"Rfid\":\""; //rfid
for (byte i = 0; i < bufferSize; i++) {
num+=(String)buffer[i];
//Serial.print(buffer[i], HEX);
}
num+="\"}}";
Serial.print(num);
num="";
}
波特率设置为115200