Begin to split the sketch

This commit is contained in:
Nico Schottelius 2016-10-22 18:53:29 +02:00
parent e26e4d49ca
commit b7c7c97eab
3 changed files with 166 additions and 100 deletions

View file

@ -0,0 +1,2 @@
void blink(int length);

View file

@ -0,0 +1,57 @@
#include <Arduino.h>
void BLUE() {
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() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
void blink(int length) {
switch(deviceNo) {
case 1:
BLUE();
break;
case 2:
WHITE();
break;
case 3:
GREEN();
break;
case 4:
RED();
break;
}
delay(length);
CLEAR();
}

View file

@ -1,13 +1,19 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h>
#include <Sodaq_UBlox_GPS.h> #include <Sodaq_UBlox_GPS.h>
/* sunlight sensor */
#include "SI114X.h"
#include "ledcolours.h"
#include "Sodaq_RN2483.h" #include "Sodaq_RN2483.h"
#define debugSerial SerialUSB #define debugSerial SerialUSB
#define loraSerial Serial1 #define loraSerial Serial1
/* The number of the device: 1,2,3,4 */ /* The number of the device: 1,2,3,4 */
#define deviceNo 1 #define deviceNo 2
#define beePin ENABLE_PIN_IO #define beePin ENABLE_PIN_IO
@ -26,61 +32,6 @@ int readLoudness()
} }
void BLUE() {
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() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
void blink(int length) {
switch(deviceNo) {
case 1:
BLUE();
break;
case 2:
WHITE();
break;
case 3:
GREEN();
break;
case 4:
RED();
break;
}
delay(length);
CLEAR();
}
#define ADC_AREF 3.3f #define ADC_AREF 3.3f
#define BATVOLT_R1 2.0f #define BATVOLT_R1 2.0f
@ -107,23 +58,19 @@ void setupLED() {
pinMode(LED_BLUE, OUTPUT); pinMode(LED_BLUE, OUTPUT);
} }
void setupGPS() void setup_gps() { sodaq_gps.init(GPS_ENABLE); }
{
sodaq_gps.init(GPS_ENABLE);
}
/*! /*!
* Find a GPS fix, but first wait a while * Find a GPS fix, but first wait a while
*/ */
boolean get_gps(long waittime) boolean get_gps(long waittime)
{ {
/* Delay and light up */
blink(10000);
uint32_t start = millis();
uint32_t timeout = waittime * 1000; uint32_t timeout = waittime * 1000;
debugSerial.println(String("waiting for fix ..., timeout=") + timeout + String("ms")); debugSerial.println(String("waiting for fix ..., timeout=") + timeout + String("ms"));
blink(5000);
uint32_t start = millis();
if (sodaq_gps.scan(false, timeout)) { if (sodaq_gps.scan(false, timeout)) {
debugSerial.println(String(" time to find fix: ") + (millis() - start) + String("ms")); debugSerial.println(String(" time to find fix: ") + (millis() - start) + String("ms"));
@ -147,28 +94,54 @@ boolean get_gps(long waittime)
debugSerial.println(msg_gps); debugSerial.println(msg_gps);
} else { } else {
debugSerial.println("GPS: No Fix"); debugSerial.println("GPS: No Fix");
return false;
} }
return true;
return msg_gps;
} }
uint8_t DevEUI[8] = { 0x9c, 0xd9, 0x0b, 0xb5, 0x2b, 0x6a, 0x1d, deviceNo };
// OTAA // OTAA
// Random numbers chosen + device id // Random numbers chosen + device id
uint8_t DevEUI[8] = { 0x9c, 0xd9, 0x0b, 0xb5, 0x2b, 0x6a, 0x1d, deviceNo }; // 9cd90bb52b6a1d
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 }; // Swisscom
// 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 };
// ttn // ttn
// uint8_t AppEUI[8] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x0E, 0x9B }; uint8_t AppEUI[8] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x0E, 0x9B };
// ttn id=1 // ttn id=1
// uint8_t AppKey[16] = { 0x27, 0x29, 0xCC, 0x46, 0xAD, 0xFA, 0xFA, 0x95, 0xA1, 0x8A, 0xA1, 0x21, 0x57, 0x21, 0x23, 0x6E }; // uint8_t AppKey[16] = { 0x27, 0x29, 0xCC, 0x46, 0xAD, 0xFA, 0xFA, 0x95, 0xA1, 0x8A, 0xA1, 0x21, 0x57, 0x21, 0x23, 0x6E };
// ttn id=2 // ttn id=2
// uint8_t AppKey[16] = { 0xE4, 0xAC, 0x4D, 0xD4, 0xB2, 0xA7, 0x84,0xB8, 0x9D, 0xC9, 0x4C, 0x37, 0xD9, 0x0E, 0x22, 0x45 }; uint8_t AppKey[16] = { 0xE4, 0xAC, 0x4D, 0xD4, 0xB2, 0xA7, 0x84,0xB8, 0x9D, 0xC9, 0x4C, 0x37, 0xD9, 0x0E, 0x22, 0x45 };
// loriot
uint8_t devAddr[4] = { 0x00, 0xC5, 0x76, 0x8A };
uint8_t appSKey[16] = { 0x70, 0x79, 0x59, 0x92, 0x37, 0xAE, 0x59, 0x41, 0x4C, 0xD5, 0x2C, 0x60, 0xF1, 0x4B, 0x8D, 0x61 };
// 0x1B, 0xFC, 0xE9, 0x18, 0x6C, 0x89, 0xB8, 0x60, 0xD3, 0xF6, 0x73, 0x0D, 0xE3, 0x2F, 0x1C, 0xA3 };
uint8_t nwkSKey[16] = { 0xA0, 0x6B, 0xEB, 0xE1, 0x30, 0xC4, 0xDA, 0x3F, 0x50, 0xD7, 0xFA, 0xB4, 0x73, 0x03, 0xDA, 0x3E };
// loriot2
/* uint8_t devAddr[4] = { 0x00, 0x88, 0x21, 0x30 }; */
/* uint8_t appSKey[16] = { 0x96, 0xEC, 0xAB, 0xBE, 0x1F, 0x2B, 0x99, 0x3C, 0x54, 0x4B, 0x9E, 0xE9, 0xA9, 0x4A, 0x23, 0x2D }; */
/* uint8_t nwkSKey[16] = { 0x1D, 0xAE, 0xC3, 0x27, 0x98, 0x56, 0x2A, 0xB1, 0xBF, 0x42, 0x94, 0xD3, 0xCA, 0x04, 0x2C, 0x56 }; */
void setupLoRaABP(){
if (LoRaBee.initABP(loraSerial, devAddr, appSKey, nwkSKey, true))
{
debugSerial.println("Communication to LoRaBEE successful.");
}
else
{
debugSerial.println("Communication to LoRaBEE failed!");
}
}
void setupLoRaOTAA(){ void setupLoRaOTAA(){
if (LoRaBee.initOTA(loraSerial, DevEUI, AppEUI, AppKey, true)) if (LoRaBee.initOTA(loraSerial, DevEUI, AppEUI, AppKey, true))
@ -181,34 +154,22 @@ void setupLoRaOTAA(){
} }
} }
SI114X SI1145 = SI114X();
void setup() { void setupSunlight() {
pinMode(ENABLE_PIN_IO, OUTPUT); // ONE
digitalWrite(beePin, HIGH); // Lorawan
delay(3000);
while ((!SerialUSB) && (millis() < 10000)){ while (!SI1145.Begin()) {
// Wait 10 seconds for the Serial Monitor debugSerial.println("Si1145 is not ready!");
delay(1000);
} }
debugSerial.println("Si1145 is ready!");
//Set baud rate
// debugSerial.begin(57600);
loraSerial.begin(LoRaBee.getDefaultBaudRate());
// setupGPS();
blink(30);
blink(30);
setupLED();
blink(60);
//connect to the LoRa Network
setupLoRa();
} }
void setupLoRa() void setupLoRa()
{ {
setupLoRaOTAA(); // setupLoRaOTAA();
setupLoRaABP();
} }
void sendPacket(String packet){ void sendPacket(String packet){
@ -268,17 +229,49 @@ void signal_loop_end()
blink(1000); delay(29000); blink(1000); delay(29000);
} }
void sendLight()
{
String msg_light;
msg_light = String("vis=") + SI1145.ReadVisible() + String(" ir=") + SI1145.ReadIR() + String(" uv=") + ((float)SI1145.ReadUV()/100);
sendPacket(msg_light);
debugSerial.println(msg_light);
}
void setup() {
pinMode(ENABLE_PIN_IO, OUTPUT); // ONE
digitalWrite(beePin, HIGH); // Lorawan
delay(3000);
while ((!SerialUSB) && (millis() < 10000)){
// Wait 10 seconds for the Serial Monitor
}
loraSerial.begin(LoRaBee.getDefaultBaudRate());
setup_gps();
setupLED();
blink(60);
setupSunlight();
//connect to the LoRa Network
setupLoRa();
}
int mybyte;
int i;
void loop() { void loop() {
signal_loop_start(); signal_loop_start();
String msg_id = String("node=") + String(deviceNo);
String msg_id = String("Node ") + String(deviceNo);
debugSerial.println(msg_id);
sendPacket(msg_id); sendPacket(msg_id);
sendBatVoltage(); sendBatVoltage();
sendLight();
loudness = readLoudness(); loudness = readLoudness();
String msg_loudness = "loudness=" + String(loudness); String msg_loudness = "loudness=" + String(loudness);
@ -286,9 +279,23 @@ void loop() {
sendPacket(msg_loudness); sendPacket(msg_loudness);
if(get_gps(60)) { /* if(get_gps(60)) { */
sendPacket(msg_gps); /* sendPacket(msg_gps); */
} /* } */
signal_loop_end(); signal_loop_end();
} }
/* Node = 2
48
48
48
52
65
51
48
66
hex: 3030303441333042
*/