Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*RTC*/
- #include "RTClib.h"
- RTC_DS3231 rtc;
- char weekDay[][7] = {"AHAD", "SENIN", "SELASA", "RABU", "KAMIS", "JUM'AT", "SABTU", "AHAD"}; // array hari, dihitung mulai dari senin, hari senin angka nya =0,
- char monthYear[][4] = { "DES", "JAN", "FEB", "MAR", "APR", "MEI", "JUN", "JUL", "AGU", "SEP", "OKT", "NOV", "DES" };
- /**/
- /*SENSOR*/
- #include <DHT.h>
- #define DHTTYPE DHT22
- #define DHTPIN 5
- DHT dht(DHTPIN, DHTTYPE);
- /**/
- /*EEPROM*/
- #include <EEPROM.h>
- /*Multi Task*/
- unsigned long previousMillis1 = 0;
- unsigned long previousMillis2 = 0;
- unsigned long previousMillis3 = 0;
- unsigned long previousMillis4 = 0;
- unsigned long previousMillis5 = 0;
- /*Pin Switch*/
- #define S1 6
- /*Relay*/
- //byte relay1 = 2;
- #define relay1 2
- #define relay2 3
- #define relay3 4
- /*Radio Gead*/
- #include <RHReliableDatagram.h>
- #include <RH_NRF24.h>
- #include <SPI.h>
- #define nodeA 1
- #define nodeB 2
- #define nodeC 3
- #define nodeD 4
- #define nodeE 5
- #define nodeF 6
- // Singleton instance of the radio driver
- RH_NRF24 driver;
- // RH_NRF24 driver(8, 7); // For RFM73 on Anarduino Mini
- // Class to manage message delivery and receipt, using the driver declared above
- RHReliableDatagram manager(driver, nodeA);
- /*OLED display*/
- #include <Wire.h>
- #include "SSD1306Ascii.h"
- #include "SSD1306AsciiWire.h"
- #define I2C_ADDRESS 0x3C
- SSD1306AsciiWire oled;
- void handleRelay() {
- if (EEPROM.read(relay1) == 255) {
- EEPROM.write(relay1, 0);
- }
- if (EEPROM.read(relay2) == 255) {
- EEPROM.write(relay2, 0);
- }
- if (EEPROM.read(relay3) == 255) {
- EEPROM.write(relay3, 0);
- }
- }
- void stateRelay() {
- if (EEPROM.read(relay1) == 1) {
- digitalWrite(relay1, HIGH);
- } else {
- digitalWrite(relay1, LOW);
- }
- if (EEPROM.read(relay2) == 1) {
- digitalWrite(relay2, HIGH);
- } else {
- digitalWrite(relay2, LOW);
- }
- if (EEPROM.read(relay3) == 1) {
- digitalWrite(relay3, HIGH);
- } else {
- digitalWrite(relay3, LOW);
- }
- }
- void(* resetFunc) (void) = 0;
- void oledConfig() {
- Wire.begin();
- Wire.setClock(400000L);
- oled.begin(&Adafruit128x64, I2C_ADDRESS);
- oled.setFont(Adafruit5x7);
- oled.set2X();
- oled.clear();
- oled.print("Ready");
- delay(1000);
- }
- const String this_node = "A";
- void setup() {
- // Serial.begin(9600);
- oledConfig();
- pinMode(S1, INPUT_PULLUP);
- pinMode(relay1, OUTPUT);
- pinMode(relay2, OUTPUT);
- pinMode(relay3, OUTPUT);
- handleRelay();
- stateRelay();
- oled.set1X();
- oled.clear();
- oled.setCursor(0, 0);
- if (!manager.init()) {
- // Serial.println("Radio Failed");
- oled.print("Radio Failed!");
- for (;;);
- } else {
- oled.print("Radio begin");
- }
- oled.setCursor(0, 1);
- if (!rtc.begin()) {
- // Serial.println("RTC allocation failed");
- oled.print("RTC failed!");
- for (;;);
- } else {
- //Serial.println("RTC OK");
- oled.print("RTC begin");
- }
- // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // rtc.adjust(DateTime(2021, 4, 21, 6, 12, 30));
- dht.begin();
- delay(2000);
- // Serial.println("Ready All");
- }
- uint8_t data[RH_NRF24_MAX_MESSAGE_LEN];
- //strcat(data, d); => append
- //itoa(dat, String, 10);
- //String str = (char*)buff;
- void sender(String t, byte p, String nodes) {
- t.toCharArray(data, 32);
- oled.clear();
- oled.setCursor(0, 1);
- oled.print("Writing: ");
- oled.setCursor(0, 2);
- oled.print(t);
- oled.setCursor(0, 3);
- oled.print("Sending: ");
- oled.setCursor(0, 4);
- oled.print((char*)data);
- oled.setCursor(0, 5);
- if (manager.sendtoWait(data, sizeof(data), p)) {
- oled.print("Status: "); oled.println("Berhasil");
- } else {
- oled.print("Status: "); oled.println("Gagal");
- }
- oled.setCursor(0, 6);
- oled.print(nodes);
- }
- void sender2(uint8_t* t, byte p) {
- oled.setCursor(0, 1);
- oled.print("Writing: "); oled.println((char*)t);
- oled.setCursor(0, 3);
- if (manager.sendtoWait(t, sizeof(t), p)) {
- oled.print("Status: "); oled.println("Berhasil");
- } else {
- oled.print("Status: "); oled.println("Gagal");
- }
- }
- void radioRead() {
- if (manager.available()) {
- oled.clear();
- // Wait for a message addressed to us from the client
- uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
- uint8_t len = sizeof(buf);
- uint8_t from;
- if (manager.recvfromAck(buf, &len, &from)) {
- oled.setCursor(0, 0);
- oled.print((char*)buf);
- oled.setCursor(0, 4);
- String temp = (char*)buf;
- if (temp.substring(0, 1) == this_node) {
- if (temp.substring(1, 3) == "r1") {
- if (temp.substring(4, 5) == "1") {
- digitalWrite(relay1, HIGH);
- EEPROM.update(relay1, 1);
- oled.print("Relay 1 On");
- } else {
- digitalWrite(relay1, LOW);
- EEPROM.update(relay1, 0);
- oled.print("Relay 1 Off");
- }
- } else if (temp.substring(1, 3) == "r2") {
- if (temp.substring(4, 5) == "1") {
- digitalWrite(relay2, HIGH);
- EEPROM.update(relay2, 1);
- oled.print("Relay 2 On");
- } else {
- digitalWrite(relay2, LOW);
- EEPROM.update(relay2, 0);
- oled.print("Relay 2 Off");
- }
- } else if (temp.substring(1, 3) == "r3") {
- if (temp.substring(4, 5) == "1") {
- digitalWrite(relay3, HIGH);
- EEPROM.update(relay3, 1);
- oled.print("Relay 3 On");
- } else {
- digitalWrite(relay3, LOW);
- EEPROM.update(relay3, 0);
- oled.print("Relay 3 Off");
- }
- }
- } else if (temp.substring(0, 1) == "#") { //from B
- String n = (char*)buf + this_node;
- oled.print("Forwad C");
- sender(n, nodeC, "Forwad C"); //send C
- } else if (temp.substring(0, 1) == "B") {
- oled.print("Forwad B");
- sender(buf, nodeB, "Forwad B");
- }
- }
- } else {
- oled.setCursor(0, 6);
- oled.print("Radio standby");
- }
- }
- void loop() {
- DateTime now = rtc.now();
- int jam = now.hour(); //jam
- int menit = now.minute(); //menit
- int detik = now.second();
- int thn = now.year(); //Tahun
- int bln = now.month();
- int tgl = now.day();
- radioRead();
- oled.setCursor(120, 0);
- oled.print(this_node);
- int readS1 = digitalRead(S1);
- ////Serial.println(readS1);
- if (readS1 == LOW) {
- sender2("#27,9393A", nodeE);
- delay(1000);
- }
- unsigned long currentMillis = millis();
- if ((unsigned long)(currentMillis - previousMillis3) >= 120000) {
- float h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- float t = dht.readTemperature();
- previousMillis3 = currentMillis;
- String allData = String(h, 1);
- allData += String(t, 1);
- if (jam < 10) {
- allData += "0" + String(jam);
- } else {
- allData += String(jam);
- }
- // allData += String(":");
- if (menit < 10) {
- allData += "0" + String(menit);
- } else {
- allData += String(menit);
- }
- if (detik < 10) {
- allData += "0" + String(detik);
- } else {
- allData += String(detik);
- }
- if (tgl < 10) {
- allData += "0" + String(tgl);
- } else {
- allData += String(tgl);
- }
- // allData += String("-");
- if (bln < 10) {
- allData += "0" + String(bln);
- } else {
- allData += String(bln);
- }
- // allData += String("-");
- allData += String(thn);
- // allData += (String) EEPROM.read(relay1);
- // allData += (String) EEPROM.read(relay2);
- allData += (String) EEPROM.read(relay3);
- allData += this_node;
- sender("#" + allData, nodeC, "Sending data to C");
- delay(1000);
- sender("#" + allData, nodeB, "Sending data to B");
- }
- if ((unsigned long)(currentMillis - previousMillis2) >= 1000) {
- previousMillis2 = currentMillis;
- oled.clear();
- float h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- float t = dht.readTemperature();
- oled.setCursor(0, 0);
- oled.print("H: "); oled.print(h);
- oled.setCursor(0, 1);
- oled.print("T: "); oled.print(t);
- String hari = String(weekDay[now.dayOfTheWeek()]);
- String bulan = String(monthYear[now.month()]);
- oled.setCursor(0, 3);
- if (jam < 10) {
- oled.print("0");
- } oled.print(jam);
- oled.print(":");
- if (menit < 10) {
- oled.print("0");
- } oled.print(menit);
- oled.print(":");
- if (detik < 10) {
- oled.print("0");
- } oled.print(detik);
- oled.setCursor(0, 4);
- oled.print(hari); oled.print(", "); oled.print(now.day()); oled.print(" "); oled.print(bulan); oled.print(" "); oled.print(thn);
- }
- }
Add Comment
Please, Sign In to add comment