Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- # => Send to Gateway //index 0
- A => Send to Node A //index 0
- B => Send to Node B... //index 0
- client to Gateway
- e.g: sender("#27.91314ABCDE");
- data.substring(0, 1); //node tujuan
- data.substring(1, 5); //nilai suhu
- data.substring(5, 7); //jam
- data.substring(7, 9); // menit
- data.substring(9); // Jalur Node
- sync feedback: sender("#sync");
- Gateway to client
- e.g: sender("Ar1=1"); //Relay
- data.substring(0, 1); //node tujuan
- data.substring(1, 3); //kode relay
- data.substring(4, 5); //status relay
- e.g: sender("Async"); //cek status node aktif tidak
- data.substring(0, 1); //node tujuan
- data.substring(1);
- A => Node A
- B => Node B
- C => Node C
- D => Node D
- E => Node E
- F => Gateway
- */
- /*OLED Display*/
- //#include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- // Color definitions
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define RED 0xF800
- #define GREEN 0x07E0
- #define CYAN 0x07FF
- #define MAGENTA 0xF81F
- #define YELLOW 0xFFE0
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
- #define SDA A4
- #define SCL A5
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
- /*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 <OneWire.h> //library OneWire
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS A3
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- float Celcius = 0;
- /**/
- /*EEPROM*/
- #include <EEPROM.h>
- byte channel = 101;
- /*Pin Switch*/
- #define S1 6
- /*Relay*/
- #define r1 2
- #define r2 3
- #define r3 4
- /*Multi Task*/
- unsigned long previousMillis1 = 0; // millis() returns an unsigned long.
- unsigned long previousMillis2 = 0; // millis() returns an unsigned long.
- unsigned long previousMillis3 = 0;
- unsigned long previousMillis4 = 0;
- unsigned long previousMillis5 = 0;
- /*Radio*/
- #include <SPI.h>
- #include "nRF24L01.h"
- #include "RF24.h"
- RF24 radioReceived(7, 8); // CE, CSN
- RF24 radioSender(9, 10); // CE, CSN
- //RF24 radio(7, 8, 13, 12, 11);
- const byte pipes[][6] = {"1000", "2000", "3000", "4000", "5000", "6000"};
- char msg[32];
- void configReceived() {
- // radioReceived.begin();
- display.clearDisplay();
- display.setTextColor(WHITE);
- display.setTextSize(1);
- display.setCursor(0, 10);
- if (!radioReceived.begin()) {
- display.println("radioReceived failed");
- display.display();
- for (;;);
- }
- if (radioReceived.isChipConnected()) {
- display.println("radioReceived is connected");
- display.display();
- }
- radioReceived.setChannel(channel);
- radioReceived.setPayloadSize(32);
- radioReceived.setPALevel(RF24_PA_MAX);
- radioReceived.setDataRate(RF24_250KBPS);
- radioReceived.openReadingPipe(0, pipes[4]); //B
- // radioSender.enableAckPayload();
- radioReceived.startListening();
- }
- void configSender() {
- // radioSender.begin();
- display.setCursor(0, 40);
- if (!radioSender.begin()) {
- display.println("radioSender failed");
- display.display();
- for (;;);
- }
- if (radioSender.isChipConnected()) {
- display.println("radioSender is connected");
- display.display();
- }
- radioSender.setChannel(channel);
- radioSender.setPayloadSize(32);
- radioSender.setDataRate(RF24_250KBPS);
- radioSender.setPALevel(RF24_PA_MAX);
- // radioSender.enableAckPayload();
- radioSender.stopListening();
- }
- void stateRelay() {
- if (EEPROM.read(r1) == 1) {
- digitalWrite(r1, HIGH);
- } else {
- digitalWrite(r1, LOW);
- }
- if (EEPROM.read(r2) == 1) {
- digitalWrite(r2, HIGH);
- } else {
- digitalWrite(r2, LOW);
- }
- if (EEPROM.read(r3) == 1) {
- digitalWrite(r3, HIGH);
- } else {
- digitalWrite(r3, LOW);
- }
- }
- void(* resetFunc) (void) = 0;
- const String this_node = "E";
- void setup() {
- //Serial.begin(115200);
- //Serial.println("#################################################");
- //Serial.println("Ready");
- if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
- //Serial.println(F("SSD1306 allocation failed"));
- for (;;);
- }
- configReceived();
- configSender();
- delay(2000);
- sensors.begin();
- if (!rtc.begin()) {
- //Serial.println("RTC allocation failed");
- for (;;);
- } else {
- //Serial.println("RTC OK");
- }
- //rtc.adjust(DateTime(2020, 10, 31, 2, 5, 0));
- pinMode(S1, INPUT_PULLUP);
- pinMode(r1, OUTPUT);
- pinMode(r2, OUTPUT);
- pinMode(r3, OUTPUT);
- stateRelay();
- display.clearDisplay();
- display.setTextColor(WHITE);
- display.setTextSize(1);
- display.setCursor(0, 0);
- display.print(EEPROM.read(r1));
- display.setCursor(0, 10);
- display.print(EEPROM.read(r2));
- display.setCursor(0, 20);
- display.print(EEPROM.read(r3));
- display.display();
- delay(2000);
- display.clearDisplay();
- //Serial.println("Ready all");
- }
- void loop() {
- radio_read();
- int readS1 = digitalRead(S1);
- ////Serial.println(readS1);
- if (readS1 == LOW) {
- sender("#27,9393E", 5);
- delay(1000);
- }
- }
- void sender(String d, byte pipe) {
- display.setCursor(0, 20);
- display.print("Writing: "); display.print(d);
- display.display();
- d.toCharArray(msg, sizeof(msg));
- radioSender.openWritingPipe(pipes[pipe]);
- radioSender.stopListening();
- byte cek = radioSender.write(msg, sizeof(msg));
- /*while (cek == 0) {
- cek = radioSender.write(msg, sizeof(msg));
- }*/
- // delayMicroseconds(130);
- delay(130);
- }
- byte u = 0;
- String temp;
- void radio_read() {
- delay(5);
- byte pipe_num = 0;
- //if (radioReceived.available(&pipe_num)) {
- while (radioReceived.available()) {
- display.clearDisplay();
- display.setTextColor(WHITE);
- display.setTextSize(1);
- display.setCursor(0, 0);
- display.print("Reading....");
- radioReceived.read(msg, sizeof(msg));
- String data = String(msg);
- display.setCursor(0, 10);
- display.print("Data: "); display.println(data);
- if (data.substring(0, 1) == this_node) {
- if (data.substring(1, 3) == "r1") {
- if (data.substring(4, 5) == "1") {
- digitalWrite(r1, HIGH);
- EEPROM.update(r1, 1);
- } else {
- digitalWrite(r1, LOW);
- EEPROM.update(r1, 0);
- }
- } else if (data.substring(1, 3) == "r2") {
- if (data.substring(4, 5) == "1") {
- digitalWrite(r2, HIGH);
- EEPROM.update(r2, 1);
- } else {
- digitalWrite(r2, LOW);
- EEPROM.update(r2, 0);
- }
- } else if (data.substring(1, 3) == "r3") {
- if (data.substring(4, 5) == "1") {
- digitalWrite(r3, HIGH);
- EEPROM.update(r3, 1);
- } else {
- digitalWrite(r3, LOW);
- EEPROM.update(r3, 0);
- }
- } else if (data.substring(1) == "sync") {
- String n = "#";
- n += this_node;
- n += "sync";
- temp = "Send A&D";
- sender(n, 5); // send F
- }
- } else if (data.substring(0, 1) == "#") { //from C or D
- String n = data;
- n += this_node;
- temp = "Forwad E";
- sender(n, 5); //send C
- u++;
- } else if (data.substring(0, 1) == "C") { //from F
- temp = "Send C";
- sender(data, 2); //send C
- u++;
- } else if (data.substring(0, 1) == "D") { //from F
- temp = "Send D";
- sender(data, 3); //send D
- u++;
- } else if (data.substring(0, 1) == "A" || data.substring(0, 1) == "B") { //from F
- temp = "Send C&D";
- sender(data, 2); //send C
- delay(1000);
- sender(data, 3); //send D
- u++;
- } else {
- temp = "Send C&D";
- sender(data, 2); //send C
- delay(1000);
- sender(data, 3); //send D
- u++;
- }
- display.setCursor(0, 30);
- display.print("Temp: "); display.print(temp);
- display.setCursor(0, 50);
- display.print("Sender: "); display.print("Berhasil-"); display.print(u);
- display.display();
- temp = "";
- delay(100);
- }
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement