UNO+ ESP+Blinker,串口通信-Arduino中文社区 - Powered by Discuz! Archiver

努力编程小阿格 发表于 2022-5-5 09:30

UNO+ ESP+Blinker,串口通信

我想将uno采集数据通过硬串口上传到esp,esp再将数据上传至点灯科技APP。有大佬能帮我看看怎么改吗?我想把火焰和人体信号改为串口通信,不知道咋改?//RC522
#include <SPI.h>
#include <String.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);
byte nuidPICC;

//OLED显示屏
#include <DFRobot_SSD1306_I2C.h>
#define OLED_RESET 13


// 静态常量
const chCode chBuf[] = {
{0x667a, 0xe699ba, 0xD6C7, {0x20, 0x00, 0x3e, 0x7c, 0x48, 0x44, 0x08, 0x44, 0xff, 0x44, 0x14, 0x44, 0x22, 0x7c, 0x40, 0x00, 0x1f, 0xf0, 0x10, 0x10, 0x10, 0x10, 0x1f, 0xf0, 0x10, 0x10, 0x10, 0x10, 0x1f, 0xf0, 0x10, 0x10}},
{0x6167, 0xe685a7, 0xBBDB, {0x10, 0x10, 0xfe, 0xfe, 0x10, 0x10, 0x7c, 0x7c, 0x10, 0x10, 0xfe, 0xfe, 0x10, 0x10, 0x3f, 0xf8, 0x00, 0x08, 0x1f, 0xf8, 0x00, 0x08, 0x3f, 0xf8, 0x01, 0x00, 0x48, 0x84, 0x48, 0x12, 0x87, 0xf2}},
{0x5c0f, 0xe5b08f, 0xD0A1, {0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x11, 0x10, 0x11, 0x08, 0x11, 0x04, 0x21, 0x04, 0x21, 0x02, 0x41, 0x02, 0x81, 0x02, 0x01, 0x00, 0x01, 0x00, 0x05, 0x00, 0x02, 0x00}},
{0x533a, 0xe58cba, 0xC7F8, {0x00, 0x00, 0x7f, 0xfc, 0x40, 0x00, 0x40, 0x10, 0x44, 0x10, 0x42, 0x20, 0x41, 0x40, 0x40, 0x80, 0x41, 0x40, 0x42, 0x20, 0x44, 0x10, 0x48, 0x10, 0x40, 0x00, 0x40, 0x00, 0x7f, 0xfe, 0x00, 0x00}},
{0x5b89, 0xe5ae89, 0xB0B2, {0x02, 0x00, 0x01, 0x00, 0x3f, 0xfc, 0x20, 0x04, 0x42, 0x08, 0x02, 0x00, 0x02, 0x00, 0xff, 0xfe, 0x04, 0x20, 0x08, 0x20, 0x18, 0x40, 0x06, 0x40, 0x01, 0x80, 0x02, 0x60, 0x0c, 0x10, 0x70, 0x08}},
{0x9632, 0xe998b2, 0xB7C0, {0x00, 0x40, 0x7c, 0x20, 0x44, 0x20, 0x4b, 0xfe, 0x48, 0x80, 0x50, 0x80, 0x48, 0x80, 0x48, 0xfc, 0x44, 0x84, 0x44, 0x84, 0x44, 0x84, 0x68, 0x84, 0x51, 0x04, 0x41, 0x04, 0x42, 0x28, 0x44, 0x10}},
{0x7cfb, 0xe7b3bb, 0xCFB5, {0x00, 0xf8, 0x3f, 0x00, 0x04, 0x00, 0x08, 0x20, 0x10, 0x40, 0x3f, 0x80, 0x01, 0x00, 0x06, 0x10, 0x18, 0x08, 0x7f, 0xfc, 0x01, 0x04, 0x09, 0x20, 0x11, 0x10, 0x21, 0x08, 0x45, 0x04, 0x02, 0x00}},
{0x7edf, 0xe7bb9f, 0xCDB3, {0x10, 0x40, 0x10, 0x20, 0x20, 0x20, 0x23, 0xfe, 0x48, 0x40, 0xf8, 0x88, 0x11, 0x04, 0x23, 0xfe, 0x40, 0x92, 0xf8, 0x90, 0x40, 0x90, 0x00, 0x90, 0x19, 0x12, 0xe1, 0x12, 0x42, 0x0e, 0x04, 0x00}},
{0x53ef, 0xe58faf, 0xBFC9, {0x00, 0x00, 0x7f, 0xfe, 0x00, 0x10, 0x00, 0x10, 0x1f, 0x10, 0x11, 0x10, 0x11, 0x10, 0x11, 0x10, 0x11, 0x10, 0x11, 0x10, 0x1f, 0x10, 0x11, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x50, 0x00, 0x20}},
{0x71c3, 0xe78783, 0xC8BC, {0x11, 0x10, 0x11, 0x14, 0x11, 0xd2, 0x16, 0x50, 0x5a, 0x7e, 0x55, 0x50, 0x50, 0x90, 0x92, 0xa8, 0x11, 0x28, 0x11, 0x44, 0x12, 0x44, 0x2c, 0x82, 0x24, 0x08, 0x42, 0xa4, 0x42, 0x52, 0x84, 0x52}},
{0x6c14, 0xe6b094, 0xC6F8, {0x10, 0x00, 0x10, 0x00, 0x3f, 0xfc, 0x20, 0x00, 0x4f, 0xf0, 0x80, 0x00, 0x3f, 0xf0, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x0a, 0x00, 0x0a, 0x00, 0x06, 0x00, 0x02}},
{0x4f53, 0xe4bd93, 0xCCE5, {0x08, 0x40, 0x08, 0x40, 0x08, 0x40, 0x10, 0x40, 0x17, 0xfc, 0x30, 0x40, 0x30, 0xe0, 0x50, 0xe0, 0x91, 0x50, 0x11, 0x50, 0x12, 0x48, 0x15, 0xf4, 0x18, 0x42, 0x10, 0x40, 0x10, 0x40, 0x10, 0x40}},
{0x6d53, 0xe6b593, 0xC5A8, {0x00, 0x40, 0x20, 0x40, 0x10, 0x40, 0x17, 0xfc, 0x84, 0x84, 0x48, 0x88, 0x41, 0x40, 0x11, 0x44, 0x13, 0x48, 0x25, 0x30, 0xe9, 0x20, 0x21, 0x10, 0x21, 0x08, 0x21, 0x44, 0x21, 0x82, 0x01, 0x00}},
{0x5ea6, 0xe5baa6, 0xB6C8, {0x01, 0x00, 0x00, 0x80, 0x3f, 0xfe, 0x22, 0x20, 0x22, 0x20, 0x3f, 0xfc, 0x22, 0x20, 0x22, 0x20, 0x23, 0xe0, 0x20, 0x00, 0x2f, 0xf0, 0x24, 0x10, 0x42, 0x20, 0x41, 0xc0, 0x86, 0x30, 0x38, 0x0e}},
{0xff1a, 0xefbc9a, 0xA3BA, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00}},
{0x706b, 0xe781ab, 0xBBF0, {0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x11, 0x08, 0x11, 0x08, 0x11, 0x10, 0x21, 0x20, 0x21, 0x00, 0x42, 0x80, 0x02, 0x80, 0x04, 0x40, 0x04, 0x40, 0x08, 0x20, 0x10, 0x10, 0x20, 0x08, 0xc0, 0x06}},
{0x7130, 0xe784b0, 0xD1E6, {0x10, 0x40, 0x10, 0x40, 0x10, 0x7c, 0x14, 0x84, 0x59, 0x08, 0x52, 0x00, 0x50, 0x20, 0x91, 0xce, 0x11, 0x02, 0x11, 0x02, 0x11, 0xce, 0x29, 0x02, 0x25, 0x02, 0x45, 0x02, 0x41, 0xfe, 0x81, 0x02}},
{0x53c2, 0xe58f82, 0xB2CE, {0x02, 0x00, 0x04, 0x40, 0x08, 0x20, 0x1f, 0xf0, 0x02, 0x00, 0x7f, 0xfc, 0x08, 0x20, 0x10, 0x90, 0x23, 0x08, 0xcc, 0x46, 0x01, 0x80, 0x06, 0x10, 0x18, 0x20, 0x00, 0xc0, 0x07, 0x00, 0x78, 0x00}},
{0x6570, 0xe695b0, 0xCAFD, {0x08, 0x20, 0x49, 0x20, 0x2a, 0x20, 0x08, 0x3e, 0xff, 0x44, 0x2a, 0x44, 0x49, 0x44, 0x88, 0xa4, 0x10, 0x28, 0xfe, 0x28, 0x22, 0x10, 0x42, 0x10, 0x64, 0x28, 0x18, 0x28, 0x34, 0x44, 0xc2, 0x82}},
{0x6709, 0xe69c89, 0xD3D0, {0x02, 0x00, 0x02, 0x00, 0xff, 0xfe, 0x04, 0x00, 0x04, 0x00, 0x0f, 0xf0, 0x08, 0x10, 0x18, 0x10, 0x2f, 0xf0, 0x48, 0x10, 0x88, 0x10, 0x0f, 0xf0, 0x08, 0x10, 0x08, 0x10, 0x08, 0x50, 0x08, 0x20}},
{0x4eba, 0xe4baba, 0xC8CB, {0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x80, 0x02, 0x80, 0x04, 0x40, 0x04, 0x40, 0x08, 0x20, 0x08, 0x20, 0x10, 0x10, 0x20, 0x08, 0x40, 0x04, 0x80, 0x02}},
{0x5165, 0xe585a5, 0xC8EB, {0x04, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x80, 0x02, 0x80, 0x02, 0x80, 0x04, 0x40, 0x04, 0x40, 0x08, 0x20, 0x08, 0x20, 0x10, 0x10, 0x20, 0x10, 0x40, 0x08, 0x80, 0x06}},
{0x4fb5, 0xe4beb5, 0xC7D6, {0x08, 0x00, 0x0b, 0xf8, 0x08, 0x08, 0x11, 0xf8, 0x10, 0x08, 0x33, 0xf8, 0x30, 0x00, 0x57, 0xfc, 0x94, 0x04, 0x13, 0xf0, 0x11, 0x10, 0x11, 0x10, 0x10, 0xa0, 0x10, 0x40, 0x11, 0xb0, 0x16, 0x0e}}
};
// 创建对象
DFRobot_SSD1306_I2C oled12864;
//设置烟雾上下限初始值
int smoke_max = 30;
int smoke = 0;
//设置硬件引脚
#definerenti8//红外
#defineflame7//火焰
#define buzzer6//蜂鸣器
#define relay 5//继电器
#define ESP8266_OUT14//蜂鸣器
#define ESP8266_OUT2 3//门禁
#define ESP8266_IN2//蜂鸣器
#define ESP8266_OUT3A0//蜂鸣器
#define MQ A1 //烟雾传感器
//声明变量
String comdata = "";
int a_1 = 0, a_2 = 0, a_3 = 0, a_4 = 0;
int b_1 = 0, b_2 = 0, b_3 = 0, b_4 = 0;
int sss = 0;


//初始化
void setup()
{
//定义字符
oled12864.setChCode(chBuf);
oled12864.begin(0x3c);
oled12864.setCursor(0, 0);
oled12864.print("智慧小区安防系统");
oled12864.setCursor(0, 1);
oled12864.print("可燃气体:");
oled12864.setCursor(0, 2);
oled12864.print("火焰参数:");
//串口
Serial.begin(9600);
//SPI
SPI.begin();
rfid.PCD_Init();
//设置引脚
pinMode(renti, INPUT_PULLUP);
pinMode(flame, INPUT_PULLUP);
pinMode(ESP8266_OUT1, INPUT_PULLUP);
pinMode(ESP8266_OUT2, INPUT_PULLUP);
pinMode(ESP8266_OUT3, OUTPUT);
digitalWrite(ESP8266_OUT3, 1);
pinMode(ESP8266_IN, OUTPUT);
digitalWrite(ESP8266_IN, 1);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, 1);
pinMode(relay, OUTPUT);
digitalWrite(relay, 1);
}

//烟雾传感器
void MQ_2() {

smoke = 100 - map(analogRead(MQ), 1023, 0, 0, 100);

Serial.print("S");
Serial.print(smoke);
Serial.print(',');

if (smoke < 10) {
    oled12864.setCursor(70, 1);
    oled12864.print(smoke);
    oled12864.setCursor(79, 1);
    oled12864.print("    ");
}
if (smoke >= 10) {
    oled12864.setCursor(70, 1);
    oled12864.print(smoke);
    oled12864.setCursor(87, 1);
    oled12864.print("    ");
}
if (smoke == 100) {
    oled12864.setCursor(70, 1);
    oled12864.print(smoke);
}


if (smoke > smoke_max) {
    b_1 = 1;
    //这里要蜂鸣器
}
}

//hex
void printHex(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++)
{
    //Serial.print(buffer < 0x10 ? " 0" : "");
    //Serial.print(buffer, HEX);
}
}
//==================开门用================//
void printDec(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++) {
    //Serial.print(buffer < 0x10 ? " 0" : "");
    //Serial.print(buffer, DEC);
}
//人员1
if (nuidPICC == 0x53 && nuidPICC == 0x25 && nuidPICC == 0x80 && nuidPICC == 0x94) // 卡号ID
{

    a_1 = 1;
    //这里要开门
}
//人员2
else if (nuidPICC == 0xA3 && nuidPICC == 0x02 && nuidPICC == 0x27 && nuidPICC == 0x98)
{

    a_1 = 1;
    //这里要开门
}
//人员3
else if (nuidPICC == 0xFA && nuidPICC == 0x9D && nuidPICC == 0x61 && nuidPICC == 0xBF)
{

    a_1 = 1;
    //这里要开门
}
}
//注册+识别
void zhuce() {
if ( ! rfid.PICC_IsNewCardPresent())
    return;
if ( ! rfid.PICC_ReadCardSerial())
    return;
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); // 将NUID保存到nuidPICC数组
for (byte i = 0; i < 4; i++)
{
    nuidPICC = rfid.uid.uidByte;
}

printHex(rfid.uid.uidByte, rfid.uid.size); //Hex:16进制

printDec(rfid.uid.uidByte, rfid.uid.size); //Dec:10进制


// 使放置在读卡区的IC卡进入休眠状态,不再重复读卡,Halt(停止)
rfid.PICC_HaltA();
// 停止读卡模块编码
rfid.PCD_StopCrypto1();
}
void zhixing() {
if ((a_1 + a_2) >= 1) {
    digitalWrite(relay, 0);
    delay(3000);
    digitalWrite(relay, 1);
    a_1 = a_2 = 0;
}
else {
    digitalWrite(relay, 1);
}
if ((b_1 + b_2 + b_3) >= 1 and sss == 0) {
    digitalWrite(buzzer, 0);
}
else {
    digitalWrite(buzzer, 1);
}
}
void huoyan() {
if (digitalRead(flame) == 0) {
    delay(10);
    if (digitalRead(flame) == 0) {
      b_2 = 1;
      oled12864.setCursor(70, 2);
      oled12864.print("NO");

      //蜂鸣器
      digitalWrite(ESP8266_IN, 0);
    }
}
else {
    b_2 = 0;

    digitalWrite(ESP8266_IN, 1);
    oled12864.setCursor(70, 2);
    oled12864.print("OK");
    //蜂鸣器
}
}
void hongwai() {
if (digitalRead(renti) == 0) {
    delay(10);
    if (digitalRead(renti) == 0) {
      b_3 = 1;
      digitalWrite(ESP8266_OUT3, 0);
      oled12864.setCursor(42, 3);
      oled12864.print("有人入侵");

    }
}
else {
    b_3 = 0;

    digitalWrite(ESP8266_OUT3, 1);
    oled12864.setCursor(42, 3);
    oled12864.print("         ");
}
}
void ESP8266() {
if (digitalRead(ESP8266_OUT1) == 0) {
    delay(10);
    if (digitalRead(ESP8266_OUT1) == 0) {
      a_2 = 1;

    }
}
else {
    a_2 = 0;

}
if (digitalRead(ESP8266_OUT2) == 0) {
    delay(10);
    if (digitalRead(ESP8266_OUT2) == 0) {
      sss = 1;

    }
}
else {
    sss = 0;

}
}
void loop() {
zhuce();
zhixing();
MQ_2();
zhixing();
huoyan();
hongwai();
ESP8266();
zhixing();
}



页: [1]
查看完整版本: UNO+ ESP+Blinker,串口通信