lorawan/send_and_blink/send_and_blink.ino

324 lines
6.5 KiB
Arduino
Raw Normal View History

#include "Sodaq_RN2483.h"
#include "Arduino.h"
#define debugSerial SerialUSB
#define loraSerial Serial1
2016-09-15 07:48:53 +00:00
/* The number of the device: 1,2,3,4 */
2016-09-16 21:42:17 +00:00
#define deviceNo 1
2016-09-15 07:48:53 +00:00
#define beePin ENABLE_PIN_IO
2016-09-15 07:48:53 +00:00
2016-09-16 21:42:17 +00:00
#define LOUDNESS_SENSOR 0
#define LIGHT_SENSOR 2
#define WATER_SENSOR 6
2016-09-16 23:35:51 +00:00
#define BUZZER 8
#define MAGNETIC_SWITCH 12
2016-09-16 21:42:17 +00:00
int loudness;
void BLUE() {
2016-09-15 07:48:53 +00:00
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, LOW);
}
void RED() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
void YELLOW() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
void WHITE() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
}
void GREEN() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
void CLEAR() {
2016-09-15 07:48:53 +00:00
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
2016-09-15 07:48:53 +00:00
void blink(int length) {
switch(deviceNo) {
case 1:
BLUE();
break;
case 2:
RED();
break;
case 3:
GREEN();
break;
case 4:
2016-09-16 15:19:30 +00:00
WHITE();
2016-09-15 07:48:53 +00:00
break;
}
delay(length);
CLEAR();
}
2016-09-16 23:35:51 +00:00
void beep(int howlong) {
2016-09-17 05:14:13 +00:00
buzzOn();
delay(howlong);
buzzOff();
}
void buzzOn() {
2016-09-16 23:35:51 +00:00
digitalWrite(BUZZER, HIGH);
2016-09-17 05:14:13 +00:00
}
void buzzOff() {
2016-09-16 23:35:51 +00:00
digitalWrite(BUZZER, LOW);
}
void setupLED() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
}
2016-09-16 21:42:17 +00:00
// OTAA
2016-09-15 07:48:53 +00:00
// Random numbers chosen + device id
uint8_t DevEUI[8] = { 0x9c, 0xd9, 0x0b, 0xb5, 0x2b, 0x6a, 0x1d, deviceNo };
uint8_t AppEUI[8] = { 0xd4, 0x16, 0xcd, 0x0b, 0x7b, 0xcf, 0x2d, 0x5c };
uint8_t AppKey[16] = { 0xa9, 0xbc, 0x8b, 0x6a, 0x81, 0x75, 0xf6, 0x33,
0xe0, 0xd6, 0x64, 0xd9, 0x2b, 0xcb, 0x13, 0x78 };
2016-09-15 07:48:53 +00:00
uint8_t counter;
void setupLoRaOTAA(){
if (LoRaBee.initOTA(loraSerial, DevEUI, AppEUI, AppKey, true))
{
debugSerial.println("Communication to LoRaBEE successful.");
}
else
{
debugSerial.println("OTAA Setup failed!");
}
}
2016-09-16 21:42:17 +00:00
int readLoudness()
{
return analogRead(LOUDNESS_SENSOR);
}
2016-09-17 04:45:43 +00:00
int readLight()
2016-09-16 21:42:17 +00:00
{
2016-09-17 03:58:49 +00:00
int sensorValue = analogRead(LIGHT_SENSOR);
2016-09-17 04:45:43 +00:00
2016-09-17 05:56:11 +00:00
return sensorValue;
2016-09-17 04:45:43 +00:00
return map(sensorValue, 1000, 200000, 1, 100);
// return (float)(sensorValue)*10/sensorValue;
// return (float)(1023-sensorValue)*10/sensorValue;
2016-09-17 03:58:49 +00:00
// Not a great value
// return analogRead(LIGHT_SENSOR) / 1000;
2016-09-16 21:42:17 +00:00
}
2016-09-16 21:49:20 +00:00
void setupWater() {
pinMode(WATER_SENSOR, INPUT);
}
2016-09-16 21:42:17 +00:00
boolean hasWater()
{
if(digitalRead(WATER_SENSOR) == LOW) {
return true;
} else {
return false;
}
}
2016-09-16 23:35:51 +00:00
void setupBuzzer()
{
pinMode(BUZZER, OUTPUT);
}
void setupMagnet()
{
pinMode(MAGNETIC_SWITCH, INPUT);
}
boolean isMagnetic()
{
if(digitalRead(MAGNETIC_SWITCH) == HIGH)
return true;
else
return false;
}
2016-09-16 21:42:17 +00:00
void setup() {
//Power up the LoRaBEE - on loraone/sodaq one
pinMode(ENABLE_PIN_IO, OUTPUT); // ONE
2016-09-16 14:07:28 +00:00
digitalWrite(beePin, HIGH); // ONE
delay(3000);
2016-09-16 23:35:51 +00:00
/* Enable the pins 2/3, 6/7 and 8/9 */
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
while ((!SerialUSB) && (millis() < 10000)){
// Wait 10 seconds for the Serial Monitor
}
//Set baud rate
debugSerial.begin(57600);
loraSerial.begin(LoRaBee.getDefaultBaudRate());
// Debug output from LoRaBee
// LoRaBee.setDiag(debugSerial); // optional
setupLED();
2016-09-15 07:48:53 +00:00
blink(60);
/* used for blinking */
counter=0;
2016-09-16 21:42:17 +00:00
loudness = 0;
//connect to the LoRa Network
setupLoRa();
2016-09-16 21:42:17 +00:00
setupWater();
2016-09-16 23:35:51 +00:00
setupBuzzer();
setupMagnet();
2016-09-16 21:42:17 +00:00
}
void setupLoRa(){
setupLoRaOTAA();
}
void sendPacket(String packet){
switch (LoRaBee.sendReqAck(1, (uint8_t*)packet.c_str(), packet.length(), 8))
{
case NoError:
debugSerial.println("Successful transmission.");
break;
case NoResponse:
debugSerial.println("There was no response from the device.");
setupLoRa();
break;
case Timeout:
debugSerial.println("Connection timed-out. Check your serial connection to the device! Sleeping for 20sec.");
delay(20000);
break;
case PayloadSizeError:
debugSerial.println("The size of the payload is greater than allowed. Transmission failed!");
break;
case InternalError:
debugSerial.println("Oh No! This shouldn't happen. Something is really wrong! Try restarting the device!\r\nThe network connection will reset.");
setupLoRa();
break;
case Busy:
debugSerial.println("The device is busy. Sleeping for 10 extra seconds.");
delay(10000);
break;
case NetworkFatalError:
debugSerial.println("There is a non-recoverable error with the network connection. You should re-connect.\r\nThe network connection will reset.");
setupLoRa();
break;
case NotConnected:
debugSerial.println("The device is not connected to the network. Please connect to the network before attempting to send data.\r\nThe network connection will reset.");
setupLoRa();
break;
case NoAcknowledgment:
debugSerial.println("There was no acknowledgment sent back!");
// When you this message you are probaly out of range of the network.
break;
default:
break;
}
}
void loop() {
2016-09-17 03:35:39 +00:00
2016-09-17 05:14:13 +00:00
2016-09-17 03:35:39 +00:00
/* Announce begin of code */
blink(20); delay(50);
blink(20); delay(50);
blink(20); delay(50);
2016-09-16 21:42:17 +00:00
loudness = readLoudness();
String data_loudness = String("loudness=" + String(loudness, DEC));
debugSerial.println(data_loudness);
String data_light = String("light=" + String(readLight(), 3));
debugSerial.println(data_light);
String data_water;
if(hasWater()) {
2016-09-17 02:27:46 +00:00
data_water = String("water=1");
2016-09-17 05:14:13 +00:00
buzzOn();
2016-09-16 21:42:17 +00:00
} else {
2016-09-17 02:27:46 +00:00
data_water = String("water=0");
2016-09-17 05:14:13 +00:00
buzzOff();
2016-09-16 21:42:17 +00:00
}
debugSerial.println(data_water);
2016-09-15 07:48:53 +00:00
2016-09-16 23:35:51 +00:00
String data_magnet;
if(isMagnetic()) {
2016-09-17 02:27:46 +00:00
data_magnet = String("magnet=1");
2016-09-16 23:35:51 +00:00
} else {
2016-09-17 02:27:46 +00:00
data_magnet = String("magnet=0");
2016-09-16 23:35:51 +00:00
}
debugSerial.println(data_magnet);
2016-09-17 03:35:39 +00:00
sendPacket(data_loudness);
blink(20); delay(2980);
sendPacket(data_light);
blink(20); delay(2980);
sendPacket(data_water);
blink(20); delay(2980);
sendPacket(data_magnet);
/* Blink long after sending packet
2016-09-16 21:42:17 +00:00
if(counter >= 10) {
2016-09-17 02:27:46 +00:00
// Beep(20);
2016-09-16 21:42:17 +00:00
blink(20);
delay(10);
blink(20);
sendPacket(data_loudness);
blink(500);
sendPacket(data_light);
2016-09-15 07:48:53 +00:00
blink(500);
2016-09-16 21:42:17 +00:00
sendPacket(data_water);
2016-09-16 23:35:51 +00:00
blink(500);
sendPacket(data_magnet);
2016-09-15 07:48:53 +00:00
counter = 0;
} else {
blink(30);
counter++;
}
2016-09-17 03:35:39 +00:00
*/
2016-09-15 07:48:53 +00:00
2016-09-17 03:35:39 +00:00
// delay(1000);
}