概述
文章目录
- 1、背景
- 2、项目整理
- 2.1 挪威牛栏气体检测
- 2.1.1 功能介绍
- 2.1.2 实物图片
- 2.1.3 视频
- 2.1.4 源代码
- 2.2 触摸屏显示称重
- 2.2.1 功能介绍
- 2.2.2 实物图片
- 2.2.3 视频
- 2.2.4 源代码
- 2.3 G7物流沙盘
- 2.3.1 功能介绍
- 2.3.2 实物图片
- 2.3.3 视频
- 2.3.4 源代码
- 2.4 阿里云家庭生产线
- 2.4.1 功能介绍
- 2.4.2 实物图片
- 2.4.3 视频
- 2.4.4 源代码
- 3、如何联系我们
博主联系方式汇总(非诚勿扰)
博主联系方式汇总(非诚勿扰)
博主联系方式汇总(非诚勿扰)
1、背景
了解博哥的同学都知道,作为一名电子爱好者,博哥除了平时乐于分享基础知识之外,还需要肩负起减轻家庭负担的责任(毕竟从农村走出来的孩子)。
目前,一般加博哥好友的人大概可以分为两类:
- 学习知识(这一部分目前基本上都很
正常运转
起来,更多人还是喜欢我的ESP8266开发系列
,主要着重点在于基础) - 项目开发、毕业设计等(这一部分内容缺失,没有一系列的帖子去
记录过博哥有经历过哪些项目开发
,不一定是我开发的。。。。)
所以,经过仔细考虑还是需要去介绍一下一些项目开发整理。
2、项目整理
主要分为几个项目:
- 挪威牛栏气体检测
- 触摸屏显示称重
- G7物流沙盘
- 阿里云家庭生产线
2.1 挪威牛栏气体检测
2.1.1 功能介绍
这是挪威的一个小项目,主要用来检测牛栏气体浓度。
- 0、电源输入为
110v~220v
交流电。 - 1、电路需要设计
备用电源电路
(也就是锂电池部分)。当电源停电时,自动启动备用电源。 - 2、MQ传感器探头不直接焊接在PCB板,而是通过座子接入。
- 3、 每x秒(x=1或者其他)读取一次MQ的值,并计算出
CH4, CO2, NH3, H2S ppm
值。 - 4、
显示屏
显示ppm值和pm2.5值以及温湿度值。 - 5、当这些值任意一个超过
限定值
时,就发送一条短信
。 - 6、当备用电源启动时,也发送一条短信。
2.1.2 实物图片
1、1* ATmega2560-16AU及其配套的阻容器件
2、1* 3.5寸触摸屏
3、1*MQ4
传感器(测 CH4)
4、1*MQ135
传感器(测NH3)
5、1*MQ136
传感器(测 H2S)
6、1*SGP30
芯片(测 CO2)
7、1*DHT11
(测温湿度)
8、1压电式有源蜂鸣器
9、15v继电器
10、1*GSM868
模块
11、1*灰尘传感器
(测PM2.5)
12、可充电电池组
13、其他器件,如电阻、电容、指示灯、电源开关等
2.1.3 视频
演示视频暂时无法上传(需要的可以另外帖子留言
)
2.1.4 源代码
暂未公开
2.2 触摸屏显示称重
2.2.1 功能介绍
- 1、一共12个相同的物块,放在称重平台。
- 2、当在对应位置放置1个物块后,就亮一颗灯(白色暖光色)。HMI屏幕显示对应的数量,屏幕右下角显示剩余个数。中间一个大数字显示剩余个数(假如剩4个),右下角显示已完成个数8/12。
- 3、当12个位置都放满后,12颗灯就闪烁闪一圈就行,且HMI屏幕显示successful。
- 4、RGB值需要支持调整,例如灯光亮度。
2.2.2 实物图片
1、1Arduino uno
2、1HX711模块称重套件
3、1*12颗WS2812 RGB灯条
3、其他可能需要的元器件(按钮、电阻、电容、导线等)
2.2.3 视频
演示视频暂时无法上传(需要的可以另外帖子留言
)
2.2.4 源代码
#include "HX711.h"
#include <Adafruit_NeoPixel.h>
#define PIN 5 //LED引脚
#define NUMPIXELS 12 //LED数量
#define loding_delay 10 //进度条响应速度,数值越大响应越慢
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const int LOADCELL_DOUT_PIN = 2; //HX711驱动DOUT引脚
const int LOADCELL_SCK_PIN = 3; //HX711驱动CLK引脚
bool change_flag = 0;
bool LED_flag = 1;
int LED_num = 0;
int Old_num = 0;
int J_val = 0;
int Old_J_val = 0;
int Old_val = 0;
char payload[30];
char End[] = {0xff,0xff,0xff};
int Bright = 200; //LED亮度设置 0-255
#define PIN 5 //LED引脚
#define NUMPIXELS 12 //LED数量
float calibration_factor = 415; //重力校准系数
float units; //储存质量变量,单位g
float units_flag = 7.5; //单个物品重量设置,单位g
float units_Debug = 1; //质量误差设置
HX711 scale;
void setup() {
Serial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //重力常量初始化
scale.tare(); //质量置0(去皮)
pixels.begin(); //LED初始化
pixels.setBrightness(Bright); //LED亮度设置
}
void loop() {
get_Weight(); //获取质量
getLED_Num(); //获取LED模式
tft_Run(); //刷新屏幕显示
LED_Run(); //刷新LED状态
J_val = map(LED_num,0,12,0,100);
j_val(); //屏幕进度条单独刷新
}
/********获取质量函数*********/
void get_Weight(){
if (scale.is_ready()) { //检测传感器是否就绪
units = scale.get_units(); //更新重量
//Serial.print("HX711 reading: "); //串口打印重量调试时可打开
//Serial.println(units);
}
}
/********质量状态转化函数*********/
void getLED_Num(){
if(units < (units_flag - units_Debug)){LED_num = 0;}
else if(units > (units_flag - units_Debug) && units < (units_flag*2 - units_Debug)){LED_num = 1;}
else if(units > (units_flag*2 - units_Debug) && units < (units_flag*3 - units_Debug)){LED_num = 2;}
else if(units > (units_flag*3 - units_Debug) && units < (units_flag*4 - units_Debug)){LED_num = 3;}
else if(units > (units_flag*4 - units_Debug) && units < (units_flag*5 - units_Debug)){LED_num = 4;}
else if(units > (units_flag*5 - units_Debug) && units < (units_flag*6 - units_Debug)){LED_num = 5;}
else if(units > (units_flag*6 - units_Debug) && units < (units_flag*7 - units_Debug)){LED_num = 6;}
else if(units > (units_flag*7 - units_Debug) && units < (units_flag*8 - units_Debug)){LED_num = 7;}
else if(units > (units_flag*8 - units_Debug) && units < (units_flag*9 - units_Debug)){LED_num = 8;}
else if(units > (units_flag*9 - units_Debug) && units < (units_flag*10 - units_Debug)){LED_num = 9;}
else if(units > (units_flag*10 - units_Debug) && units < (units_flag*11 - units_Debug)){LED_num = 10;}
else if(units > (units_flag*11 - units_Debug) && units < (units_flag*12 - units_Debug)){LED_num = 11;}
else if(units > (units_flag*12 - units_Debug)){LED_num = 12;}
}
/********显示屏状态刷新函数*********/
void tft_Run(){
if(LED_num != Old_num){ //减少不必要的串口输出
Old_num = LED_num; //更新状态
if(LED_num <= 11){
if(change_flag == 1){ //页面切换标志位,防止不必要的刷新
sprintf(payload,"page 0");
Serial.print(payload); //切换到页面0
Serial.print(End);
change_flag = 0;
}
sprintf(payload,"t0.txt="%d"",LED_num);
Serial.print(payload); //更新t0状态
Serial.print(End);
sprintf(payload,"t1.txt="%d"",(12-LED_num));
Serial.print(payload); //更新t1状态
Serial.print(End);
}
else{
sprintf(payload,"page 1");
Serial.print(payload); //切换到页面1
Serial.print(End);
change_flag = 1; //重置切换标志位
}
}
}
/********进度条状态刷新函数*********/
void j_val(){
if(Old_J_val != J_val){
Old_J_val = J_val;
for(Old_val;Old_val < J_val;Old_val++){
sprintf(payload,"j0.val=%d",Old_val);
Serial.print(payload);
Serial.print(End);
delay(loding_delay);
}
for(Old_val;Old_val > J_val;Old_val--){
sprintf(payload,"j0.val=%d",Old_val);
Serial.print(payload);
Serial.print(End);
delay(loding_delay);
}
}
}
/********LED状态刷新函数*********/
void LED_Run(){
if(LED_num == 0){ //没有时将灯熄灭
pixels.clear();
pixels.show();
LED_flag = 1;
}
else if(LED_num == 12){ //12个全满时闪烁一圈
if(LED_flag == 1){
pixels.clear();
LED_flag = 0;
for(int i = 0;i < 12;i++){
pixels.setPixelColor(i, pixels.Color(255, 255, 255));
pixels.show();
delay(100);
}
}
}
else{ //其他情况点亮对应数量的灯
pixels.clear();
LED_flag = 1;
for(int m = 0; m < (LED_num); m++){
pixels.setPixelColor(m, pixels.Color(255, 255, 255));
}
pixels.show();
}
}
2.3 G7物流沙盘
2.3.1 功能介绍
- 1、车子是独立运行,当车子进入红外传感器是,会停留约1min。
- 2、当第1个红外传感器检测到车子时,Arduino串口发送十六进制数据 0xEE 0X01。以此类推第2个红外传感器发送0xEE 0x02,第15个就发送0xEE 0x0F。
- 3、从车子进入某个红外传感器到车子离开,有且只有发送一次十六进制数据。
2.3.2 实物图片
1、1Arduino uno/nano
2、15红外传感器,传感器到Arduino的线长是3m。
3、其他可能需要的元器件(面包板、按钮、电阻、电容、导线等)
2.3.3 视频
演示视频暂时无法上传(需要的可以另外帖子留言
)
2.3.4 源代码
#define versions "FirmwareVersions:V3.0"
/**************************引脚定义***************************/
//注A6、A7脚只能作为模拟信号
#define Switch_1 A0
#define Switch_2 A1
#define Switch_3 A2
#define Switch_4 A3
#define Switch_5 A4
#define Switch_6 3
#define Switch_7 4
#define Switch_8 5
#define Switch_9 6
#define Switch_10 7
#define Switch_11 8
#define Switch_12 9
#define Switch_13 10
#define Switch_14 11
#define Switch_15 12
#define MAX485_RDE 13
bool Scan_flag = 1; //扫描标志位
bool Set_State = 0; //触发电平设置
char Sent_Data[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
char Sent_void[1];
char Sent_head[] = {0xEE};
char Last_Sent_void[1];
void setup(){
Serial.begin(9600,SERIAL_8N1); //Arduino Serial UART Config
pinMode(Switch_1,INPUT);
pinMode(Switch_2,INPUT);
pinMode(Switch_3,INPUT);
pinMode(Switch_4,INPUT);
pinMode(Switch_5,INPUT);
pinMode(Switch_6,INPUT);
pinMode(Switch_7,INPUT);
pinMode(Switch_8,INPUT);
pinMode(Switch_9,INPUT);
pinMode(Switch_10,INPUT);
pinMode(Switch_11,INPUT);
pinMode(Switch_12,INPUT);
pinMode(Switch_13,INPUT);
pinMode(Switch_14,INPUT);
pinMode(Switch_15,INPUT);
pinMode(MAX485_RDE,OUTPUT);
digitalWrite(MAX485_RDE,HIGH);
}
/**************************按键扫描函数***************************/
void Switch_Scan(){
if (Scan_flag == 1){
if(digitalRead(Switch_1) == Set_State){Sent_void[0] = Sent_Data[0];}
if(digitalRead(Switch_2) == Set_State){Sent_void[0] = Sent_Data[1];}
if(digitalRead(Switch_3) == Set_State){Sent_void[0] = Sent_Data[2];}
if(digitalRead(Switch_4) == Set_State){Sent_void[0] = Sent_Data[3];}
if(digitalRead(Switch_5) == Set_State){Sent_void[0] = Sent_Data[4];}
if(digitalRead(Switch_6) == Set_State){Sent_void[0] = Sent_Data[5];}
if(digitalRead(Switch_7) == Set_State){Sent_void[0] = Sent_Data[6];}
if(digitalRead(Switch_8) == Set_State){Sent_void[0] = Sent_Data[7];}
if(digitalRead(Switch_9) == Set_State){Sent_void[0] = Sent_Data[8];}
if(digitalRead(Switch_10) == Set_State){Sent_void[0] = Sent_Data[9];}
if(digitalRead(Switch_11) == Set_State){Sent_void[0] = Sent_Data[10];}
if(digitalRead(Switch_12) == Set_State){Sent_void[0] = Sent_Data[11];}
if(digitalRead(Switch_13) == Set_State){Sent_void[0] = Sent_Data[12];}
if(digitalRead(Switch_14) == Set_State){Sent_void[0] = Sent_Data[13];}
if(digitalRead(Switch_15) == Set_State){Sent_void[0] = Sent_Data[14];}
}
}
/**************************数据发送函数***************************/
void Send(){
if(Sent_void[0] != Last_Sent_void[0]){
Last_Sent_void[0] = Sent_void[0];
//Serial.print("chang");
Serial.write(Sent_head);
Serial.write(Sent_void[0]);
}
}
void loop(){
Switch_Scan();
Send();
}
2.4 阿里云家庭生产线
2.4.1 功能介绍
- 1、阿里云物联网平台设置3个温度值起始加热温度T1(Start_heating)、停止加热温度T2(Stop_heating)、过热通风温度T3(Ventilate_temp)。
- 2、nodemcu读取阿里云设置的3个温度值。
- 3、nodemcu把3个温度值通过串口的形式发给Arduino。
- 4、Arduino实时监测8个DS18B20传感器的数据。
- 5、当8个温度值,任意一个低于T1,则给高电平闭合8合1对应的继电器。如果对应的通风口打开(即Switch_L6或Switch_R6是低电平),则给高低电平反转左边或者右边2合1对应的继电器。直到Switch_L6或Switch_R6是高电平。
- 6、当8个温度值,任意一个高于T2,则给低电平断开8合1对应的继电器。
- 7、当左边或者右边的4个温度值的平均值,高于T3。如果Switch_L5/Switch_R5为低电平,则高电平闭合Relay_L5/R5,低电平断开Relay_L6/R6,实现正转。反之,如果Switch_L5/R5为高电平,则低电平断开Relay_L5/R5、L6/R6实现停止。
- 8、12个微动限位开关,按下是高电平、不按下是低电平。其中8个和DS18B20传感器对应,按下表示盖子盖上,不按下表示盖子打开。另外4个(Switch_L5、Switch_L6、Switch_R5、Switch_R6)作为限位开关。
当按下Switch_L5或Switch_L6时,低电平断开2个对应的继电器(Relay_L5、L6)
同理当按下Switch_R5或Switch_R6时,低电平断开2个对应的继电器(Relay_R5、R6) - 9、设置的3个温度值,需要断电保存。也就是断电重新开机,需要恢复上一次设置的值。当然如果阿里云重新设置了其中的一个或多个温度值,则也要同步更新。
- 10、要把8个DS18B20传感器的数据和12个微动开关的状态,以及12个继电器的状态显示在云端。
- 11、特别重要:如果是首次降温到T1,在降温过程中不管温度如何,都不操作12个继电器。当不是首次低于T1,也可以通过阿里云的开关恢复到首次降温。
2.4.2 实物图片
1、Arduino mega 2560代码
2、nodemcu 8266 代码
2.4.3 视频
演示视频暂时无法上传(需要的可以另外帖子留言
)
2.4.4 源代码
Mega2560:
#include <DallasTemperature.h>
#include <EEPROM.h>
int addr = 0;
bool first_flag = 1; //首次加热标志位
unsigned long lastSend = 0; //上传时间现态
unsigned long last_SendF = 0; //刷新时间现态
unsigned long eeprom_Send = 0;
union data{
float a;
byte b[4];//一个float4字节
};
data EP_T1,EP_T2,EP_T3,EP_T4;
bool L_Wind_flag ;//左通风状态
bool R_Wind_flag ;//右通风状态
float Temperature_L1;
float Temperature_L2;
float Temperature_L3;
float Temperature_L4;
float Temperature_R1;
float Temperature_R2;
float Temperature_R3;
float Temperature_R4;
float Average_L ;
float Average_R ;
float T1 ; //起始加热温度
float T2 ; //停止加热温度
float T3 ; //停止通风温度
float T4 ; //过热通风温度
float Old_T1 = T1; //起始加热温度
float Old_T2 = T2; //停止加热温度
float Old_T3 = T3; //停止通风温度
float Old_T4 = T4; //过热通风温度
String Clouddata[5] = {""};
bool Switch_L1; //盒子状态
bool Switch_L2;
bool Switch_L3;
bool Switch_L4;
bool Switch_L5;
bool Switch_L6;
bool Switch_R1;
bool Switch_R2;
bool Switch_R3;
bool Switch_R4;
bool Switch_R5;
bool Switch_R6;
bool Relay_L1; //继电器状态
bool Relay_L2;
bool Relay_L3;
bool Relay_L4;
bool Relay_L5;
bool Relay_L6;
bool Relay_R1;
bool Relay_R2;
bool Relay_R3;
bool Relay_R4;
bool Relay_R5;
bool Relay_R6;
#define Switch_Pin_L1 20
#define Switch_Pin_L2 21
#define Switch_Pin_L3 22
#define Switch_Pin_L4 23
#define Switch_Pin_L5 28
#define Switch_Pin_L6 29
#define Switch_Pin_R1 24
#define Switch_Pin_R2 25
#define Switch_Pin_R3 26
#define Switch_Pin_R4 27
#define Switch_Pin_R5 30
#define Switch_Pin_R6 31
#define Relay_Pin_L1 2
#define Relay_Pin_L2 3
#define Relay_Pin_L3 4
#define Relay_Pin_L4 5
#define Relay_Pin_L5 10
#define Relay_Pin_L6 11
#define Relay_Pin_R1 6
#define Relay_Pin_R2 7
#define Relay_Pin_R3 8
#define Relay_Pin_R4 9
#define Relay_Pin_R5 12
#define Relay_Pin_R6 13
#define button_state HIGH //按钮按下时电平
#define Hot_state HIGH //继电器加热电平
#define ONE_WIRE_BUS 14 //温度传感器数据总线连接在IO14
OneWire oneWire(ONE_WIRE_BUS); //声明
DallasTemperature sensors(&oneWire); //声明
DeviceAddress myDS18B20[8]; //声明一个地址对象
//获取数据
void get_data(){
if(Serial1.available() > 0){
String Rawdata = "";
while (Serial1.available() > 0){
Rawdata += char(Serial1.read());
delay(2);
}
// Serial.println(Rawdata);
int index = 0;
for(int i = 0; i < Rawdata.length(); i++){
if(Rawdata[i]==','){
Clouddata[index] = "";
for(i++; Rawdata[i]!=','; i++){
Clouddata[index] += Rawdata[i];
}
// Serial.println(String("i=")+ i);
// Serial.println(Clouddata[index]);
if(i < Rawdata.length() - 2)i--;
index++;
}
}
T1 = Clouddata[0].toFloat();
T2 = Clouddata[1].toFloat();
T3 = Clouddata[2].toFloat();
T4 = Clouddata[3].toFloat();
}
}
//校验数据
void data_Check(){
if(T1 > Old_T2 || T1 == 0){T1 = Old_T1;}
else{Old_T1 = T1;}
if(T2 > Old_T3 || T2 == 0){T2 = Old_T2;}
else{Old_T2 = T2;}
if(T3 > Old_T4 || T3 == 0){T3 = Old_T3;}
else{Old_T3 = T3;}
if(T4 < Old_T3 || T4 == 0){T4 = Old_T4;}
else{Old_T4 = T4;}
}
//发送数据
void sent_data(){
String all_data = ","+ String(Temperature_L1)+","+String(Temperature_L2)+","+String(Temperature_L3)+","+String(Temperature_L4)+","+String(Temperature_R1)+","+String(Temperature_R2)+","+String(Temperature_R3)+","+String(Temperature_R4);
all_data += ","+ String(T1)+","+String(T2)+","+String(T3)+","+String(T4);
all_data += ","+String(Switch_L1)+","+String(Switch_L2)+","+String(Switch_L3)+","+String(Switch_L4)+","+String(Switch_L5)+","+String(Switch_L6)+","+String(Switch_R1)+","+String(Switch_R2)+","+String(Switch_R3)+","+String(Switch_R4)+","+String(Switch_R5)+","+String(Switch_R6);
all_data += ","+String(Relay_L1)+","+String(Relay_L2)+","+String(Relay_L3)+","+String(Relay_L4)+","+String(Relay_L5)+","+String(Relay_L6)+","+String(Relay_R1)+","+String(Relay_R2)+","+String(Relay_R3)+","+String(Relay_R4)+","+String(Relay_R5)+","+String(Relay_R6)+",";
Serial1.print(all_data);
}
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
eeprom_read(); //读取缓存数据
T1 = Old_T1;
T2 = Old_T2;
T3 = Old_T3;
T4 = Old_T4;
Serial.println("");
sensors.begin(); //初始化总线
Serial.print("总线上DS18系列设备数量为:");
Serial.println(sensors.getDS18Count());
Serial.print("总线是否需要寄生供电(1-true; 0-false):");
Serial.println(sensors.isParasitePowerMode());
Serial.print("总线上设备数据最大分辨率为:");
Serial.println(sensors.getResolution());
for(int i = 0;i <= 7;i++){
sensors.getAddress(myDS18B20[i], i); //获取索引号0的设备地址(设备序列号)
}
pinMode(Switch_Pin_L1,INPUT_PULLUP); //GetButton(Switch_Pin_L1,0)
pinMode(Switch_Pin_L2,INPUT_PULLUP);
pinMode(Switch_Pin_L3,INPUT_PULLUP);
pinMode(Switch_Pin_L4,INPUT_PULLUP);
pinMode(Switch_Pin_L5,INPUT);
pinMode(Switch_Pin_L6,INPUT);
pinMode(Switch_Pin_R1,INPUT_PULLUP);
pinMode(Switch_Pin_R2,INPUT_PULLUP);
pinMode(Switch_Pin_R3,INPUT_PULLUP);
pinMode(Switch_Pin_R4,INPUT_PULLUP);
pinMode(Switch_Pin_R5,INPUT);
pinMode(Switch_Pin_R6,INPUT);
pinMode(Relay_Pin_L1,OUTPUT);
pinMode(Relay_Pin_L2,OUTPUT);
pinMode(Relay_Pin_L3,OUTPUT);
pinMode(Relay_Pin_L4,OUTPUT);
pinMode(Relay_Pin_L5,OUTPUT);
pinMode(Relay_Pin_L6,OUTPUT);
pinMode(Relay_Pin_R1,OUTPUT);
pinMode(Relay_Pin_R2,OUTPUT);
pinMode(Relay_Pin_R3,OUTPUT);
pinMode(Relay_Pin_R4,OUTPUT);
pinMode(Relay_Pin_R5,OUTPUT);
pinMode(Relay_Pin_R6,OUTPUT);
while(Serial1.read()>= 0){}//clear serialbuffer
}
/********写入数据函数***********/
void eeprom_write(){
EP_T1.a = Old_T1;
EP_T2.a = Old_T2;
EP_T3.a = Old_T3;
EP_T4.a = Old_T4;
for(int i = 0;i <= 3;i++){
EEPROM.write(i,EP_T1.b[i]);
EEPROM.write(i+4,EP_T2.b[i]);
EEPROM.write(i+8,EP_T3.b[i]);
EEPROM.write(i+12,EP_T4.b[i]);
}
}
/********读取数据函数***********/
void eeprom_read(){
for(int i = 0;i <= 3;i++){
EP_T1.b[i] = EEPROM.read(i);
EP_T2.b[i] = EEPROM.read(i+4);
EP_T3.b[i] = EEPROM.read(i+8);
EP_T4.b[i] = EEPROM.read(i+12);
Old_T1 = EP_T1.a ;
Old_T2 = EP_T2.a ;
Old_T3 = EP_T3.a ;
Old_T4 = EP_T4.a ;
}
}
void eeprom_chack(){
if(millis() - eeprom_Send >5){
if(EP_T1.a != Old_T1 || EP_T2.a != Old_T2 || EP_T3.a != Old_T3|| EP_T4.a != Old_T4){
eeprom_write();
}
}
}
/********按钮结构体*********/
struct Button{
int buttonState; //按钮状态变量
int lastButtonState = LOW; //按钮状态初始化
long lastDebounceTime = 0; //记录抖动变量
long debounceDelay = 50; //抖动时间变量50ms
};
Button button[12]; //新建1个按钮
/********按钮消抖函数********/
void GetButton(int button_pin,int button_NUM){
int reading = digitalRead(button_pin); //读取I/O口状态
if(reading!=button[button_NUM].lastButtonState){ //如果状态发生改变
button[button_NUM].lastDebounceTime = millis();//更新时间
} //如果等待时间大于debounceDelay
if((millis()-button[button_NUM].lastDebounceTime)>button[button_NUM].debounceDelay){
if(reading!=button[button_NUM].buttonState){ //读取状态不等于按钮状态
button[button_NUM].buttonState = reading; //更新buttonState
}
}
button[button_NUM].lastButtonState = reading; //更新lastButtonState
}
/********获取按钮状态函数********/
void GetAllButton(){
GetButton(Switch_Pin_L1,0);
GetButton(Switch_Pin_L2,1);
GetButton(Switch_Pin_L3,2);
GetButton(Switch_Pin_L4,3);
GetButton(Switch_Pin_L5,4);
GetButton(Switch_Pin_L6,5);
GetButton(Switch_Pin_R1,6);
GetButton(Switch_Pin_R2,7);
GetButton(Switch_Pin_R3,8);
GetButton(Switch_Pin_R4,9);
GetButton(Switch_Pin_R5,10);
GetButton(Switch_Pin_R6,11);
Switch_L1 = button[0].lastButtonState;
Switch_L2 = button[1].lastButtonState;
Switch_L3 = button[2].lastButtonState;
Switch_L4 = button[3].lastButtonState;
Switch_L5 = button[4].lastButtonState;
Switch_L6 = button[5].lastButtonState;
Switch_R1 = button[6].lastButtonState;
Switch_R2 = button[7].lastButtonState;
Switch_R3 = button[8].lastButtonState;
Switch_R4 = button[9].lastButtonState;
Switch_R5 = button[10].lastButtonState;
Switch_R6 = button[11].lastButtonState;
}
/*************获取温度函数*************/
void get_Temp(){
for(int i = 0;i <= 7;i++){
sensors.requestTemperaturesByAddress(myDS18B20[i]); //向总线上指定地址传感器发送温度转换请求
//Serial.print(i);
//Serial.print(">当前摄氏是: ");
//Serial.println(sensors.getTempCByIndex(i));
if(i == 0){Temperature_L1 = sensors.getTempCByIndex(i);}
if(i == 1){Temperature_L2 = sensors.getTempCByIndex(i);}
if(i == 2){Temperature_L3 = sensors.getTempCByIndex(i);}
if(i == 3){Temperature_L4 = sensors.getTempCByIndex(i);}
if(i == 4){Temperature_R1 = sensors.getTempCByIndex(i);}
if(i == 5){Temperature_R2 = sensors.getTempCByIndex(i);}
if(i == 6){Temperature_R3 = sensors.getTempCByIndex(i);}
if(i == 7){Temperature_R4 = sensors.getTempCByIndex(i);}
}
Average_L = (Temperature_L1+Temperature_L2+Temperature_L3+Temperature_L4)/4;
Average_R = (Temperature_R1+Temperature_R2+Temperature_R3+Temperature_R4)/4;
}
/*************加热触发函数*************/
void Heating(){
if(first_flag == 1){
if(Temperature_L1 < T1 || Temperature_L2 < T1 || Temperature_L3 < T1 || Temperature_L4 < T1 || Temperature_R1 < T1 || Temperature_R2 < T1 || Temperature_R3 < T1 || Temperature_R4 < T1){
first_flag = 0; //如果温度降到T1,更新标志位
} //各盒子温度低于T1开始加热,否则停止加热
}
if(Temperature_L1 < T1 ){digitalWrite(Relay_Pin_L1,Hot_state);Relay_L1=Hot_state;}
if(Temperature_L1 > T2 ){digitalWrite(Relay_Pin_L1,!Hot_state);Relay_L1=!Hot_state;}
if(Temperature_L2 < T1 ){digitalWrite(Relay_Pin_L2,Hot_state);Relay_L2=Hot_state;}
if(Temperature_L2 > T2 ){digitalWrite(Relay_Pin_L2,!Hot_state);Relay_L2=!Hot_state;}
if(Temperature_L3 < T1 ){digitalWrite(Relay_Pin_L3,Hot_state);Relay_L3=Hot_state;}
if(Temperature_L3 > T2 ){digitalWrite(Relay_Pin_L3,!Hot_state);Relay_L3=!Hot_state;}
if(Temperature_L4 < T1 ){digitalWrite(Relay_Pin_L4,Hot_state);Relay_L4=Hot_state;}
if(Temperature_L4 > T2 ){digitalWrite(Relay_Pin_L4,!Hot_state);Relay_L4=!Hot_state;}
if(Temperature_R1 < T1 ){digitalWrite(Relay_Pin_R1,Hot_state);Relay_R1=Hot_state;}
if(Temperature_R1 > T2 ){digitalWrite(Relay_Pin_R1,!Hot_state);Relay_R1=!Hot_state;}
if(Temperature_R2 < T1 ){digitalWrite(Relay_Pin_R2,Hot_state);Relay_R2=Hot_state;}
if(Temperature_R2 > T2 ){digitalWrite(Relay_Pin_R2,!Hot_state);Relay_R2=!Hot_state;}
if(Temperature_R3 < T1 ){digitalWrite(Relay_Pin_R3,Hot_state);Relay_R3=Hot_state;}
if(Temperature_R3 > T2 ){digitalWrite(Relay_Pin_R3,!Hot_state);Relay_R3=!Hot_state;}
if(Temperature_R4 < T1 ){digitalWrite(Relay_Pin_R4,Hot_state);Relay_R4=Hot_state;}
if(Temperature_R4 > T2 ){digitalWrite(Relay_Pin_R4,!Hot_state);Relay_R4=!Hot_state;}
}
/*************过热通风函数*************/
void Cooling(){
if(first_flag == 0){
if(Average_L > T4){Left_Vent_On();L_Wind_flag = 1;}
if(Average_L < T3){Left_Vent_Off();L_Wind_flag = 0;}
if(Average_R > T4){Right_Vent_On();R_Wind_flag = 1;}
if(Average_L < T3){Right_Vent_Off();R_Wind_flag = 0;}
}
}
void Left_Vent_On(){
if(button[4].lastButtonState != button_state){
digitalWrite(Relay_Pin_L5,HIGH);
digitalWrite(Relay_Pin_L6,LOW);
}
else{
digitalWrite(Relay_Pin_L5,LOW);
digitalWrite(Relay_Pin_L6,LOW);
}
}
void Left_Vent_Off(){
if(button[5].lastButtonState != button_state){
digitalWrite(Relay_Pin_L5,LOW);
digitalWrite(Relay_Pin_L6,HIGH);
}
else{
digitalWrite(Relay_Pin_L5,LOW);
digitalWrite(Relay_Pin_L6,LOW);
}
}
void Right_Vent_On(){
if(button[10].lastButtonState != button_state){
digitalWrite(Relay_Pin_R5,HIGH);
digitalWrite(Relay_Pin_R6,LOW);
}
else{
digitalWrite(Relay_Pin_R5,LOW);
digitalWrite(Relay_Pin_R6,LOW);
}
}
void Right_Vent_Off(){
if(button[11].lastButtonState != button_state){
digitalWrite(Relay_Pin_R5,LOW);
digitalWrite(Relay_Pin_R6,HIGH);
}
else{
digitalWrite(Relay_Pin_R5,LOW);
digitalWrite(Relay_Pin_R6,LOW);
}
}
/*************串口显示函数*************/
void Serial_Send(){
String data1 = "----------------------------------------------------------------rn";
data1 +=" T1:" + String(T1) + " tT2:" + String(T2) + " tT3:" + String(T3) + " tT4:" + String(T4) +"rn";
data1 += "tSwitchtRelaytSensortSensortRelaytSwitchrn";
data1 += "L1t " + String(Switch_L1)+ "t " + String(Relay_L1)+ "t" + String(Temperature_L1)+ "t" + String(Temperature_R1)+ "t " + String(Relay_R1) + "t " +String(Switch_R1)+ "t" +"R1rn";
data1 += "L2t " + String(Switch_L2)+ "t " + String(Relay_L2)+ "t" + String(Temperature_L2)+ "t" + String(Temperature_R2)+ "t " + String(Relay_R2) + "t " +String(Switch_R2)+ "t" +"R2rn";
data1 += "L3t " + String(Switch_L3)+ "t " + String(Relay_L3)+ "t" + String(Temperature_L3)+ "t" + String(Temperature_R3)+ "t " + String(Relay_R3) + "t " +String(Switch_R3)+ "t" +"R3rn";
data1 += "L4t " + String(Switch_L4)+ "t " + String(Relay_L4)+ "t" + String(Temperature_L4)+ "t" + String(Temperature_R4)+ "t " + String(Relay_R4) + "t " +String(Switch_R4)+ "t" +"R4rn";
data1 += "L5t " + String(Switch_L5)+ "t " + String(Relay_L5)+ "t" + String(" Average ")+ "t " + String(Relay_R5) + "t " +String(Switch_R5)+ "t" +"R5rn";
data1 += "L6t " + String(Switch_L6)+ "t " + String(Relay_L6)+ "t" + String(Average_L)+ "t" + String(Average_R)+ "t " + String(Relay_R6) + "t " +String(Switch_R6)+ "t" +"R6rn";
//data += "L_windt " + String(L_Wind_flag)+ "t " + String(L_Wind_flag)+ "t" + String(Average_L)+ "t" + String(Average_R)+ "t " + String(R_Wind_flag) + "t " +String(R_Wind_flag)+ "t" +"R_Windrn";
data1 += "Is it the first cooling :" + String(first_flag);
Serial.println(data1);
}
/*************随机温度函数*************/
void random_temp(){
T1 = 35;
T2 = 39;
T3 = 40;
T4 = 50;
Temperature_L1 = random(25,60);
Temperature_L2 = random(25,60);
Temperature_L3 = random(25,60);
Temperature_L4 = random(25,60);
Temperature_R1 = random(25,60);
Temperature_R2 = random(25,60);
Temperature_R3 = random(25,60);
Temperature_R4 = random(25,60);
}
void loop() {
if(millis() - lastSend >= 1000){
sent_data();
}
if(millis() - last_SendF >= 3000){
get_Temp(); //获取温度
//random_temp(); //随机获取温度
GetAllButton(); //获取按钮状态函数
Heating(); //加热判断
Cooling(); //过热通风
Serial_Send(); //串口打印状态
}
eeprom_chack();
get_data();
data_Check();
}
nodemcu:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <aliyun_mqtt.h>
#include <Ticker.h>
void initSystem();
void initWifiManager();
void configModeCallback(WiFiManager *myWiFiManager);
void saveConfigCallback();
void tick();
bool shouldSaveConfig = false;
Ticker ticker;
char mqtt_server[40];
char mqtt_port[6] = "8080";
char api_key[34] = "Your ApiKey";
int Switch_L1; //盒子状态
int Switch_L2;
int Switch_L3;
int Switch_L4;
int Switch_L5;
int Switch_L6;
int Switch_R1;
int Switch_R2;
int Switch_R3;
int Switch_R4;
int Switch_R5;
int Switch_R6;
int Relay_L1; //继电器状态
int Relay_L2;
int Relay_L3;
int Relay_L4;
int Relay_L5;
int Relay_L6;
int Relay_R1;
int Relay_R2;
int Relay_R3;
int Relay_R4;
int Relay_R5;
int Relay_R6;
float T1 ; //起始加热温度
float T2 ; //停止加热温度
float T3 ; //停止通风温度
float T4 ; //过热通风温度
unsigned long lastSend = 0; //上云时间现态
float Old_T1 = T1; //起始加热温度
float Old_T2 = T2; //停止加热温度
float Old_T3 = T3; //停止通风温度
float Old_T4 = T4; //过热通风温度
String Clouddata[40] = {""};
float Temperature_L1;
float Old_Temperature_L1;
float Temperature_L2;
float Temperature_L3;
float Temperature_L4;
float Temperature_R1;
float Temperature_R2;
float Temperature_R3;
float Temperature_R4;
/* 产品的三元组信息,根据9个测试设备的三元组,每个设备都烧录不同的*/
#define PRODUCT_KEY "a1q7OV3Qy0Y"
#define DEVICE_NAME "code_V1"
#define DEVICE_SECRET "u9rwv4Z6OSz0kt6rUfol9SaZ2UOydI3K"
#define ALINK_BODY_FORMAT "{"id":"123","version":"1.0","method":"%s","params":%s}"
#define ALINK_TOPIC_PROP_POST "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post"
#define ALINK_TOPIC_PROP_POSTRSP "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/event/property/post_reply"
#define ALINK_TOPIC_PROP_SET "/sys/" PRODUCT_KEY "/" DEVICE_NAME "/thing/service/property/set"
#define ALINK_METHOD_PROP_POST "thing.event.property.post"
//创建WiFiClient实例
WiFiClient espClient;
//创建MqttClient实例
PubSubClient mqttClient(espClient);
//监听云端下发指令并处理
void callback(char *topic, byte *payload, unsigned int length){
payload[length] = '