ผมขอสอบถามหน่อยครับ คือผมตั้งเวลาใน netpie ครับให้มันทำงานตามเวลาแต่มันทำได้แค่ 2 นาทีครับ แล้วดับเป็นเพราะอะไรครับ
#include <WiFi.h>
#include <PubSubClient.h>
#include <LoRa.h> // เรียกใช้ libraries LoRa
#include <SPI.h> // เรียกใช้ libraries SPI
#include <RTClib.h>
#include <Wire.h>
RTC_DS3231 RTC;
#define pump 13
#define sol1 4
#define sol2 16
#define Auto_Man 17
#define ss 5 // กำหนดขาสำหรับ lora ss
#define rst 14 // กำหนดขาสำหรับ lora rst
#define dio0 2 // กำหนดขาสำหรับ lora dio
#define AutoStatus 36
#define PumpStatus 39
const char* ssid = "Tanarak3 floor-2";
const char* password = "0801736767";
const char* mqtt_server = "broker.netpie.io";
const int mqtt_port = 1883;
const char* mqtt_Client = "b8853271-9f8f-46d8-b843-2a21323d7f3e";
const char* mqtt_username = "ubhWWWigyyLRWTUfeSMMBq2YDTgtmfA8";
const char* mqtt_password = "u_ecADqTCoX4W9At!RECy3RxuFASvhjS";
int analog_value = 0;
char msg[100];
long lastMsg = 0;
int humidity1;
int humidity2;
WiFiClient espClient;
PubSubClient client(espClient);
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting NETPIE2020 connection…");
if (client.connect(mqtt_Client, mqtt_username, mqtt_password)) {
Serial.println("NETPIE2020 connected");
client.subscribe("@msg/led");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
int hum1, hum2;
// Timer SET 1 //
int t1_on_hr = 0;
int t1_on_min = 0;
int t1_off_hr = 0;
int t1_off_min = 0;
// Timer SET 2 //
int t2_on_hr ;
int t2_on_min ;
int t2_off_hr ;
int t2_off_min ;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String message;
for (int i = 0; i < length; i++) {
message = message + (char)payload[i];
}
Serial.println(message);
// ----------------- Auto/Manual ----------------------------//
if (String(topic) == "@msg/led") {
// int hum1, hum2;
// // Timer SET 1
// int t1_on_hr = 0;
// int t1_on_min = 0;
// int t1_off_hr = 0;
// int t1_off_min = 0;
// // Timer SET 2
// int t2_on_hr = 0;
// int t2_on_min = 0;
// int t2_off_hr = 0;
// int t2_off_min = 0;
int index = message.indexOf("=") + 1;
if (message.indexOf("on_h1") != -1) {
t1_on_hr = (message.substring(index, message.length())).toInt();
Serial.print("t1_on_hr ====== ");
Serial.println(t1_on_hr);
} else if (message.indexOf("on_m1") != -1) {
Serial.print("t1_on_min ====== ");
t1_on_min = (message.substring(index, message.length())).toInt();
Serial.println(t1_on_min);
} else if (message.indexOf("off_h1") != -1) {
Serial.print("t1_off_hr ====== ");
t1_off_hr = (message.substring(index, message.length())).toInt();
Serial.println(t1_off_hr);
} else if (message.indexOf("off_m1") != -1) {
Serial.print("t1_off_min ====== ");
t1_off_min = (message.substring(index, message.length())).toInt();
Serial.println(t1_off_min);
} else if (message.indexOf("on_h2") != -1) {
t2_on_hr = (message.substring(index, message.length())).toInt();
Serial.print("t2_on_hr ====== ");
Serial.println(t2_on_hr);
} else if (message.indexOf("on_m2") != -1) {
Serial.print("t2_on_min ====== ");
t2_on_min = (message.substring(index, message.length())).toInt();
Serial.println(t2_on_min);
} else if (message.indexOf("off_h2") != -1) {
Serial.print("t2_off_hr ====== ");
t2_off_hr = (message.substring(index, message.length())).toInt();
Serial.println(t2_off_hr);
} else if (message.indexOf("off_m2") != -1) {
Serial.print("t2_off_min ====== ");
t2_off_min = (message.substring(index, message.length())).toInt();
Serial.println(t2_off_min);
} else if (message.indexOf("hum1") != -1) {
Serial.print("hum1 ====== ");
hum1 = (message.substring(index, message.length())).toInt();
Serial.println(hum1);
} else if (message.indexOf("hum2") != -1) {
Serial.print("hum2 ====== ");
hum2 = (message.substring(index, message.length())).toInt();
Serial.println(hum2);
}
if (message == "3on") {
digitalWrite(Auto_Man, HIGH);
client.publish("@shadow/data/update", "{\"data\" : {\"led3\" : \"3on\"}}");
Serial.println("Auto_Man ON");
} else if (message == "3off") {
digitalWrite(Auto_Man, LOW);
client.publish("@shadow/data/update", "{\"data\" : {\"led3\" : \"3off\"}}");
Serial.println("Auto_Man OFF");
}
// ----------------- pump control ----------------------------//
if (message == "on" ) {
digitalWrite(pump, HIGH);
client.publish("@shadow/data/update", "{\"data\" : {\"led\" : \"on\"}}");
Serial.println("pump ON");
} else if (message == "off") {
digitalWrite(pump, LOW);
client.publish("@shadow/data/update", "{\"data\" : {\"led\" : \"off\"}}");
Serial.println("pump OFF");
}
// ----------------- Solenoid Valve 1 ----------------------------//
if (message == "1on") {
digitalWrite(sol1, HIGH);
client.publish("@shadow/data/update", "{\"data\" : {\"led1\" : \"1on\"}}");
Serial.println("sol1 ON");
} else if (message == "1off") {
digitalWrite(sol1, LOW);
client.publish("@shadow/data/update", "{\"data\" : {\"led1\" : \"1off\"}}");
Serial.println("sol1 OFF");
}
// ----------------- Solenoid Valve 2 ----------------------------//
if (message == "2on") {
digitalWrite(sol2, HIGH);
client.publish("@shadow/data/update", "{\"data\" : {\"led2\" : \"2on\"}}");
Serial.println("sol2 ON");
} else if (message == "2off") {
digitalWrite(sol2, LOW);
client.publish("@shadow/data/update", "{\"data\" : {\"led2\" : \"2off\"}}");
Serial.println("sol2 OFF");
}
}
}
void control_auto() {
long now = millis();
if (now - lastMsg > 1000) {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
if (digitalRead(AutoStatus) == HIGH) {
DateTime now = RTC.now();
Serial.println("Timer Start");
if (now.hour() == t1_on_hr && now.minute() >= t1_on_min && now.minute() <= t1_on_min + 1 ) {
digitalWrite(pump, HIGH);
if (humidity1 < hum1) {
digitalWrite(sol1, HIGH);
} else {
digitalWrite(sol1, LOW);
}
if (humidity2 < hum2) {
digitalWrite(sol2, HIGH);
} else {
digitalWrite(sol2, LOW);
}
if (humidity2 > hum2 && humidity1 > hum1) {
digitalWrite(pump, LOW);
}
} else if (now.hour() == t2_on_hr && now.minute() >= t2_on_min && now.minute() <= t2_on_min + 1 ) {
digitalWrite(pump, HIGH);
if (humidity1 < hum1) {
digitalWrite(sol1, HIGH);
} else {
digitalWrite(sol1, LOW);
}
if (humidity2 < hum2) {
digitalWrite(sol2, HIGH);
} else {
digitalWrite(sol2, LOW);
}
if (humidity2 > hum2 && humidity1 > hum1) {
digitalWrite(pump, LOW);
}
} else {
digitalWrite(pump, LOW);
digitalWrite(sol1, LOW);
digitalWrite(sol2, LOW);
delay(1000);
}
}
}
}
void setup() {
Serial.begin(115200);
RTC.begin();
while (!Serial);
pinMode(pump, OUTPUT);
pinMode(sol1, OUTPUT);
pinMode(sol2, OUTPUT);
pinMode(Auto_Man, OUTPUT);
pinMode(AutoStatus, INPUT);
pinMode(PumpStatus, INPUT);
digitalWrite(pump, LOW);
Serial.println("Starting...");
if (WiFi.begin(ssid, password)) {
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
Serial.println("LoRa Receiver");
//setup LoRa transceiver module
LoRa.setPins(ss, rst, dio0); // ตั้งค่า pin Lora
if (!LoRa.begin(433E6)) { // เชื่อมต่อ lora
Serial.println("Starting LoRa failed!");
while (1);
}
}
//int humidity1;
//int humidity2;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
int packetSize = LoRa.parsePacket(); // เช็คว่ามีแพ็คเกจส่งมาจาก node ลูกหรือไม่
if (packetSize) { // ถ้ามีแพ็คเกจส่งมาจาก node ลูก
Serial.print("Received packet '");
String str = "";
while (LoRa.available()) { // ถ้ามีแพ็คเกจส่งมาจาก node ลูก
for (int i = 0; i < packetSize; i++) {
char c = (char)LoRa.read();// เก็บอักขระที่อ่านได้ทีละตัวไว้ที่ตัวแปร c
str += c;
}
}
// print RSSI of packet
Serial.print("data = ");
Serial.println(str);
char LoraData = str.charAt(0);
if (LoraData == '1') { // เช็คข้อมูลว่าส่งมาจาก node 1 หรือไม่ 1#xxx
humidity1 = str.substring(str.indexOf("#") + 1, str.length()).toInt(); // แยกข้อความจาก 1#xxx ให้เหลือแค่ xxx ที่เป็นค่าความชื้น
Serial.print("humidity1 = ");
Serial.println(humidity1);
} else if (LoraData == '2') { // เช็คข้อมูลว่าส่งมาจาก node 2 หรือไม่ 2#xxx
humidity2 = str.substring(str.indexOf("#") + 1, str.length()).toInt(); // แยกข้อความจาก 2#xxx ให้เหลือแค่ xxx ที่เป็นค่าความชื้น
Serial.print("humidity2 = ");
Serial.println(humidity2);
}
String place = "NECTEC";
long now = millis();
control_auto();
if (now - lastMsg > 5000) {
lastMsg = now;
String data = "{\"data\": {\"humidity1\":" + String(humidity1) + ", \"humidity2\":" + String(humidity2) + ", \"place\": \"" + String(place) + "\"}}";
Serial.println(data);
data.toCharArray(msg, (data.length() + 1));
client.publish("@shadow/data/update", msg);
client.publish("@shadow/data/update", data.c_str());
}
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
delay(15);
}
}**ตัวอักษรหนา**