求助!esp8266+重力传感不能同时工作。

作者:往兮321 | 更新时间:2020-05-12

ESP8266+重力传感HX711芯片 使用论坛的控制LED代码合成后,可以看到设备在线状态,称重也可以正常工作, 但设备对话时就没有反应 。  两个代码分开时都能运行, 合起来就不行了,小白一个,大神能说详细点不。。

源代码 如下:  

 

/*
 * 按钮按下开始指纹检测,成功发送信息,并LED闪烁,lock开启;失败发送信息报警;
 * lock开启后检测开关,按下开关,lock关闭; 
 * 发送open指纹检测开锁;发送new添加指纹;发送stop关闭lock;发送play打开lock;
 * 检测到物品后,发送信息提示;
 * 压力HX711:VCC接5V、SCK接A0,DT接A1、GND接地;模块红黑白绿接E+E-A-A+;
 * wifi8266:VCC接3.3V、GND接地、TXD接0、RXD接1;
 * 指纹AS608:VCC接3.3V 红线、TX接2 黄线、RX接3 白线、GND接地 黑线;
 * lock接11,通电不能大于1秒;LED接9;门锁按钮接10;
*/
#include <Adafruit_Fingerprint.h>
#include <aJSON.h>
#include "HX711.h"
#include <SoftwareSerial.h>

// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
SoftwareSerial mySerial(2, 3);
//SoftwareSerial wifiSerial(0, 1);
HX711 scale(A5, A4);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);//mySerial = finger
uint8_t id;

String DEVICEID = "17388"; // 设备ID
String  APIKEY = "63fb07905"; //APIKEY


const int lock = 11;
const int LED = 9;//提示灯
unsigned long lastCheckStatusTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
unsigned long checkoutTime = 0;//登出时间
int sta;

int button = 7;//门锁按钮

boolean buttonState = 1;

extern unsigned long HX711_Read(void);
extern long Get_Weight();
///变量定义
float Weight = 0;
int HX711_SCK =A4;   ///     作为输出口
int HX711_DT= A5;    ///     作为输入口
long HX711_Buffer = 0;
long Weight_Maopi = 0, Weight_Shiwu = 0;
#define GapValue 240       ///该值需校准 每个传感器都有所不同

void setup()
{  
 //初始化HX711的两个io口   
pinMode(HX711_SCK, OUTPUT);  ///SCK 为输出口 ---输出脉冲
pinMode(HX711_DT, INPUT); ///  DT为输入口  ---读取数据
 Serial.begin(115200);
 Serial.print("Welcome to use!\n");
 delay(3000);    ///延时3秒  
 //获取毛皮重量
 Weight_Maopi = HX711_Read(); 

  //WIFI
  pinMode(LED, OUTPUT);
  //Serial.begin(115200);//串口初始化(WIFI),硬件串口 rx=0,tx=1
  //Serial.begin(9600);
  finger.begin(9600);//指纹模块初始化,软件串口rx=2,tx=3
  //while (!Serial); 
  delay(5000);
  //Serial.println("\n\n指纹传感器记录");
  Serial.print("启动成功!");
  //LOCK
  pinMode(lock, OUTPUT);
  pinMode(button, INPUT);
  
  


}
 void loop()    ///  一直循环{}内容  ----- 同while(1){xxx}
{
  //每一定时间查询一次设备在线状态,同时替代心跳
 if (millis() - lastCheckStatusTime > postingInterval) {
    checkStatus();
  }
  //checkout 50ms 后 checkin
 if ( checkoutTime != 0 && millis() - checkoutTime > 50 ) {
    checkIn();
    checkoutTime = 0;
  }
  //读取串口信息 ||buttonState == HIGH
 buttonState = digitalRead(button);
 while (Serial.available()||buttonState == HIGH) {


    buttonState = digitalRead(button);
    if (buttonState == HIGH){
      Weight = Get_Weight();  //计算放在传感器上的重物重量
      float kuaidi=float(Weight/1000);
      Serial.print(kuaidi,3);  //串口显示重量,3意为保留三位小数
      Serial.print(" kg\n"); //显示单位
      Serial.print("\n");  //显示单位
      scale.power_down();              // 将ADC置于休眠模式
      if (kuaidi>1){
        Serial.print("有快递,请指纹解锁!");
        FingerprintIdentification();
        buttonState=0;
        delay(3000);
        return;

  }
      else if (kuaidi<1){
        Serial.print("锁已开启,可以放入快递。");
        digitalWrite(lock, HIGH);
        delay(500); 
        digitalWrite(lock, LOW);        
        digitalWrite(LED, HIGH);
        delay(500);
        digitalWrite(LED, LOW);
        delay(500);
        digitalWrite(LED, HIGH);
        delay(500);
        digitalWrite(LED, LOW);
        sta = 3;
        buttonState=0;
        return;
        
  }

  
  } 

    String inputString = Serial.readStringUntil('\n');
    //检测json数据是否完整
    int jsonBeginAt = inputString.indexOf("{");
    int jsonEndAt = inputString.lastIndexOf("}");
    if (jsonBeginAt != -1 && jsonEndAt != -1) {
      //净化json数据
      inputString = inputString.substring(jsonBeginAt, jsonEndAt + 1);
      int len = inputString.length() + 1;
      char jsonString[len];
      inputString.toCharArray(jsonString, len);
      aJsonObject *msg = aJson.parse(jsonString);
      processMessage(msg);
      aJson.deleteItem(msg);
    }

    //-

    

  
  }


  
//--
  
 

 scale.power_up(); 


 
}


 //称重函数
long Get_Weight()
{
 HX711_Buffer = HX711_Read();    ///读取此时的传感器输出值
 Weight_Shiwu = HX711_Buffer;     ///将传感器的输出值储存
 Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
 Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue);    //AD值转换为重量(g)
 return Weight_Shiwu; 
}
unsigned long HX711_Read(void) //选择芯片工作方式并进行数据读取
{
 unsigned long count;   ///储存输出值  
 unsigned char i;     
   ////high--高电平 1  low--低电平 0  
 digitalWrite(HX711_DT, HIGH);   ////  digitalWrite作用: DT=1;
 delayMicroseconds(1); ////延时 1微秒  
 digitalWrite(HX711_SCK, LOW);  ////  digitalWrite作用: SCK=0;
 delayMicroseconds(1);   ////延时 1微秒  
 count=0; 
  while(digitalRead(HX711_DT));    //当DT的值为1时,开始ad转换
  for(i=0;i<24;i++)   ///24个脉冲,对应读取24位数值
 { 
   digitalWrite(HX711_SCK, HIGH);  ////  digitalWrite作用: SCK=0;
                                /// 利用 SCK从0--1 ,发送一次脉冲,读取数值
  delayMicroseconds(1);  ////延时 1微秒  
  count=count<<1;    ///用于移位存储24位二进制数值
  digitalWrite(HX711_SCK, LOW);   //// digitalWrite作用: SCK=0;为下次脉冲做准备
 delayMicroseconds(1);
   if(digitalRead(HX711_DT))    ///若DT值为1,对应count输出值也为1
   count++; 
 } 
  digitalWrite(HX711_SCK, HIGH);    ///再来一次上升沿 选择工作方式  128增益
 count ^= 0x800000;   //按位异或  不同则为1   0^0=0; 1^0=1;
///对应二进制  1000 0000 0000 0000 0000 0000  作用为将最高位取反,其他位保留原值
 delayMicroseconds(1);
 digitalWrite(HX711_SCK, LOW);      /// SCK=0;     
 delayMicroseconds(1);  ////延时 1微秒  
 return(count);     ///返回传感器读取值
}


//设备登录
//{"M":"checkin","ID":"xx1","K":"xx2"}\n
void checkIn() {
  Serial.print("{\"M\":\"checkin\",\"ID\":\"");
  Serial.print(DEVICEID);
  Serial.print("\",\"K\":\"");
  Serial.print(APIKEY);
  Serial.print("\"}\r\n");
}
//处理网络接收到到指令,执行相关动作
void processMessage(aJsonObject *msg) {
  aJsonObject* method = aJson.getObjectItem(msg, "M");
  if (!method) {
    return;
  }
  String M = method->valuestring;
  if (M == "WELCOME TO BIGIOT") {
    checkOut();
    checkoutTime = millis();
    return;
  }
  if (M == "connected") {
    checkIn();
  }
  if (M == "say") {
    aJsonObject* content = aJson.getObjectItem(msg, "C");
    aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
    String C = content->valuestring;
    String F_C_ID = client_id->valuestring;
    if (C == "play") {
          digitalWrite(LED, HIGH);
          delay(500);
          digitalWrite(LED, LOW);
          delay(500);
          digitalWrite(LED, HIGH);
          delay(500);
          digitalWrite(LED, LOW);
          say(F_C_ID, "lock on!");
    }
    if (C == "stop") {
      digitalWrite(lock, HIGH);
      delay(500); 
      digitalWrite(lock, LOW); 
      say(F_C_ID, "lock off!");
    }
    if (C == "new") {
      say(F_C_ID, "new finger");
      getFingerprintEnroll();
    }
    if (C == "open") {
      say(F_C_ID, "finger test ");
      FingerprintIdentification();   
    }
    if (sta == 1) {
      say(F_C_ID, "未知者尝试开启!");
  }
    if (sta == 3) {
      say(F_C_ID, "有你的快递!");
  }
    if (sta == 2) {
      say(F_C_ID, "锁已开启!");
  }
}
}//

//发送say指令,用于设备与用户、设备与设备间通讯
//{"M":"say","ID":"xx1","C":"xx2","SIGN":"xx3"}\n
void say(String ID, String c) {
  Serial.print("{\"M\":\"say\",\"ID\":\"");
  Serial.print(ID);
  Serial.print("\",\"C\":\"");
  Serial.print(c);
  Serial.print("\"}\r\n");
}
//强制设备下线,用消除设备掉线延时
void checkOut() {
  Serial.print("{\"M\":\"checkout\",\"ID\":\"");
  Serial.print(DEVICEID);
  Serial.print("\",\"K\":\"");
  Serial.print(APIKEY);
  Serial.print("\"}\n");
}
//查询设备在线状态
//{"M":"status"}\n
void checkStatus() {
  Serial.print("{\"M\":\"status\"}\n");
  lastCheckStatusTime = millis();
}


uint8_t readnumber(void) {
  uint8_t num = 0;
  
  while (num == 0) {
    while (! Serial.available());
    num = Serial.parseInt();
  }
  return num;
}

uint8_t getFingerprintEnroll() { //指纹注册
  Serial.println("准备注册指纹!");
  int id =1;
  Serial.print("登记ID #");
  Serial.println(id);
  int p = -1;
  Serial.print("等待有效的指纹注册为 #"); Serial.println(id);
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("采集指纹");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println(".");
      break;
    default:
      Serial.println("未知的错误");
      break;
    }
  }

  // OK success!

  p = finger.image2Tz(1);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("图像修改");
      break;
    default:
      Serial.println("未知的错误");
      return p;
  }
  
  Serial.println("请移动手指");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }
  Serial.print("ID "); Serial.println(id);
  p = -1;
  Serial.println("再次放置手指");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      Serial.println("图像已采集");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.print(".");
    default:
      Serial.println("未知的错误");
      break;
    }
  }

//第二次对比
  p = finger.image2Tz(2);
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("正在记录");
      break;
    default:
      Serial.println("未知的错误");
      return p;
  }
  
  // 创建成功
  Serial.print("创建指纹为 #");  Serial.println(id);
  
  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    Serial.println("匹配!");
    id++;
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    


    
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("连接错误");
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    Serial.println("指纹不匹配");
    digitalWrite(LED, LOW);
    return p;
  } else {
    Serial.println("未知的错误");
    return p;
  }   
  
  Serial.print("ID "); Serial.println(id);
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    Serial.println("已储存!");
  } else {
    Serial.println("未知的错误");
    return p;
  }   

}


uint8_t FingerprintIdentification()
{
  int p = -1;
  p=0;
  delay(2000);
  Serial.print("开始指纹测试:");
  p = finger.getImage();
  if (p == FINGERPRINT_OK) {
    Serial.print("开始指纹测试:");
    delay(2000);
    if (p != FINGERPRINT_OK)  return -1;
    delay(2000);
    p = finger.image2Tz();
    if (p != FINGERPRINT_OK)  return -1;
    delay(2000);
    p = 0;
    p = finger.fingerFastSearch();
    if (p != FINGERPRINT_OK)   Serial.print("没有找到指纹");
    delay(2000);                         //验证指纹
    if(finger.confidence>80){
      digitalWrite(LED, HIGH); 
      Serial.print("找到指纹ID为 #"); Serial.print(finger.fingerID);
      Serial.print("信心值: "); Serial.println(finger.confidence);
      sta=2;
      
      digitalWrite(lock, HIGH);
      delay(500); 
     // digitalWrite(lock, LOW);        
      digitalWrite(LED, HIGH);
      delay(500);
      digitalWrite(LED, LOW);
      delay(500);
      digitalWrite(LED, HIGH);
      delay(500);
      digitalWrite(LED, LOW);}
    else{
      Serial.print("指纹不匹配");
      sta=1;
      return p;
    }
  }
}


评论:共0条

评论支持使用[code][/code]标签添加代码
返回顶部