Created
January 7, 2020 20:17
-
-
Save erikbuild/c0f5083aa59549448007007acc8f7b37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* HART Industrial Unions, LLC | |
* Erik J. Reynolds | |
* 2019-11-13 | |
* | |
* Position Sensor Switch Status Sender | |
*/ | |
// includes | |
#include <SPI.h> | |
#include <RH_RF69.h> | |
#include <RHReliableDatagram.h> | |
// radio setup | |
#define RF69_FREQ 915.0 | |
// Metro M4 Airlift | |
#define RFM69_CS 11 | |
#define RFM69_INT 0 | |
#define RFM69_RST 1 | |
#define LED 13 | |
// Where to send packets to! | |
#define DEST_ADDRESS 1 | |
// change addresses for each client board, any number :) | |
// 2 == Air Compressor | |
// 3+ == future | |
#define MY_ADDRESS 2 | |
// Singleton instance of the radio driver | |
RH_RF69 rf69(RFM69_CS, RFM69_INT); | |
// Class to manage message delivery and receipt, using the driver declared above | |
RHReliableDatagram rf69_manager(rf69, MY_ADDRESS); | |
const int sensor = 4; | |
int state; // 0 = closed/contact, 1 = open/no-contact | |
void setup() { | |
//Serial.begin(115200); // serial console only used if connected to a computer for testing anyways | |
pinMode(sensor, INPUT_PULLUP); // reed relay | |
pinMode(LED, OUTPUT); | |
pinMode(RFM69_RST, OUTPUT); | |
digitalWrite(RFM69_RST, LOW); | |
// manual reset | |
digitalWrite(RFM69_RST, HIGH); | |
delay(10); | |
digitalWrite(RFM69_RST, LOW); | |
delay(10); | |
if (!rf69_manager.init()) { | |
//Serial.println(F("RFM69 radio init failed")); | |
while (1); | |
} | |
//Serial.println(F("RFM69 radio init OK!")); | |
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module) | |
// No encryption | |
if (!rf69.setFrequency(RF69_FREQ)) { | |
//Serial.println(F("setFrequency failed")); | |
} | |
// If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the | |
// ishighpowermodule flag set like this: | |
rf69.setTxPower(20, true); // range from 14-20 for power, 2nd arg must be true for 69HCW | |
// The encryption key has to be the same as the one in the server | |
// this is the default key from the example code but sure why not | |
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; | |
rf69.setEncryptionKey(key); | |
pinMode(LED, OUTPUT); | |
//Serial.print("RFM69 radio @"); | |
//Serial.print((int)RF69_FREQ); | |
//Serial.println(" MHz"); | |
} | |
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN]; // receive buffer | |
uint8_t data[] = " OK"; // I don't know what this is used for. | |
void loop() { | |
state = digitalRead(sensor); | |
char radiopacket[20] = "AIRCOMPRESSOR: "; | |
if (state == HIGH) { | |
// NO CONTACT | |
strcat(radiopacket, "OFF"); | |
} else { | |
// CONTACT | |
strcat(radiopacket, "ON"); | |
} | |
//Serial.print(F("Sending ")); | |
//Serial.println(radiopacket); | |
// Send a message to the DESTINATION! | |
if (rf69_manager.sendtoWait((uint8_t *)radiopacket, strlen(radiopacket), DEST_ADDRESS)) { | |
// Now wait for a reply from the server | |
uint8_t len = sizeof(buf); | |
uint8_t from; | |
if (rf69_manager.recvfromAckTimeout(buf, &len, 2000, &from)) { | |
buf[len] = 0; // zero out remaining string | |
//Serial.print(F("Got reply from #")); Serial.print(from); | |
//Serial.print(F(" [RSSI :")); | |
//Serial.print(rf69.lastRssi()); | |
//Serial.print(F("] : ")); | |
//Serial.println((char*)buf); | |
Blink(LED, 40, 3); //blink LED 3 times, 40ms between blinks | |
} else { | |
//Serial.println(F("No reply, is anyone listening?")); | |
} | |
} else { | |
//Serial.println(F("Sending failed (no ack)")); | |
} | |
// Wait a second... | |
delay(1000); | |
} | |
void Blink(byte PIN, byte DELAY_MS, byte loops) { | |
for (byte i=0; i<loops; i++) { | |
digitalWrite(PIN,HIGH); | |
delay(DELAY_MS); | |
digitalWrite(PIN,LOW); | |
delay(DELAY_MS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment