|
调度器用毫秒,计时超过60000毫秒就不正常,怎么改成秒!
#include <Wire.h>
#include <DS1302.h>
#include "TaskScheduler.h" //包含此头文件,才能使用调度器
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS A0 //数据总线
OneWire oneWire(ONE_WIRE_BUS); //声明
DallasTemperature sensors(&oneWire); //声明温度变量
SoftwareSerial esp8266(5, 6); //rx,tx
DS1302 rtc(9, 8, 7); // RST, DAT, CLK
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
int x5 = A5;
boolean g_led1State=0; //多少任务变量(水泵)
//boolean g_led2State=0; //多少任务变量
String outa=""; //读取温度值存入变量
String outb=""; //读取日期值存入变量
String outc=""; //读取时间值存入变量
String outd=""; //读取气压值存入变量
String oute=""; //读取海拔值存入变量
//String outf=""; //读取温度值存入变量
String AP = "padavan-2x";
String PASS = "12345678";
String Data;
int countTrueCommand;
int countTimeCommand;
boolean found = false;
//*******************************
void setup() {
pinMode(x5, OUTPUT);
sensors.begin(); //初始化总线
sensors.setWaitForConversion(false); //设置为非阻塞模式
Sch.init(); //初始化调度器
Sch.addTask(led1Update,0, 20000,1); //从第 0 毫秒开始闪烁 LED,每隔 1s, 状态改变一次,温度
Sch.addTask(led2Update,20,3600000,1); //从第 20 毫秒开始闪烁 LED,每隔 1s,状态改变一次,水泵
Sch.addTask(led3Update,30,20000,1); //从第 20 毫秒开始闪烁 LED,每隔 1s,状态改变一次,时间
Sch.addTask(led4Update,40,20000,1); //从第 20 毫秒开始闪烁 LED,每隔 1s,状态改变一次,气压
Sch.addTask(led100Update,100,60000,1); //从第 20 毫秒开始闪烁 LED,每隔 1s,状态改变一次,上传
Sch.start();//启动调度器
}
void loop() {
Sch.dispatchTasks(); // 执行被调度的任务.
}
//温度代码
void led1Update()
{
float tempC = sensors.getTempCByIndex(0); //获取索引号0的传感器摄氏温度数据
if (tempC != DEVICE_DISCONNECTED_C) //如果获取到的温度正常
{
Serial.print("\n当前温度是: ");
Serial.print(tempC);
Serial.println(" ℃");
}
//Serial.println("发起温度转换");
sensors.requestTemperatures(); //发起新的温度转换
//Serial.print(tempC); //打印当前温度值
outa=String(tempC)+"℃";
//delay(1000);
//温度代码结束
}
void led2Update()
{
if(g_led1State==0)
{
g_led1State=1;
digitalWrite(x5, HIGH); // 打开水泵
//delay(1000); // 等待1秒钟
}
else
{
g_led1State=0;
digitalWrite(x5, LOW); // 关闭水泵
//delay(1000); // 等待1秒钟
}
}
void led100Update()
{
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
Data = "GET /crzz.php?aa=1&ab="+outd+"&ac="+outc+"&ad="+outb+"&ae="+outa;
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"192.168.10.204\",80",4,"OK");
sendCommand("AT+CIPSEND=0," +String(Data.length()+4),2,">");
esp8266.println(Data);delay(100);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",2,"OK");
}
//时间函数
void led3Update()
{
Serial.begin(9600);
//Serial.println(rtc.getDateStr(FORMAT_LONG, FORMAT_LITTLEENDIAN, '-'));
//Serial.print(rtc.getDOWStr());
//Serial.print(" ");
//Serial.println(rtc.getTimeStr());
outb=String(rtc.getDateStr(FORMAT_LONG, FORMAT_LITTLEENDIAN, '-'))+"-"+String(rtc.getTimeStr());
}
void led4Update()
{
Serial.begin(9600);
//Serial.println(F("BMP280 测试"));
if (!bmp.begin()) {
// Serial.println(F("找不到对应的传感器"));
while (1);
}
//Serial.print(F("温度 = "));
//Serial.print(bmp.readTemperature());
//Serial.println(" *C");
//Serial.print(F("气压 = "));
//Serial.print(bmp.readPressure());
//Serial.println(" Pa");
//Serial.print(F("海拔 = "));
//Serial.print(bmp.readAltitude(1013.25));
//Serial.println(" m");
outc=String(Serial.print(F("")))+String(bmp.readPressure())+"Pa";
outd=String(bmp.readAltitude(1013.25))+"m";
//delay(2000);
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while (countTimeCommand < (maxTime * 1))
{
esp8266.println(command);//at+cipsend
if (esp8266.find(readReplay)) //ok
{
found = true;
break;
}
countTimeCommand++;
}
if (found == true)
{
Serial.println("Yes");
countTrueCommand++;
countTimeCommand = 0;
}
if (found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
|
|