本帖最后由 风醒吹梦忱 于 2021-11-13 15:40 编辑
我想用NodeMCU8266在控制继电器开关的时候向TF卡记录铂电阻温度数据.
测量铂电阻的,MAX31865 和SD卡模块都是接的的 D5/D6/D7 SPI.
现在运行程序时MAX31865失灵,读出的温度固定为-242.02.
SD记录正常.
如果将代码中的SD.begin();注释,MAX31865恢复正常.- #define BLINKER_WIFI
- #include <ESP8266WiFi.h>
- #include <Blinker.h>
- #include <DHT.h>
- #include <Adafruit_MAX31865.h>
- #include <SPI.h>
- #include <SD.h>
- #define JI_DIAN_QI D0//静态继电器接口D0 LOW开启 HIGH关闭
- //MAX31865 温度模拟信号转数字信号PT100=430 The value of the Rref resistor. Use 430.0!
- #define RREF 430.0
- // MAX3185接口(SPI通讯)Use software SPI: CS, DI, DO, CLK
- Adafruit_MAX31865 max1 = Adafruit_MAX31865(D2, D7, D6, D5);
- //初始化开发板WIFI
- char auth[] = "oooooooooooo";
- char ssid[] = "oooo";
- char pswd[] = "oooooo";
- /* 新建APP按钮组件对象*/
- //APP继电器按钮对象
- BlinkerButton JiDianQi("btn-jdq");
- //APP电流数值对象
- BlinkerNumber ELEC("num-dianliu");
- //APP线圈温度值对象 xian_quan_wen_du_read num-xianquan
- BlinkerNumber XQTemperature("num-xianquan");
- //初始化电流变量
- float elec_read = 0;
- //初始化温度变量
- float xq_temperature_read = 0;
- // 创建File实例化对象 用于将数据记录SD卡
- File myFile;
- // 定义SD/TF卡模块cs引脚为D1
- const int sd_cs = D1;
- String xing_qi[] = {"日", "一", "二", "三", "四", "五", "六"};
- //当前时间变量
- String tag_time = "获取时间失败";
- void setup() {
- //启动串口
- Serial.begin(115200);
- //点灯初始化
- BLINKER_DEBUG.stream(Serial);
- // 初始化blinker
- Blinker.begin(auth, ssid, pswd);
- JiDianQi.attach(jidianqi_diancifa);
- //初始化继电器接口,设置继电器控制接口为输出模式
- pinMode(JI_DIAN_QI, OUTPUT);
- //默认关闭继电器 LOW开启 HIGH关闭
- digitalWrite(JI_DIAN_QI, HIGH);
- //初始化MAX31865
- Serial.println("Adafruit MAX31865 PT100 Sensor Test!");
- pinMode(D2,OUTPUT);
- max1.begin(MAX31865_2WIRE); // set to 2WIRE or 4WIRE as necessary
- //SD/TF卡记录模块初始化
- Serial.print("Initializing SD card...");
- //SDCS并设置为输出OUTPUT模式
- pinMode(sd_cs, OUTPUT);
- if (!SD.begin(sd_cs))
- {
- Serial.println("SD initialization failed!");
-
- }else{
- Serial.println("SD initialization done.");
- }
- //注册一个心跳包
- Blinker.attachHeartbeat(heartbeat);
- //初始化NTP时间,北京时间+8:00
- Blinker.setTimezone(8.0);
- }
- void loop() {
- //APP循环运行
- Blinker.run();
- //改变按钮图案
- btnState();
-
- //SPI MAX31865采样 线圈温度(铂热电阻)采样
- uint16_t rtd = max1.readRTD();
- //Serial.print("RTD value: "); Serial.println(rtd);
- float ratio = rtd;
- ratio /= 32768;
- //Serial.print("Ratio = "); Serial.println(ratio, 8);
- // Serial.print("Resistance = "); Serial.println(RREF * ratio, 8);
- Serial.print("Temperature = "); Serial.println(max1.temperature(100, RREF));
- xq_temperature_read = max1.temperature(100, RREF);
- XQTemperature.print(xq_temperature_read);
- // Check and print any faults
- uint8_t fault = max1.readFault();
- if (fault) {
- Serial.print("Fault 0x"); Serial.println(fault, HEX);
- if (fault & MAX31865_FAULT_HIGHTHRESH) {
- Serial.println("RTD High Threshold");
- }
- if (fault & MAX31865_FAULT_LOWTHRESH) {
- Serial.println("RTD Low Threshold");
- }
- if (fault & MAX31865_FAULT_REFINLOW) {
- Serial.println("REFIN- > 0.85 x Bias");
- }
- if (fault & MAX31865_FAULT_REFINHIGH) {
- Serial.println("REFIN- < 0.85 x Bias - FORCE- open");
- }
- if (fault & MAX31865_FAULT_RTDINLOW) {
- Serial.println("RTDIN- < 0.85 x Bias - FORCE- open");
- }
- if (fault & MAX31865_FAULT_OVUV) {
- Serial.println("Under/Over voltage");
- }
- max1.clearFault();
- }
- //串口输出时间,测试
- tag_time = "当前时间:"+String(Blinker.year()) + "年" + String(Blinker.month()) + "月" + String(Blinker.mday()) + "日 " + String(Blinker.hour()) + ":" + String(Blinker.minute()) + ":" + String(Blinker.second()) + " 星期" + xing_qi[Blinker.wday()];
- Serial.println(tag_time);
- Blinker.delay(2000);
- }
- //记录模块方法
- void jiluSD() {
- //SD/TF打开文件。 一次只能打开一个文件
- //必须先关闭该文件再打开另一个文件。
- myFile = SD.open("jilu.txt", FILE_WRITE);
- // 如果文件打开,则进行写入
- if (myFile)
- {
- Serial.print("数据写入 jilu.txt...");
- if (digitalRead(JI_DIAN_QI) == HIGH) {
- //如果继电器为关闭状态
- myFile.println(tag_time + "关");
- } else {
- myFile.println(tag_time + "开");
- }
- // 关闭文件打开状态
- myFile.close();
- Serial.println("写入结束.");
- }
- else
- {
- //如果文件没有打开,则向串口输出文件打开错误
- Serial.println("error opening test.txt");
- myFile.close();
- }
- }
- //按钮组件对象绑定该函数,当点击按钮时调用函数
- void jidianqi_diancifa(const String & state) {
- BLINKER_LOG("get button state: ", state);
- digitalWrite(JI_DIAN_QI, !digitalRead(JI_DIAN_QI));
- jiluSD();//记录到SD卡
- //反馈继电器状态,改变图标颜色
- btnState();
- Blinker.vibrate();
- }
- //判断按钮状态函数
- void btnState() {
- //反馈继电器状态,改变图标颜色
- if (digitalRead(JI_DIAN_QI) == LOW) {
- JiDianQi.icon("fas fa-burn");
- JiDianQi.color("#00ff7f");// #00FF7F绿色
- JiDianQi.text("开启");
- } else {
- JiDianQi.icon("fas fa-burn");
- JiDianQi.color("#ff0000");// #FF0000红色
- JiDianQi.text("关闭");
- }
- }
- // 心跳包函数
- void heartbeat() {
- //反馈继电器状态,改变图标颜色
- btnState();
- JiDianQi.print("on");
- //APP电流数值
- ELEC.print(elec_read);
- }
复制代码
接线图:
SD模块是5V ,MAX31865是3.3V.
感谢大佬们.
|