diff --git a/.gitignore b/.gitignore index 4c1d6ea..9036908 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ Sodaq_UBlox_GPS/ +libraries/ +a.out +/venv/ diff --git a/loriot-receiver.py b/loriot-receiver.py new file mode 100644 index 0000000..399bb01 --- /dev/null +++ b/loriot-receiver.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import ssl +import websocket +import json +import os +import psycopg2 + +class Loriot(): + def __init__(self): + ws = websocket.WebSocket() + + def connect(self): + tokenurl = os.environ['LORIOT_URL'] + self.ws = websocket.create_connection(tokenurl, sslopt={"cert_reqs": ssl.CERT_NONE}) + + + def listen(self): + while True: + result = self.ws.recv() + print(result) + jdata = jsonToDict(result) + eui = devEUI(jdata) + payload = get_payloan(jdata) + + + def jsonToDict(self, data): + return json.loads(data) + + def devEUI(self, data): + return data['EUI'] + + def get_payload(self, ): + return data['EUI'] + + def insert_json(self, provider, data, payload='', deveui=''): + try: + conn = psycopg2.connect("dbname=lorawan") + cursor = conn.cursor() + cursor.execute("insert into packets values (DEFAULT, DEFAULT, %s, %s, %s, %s)", (provider, data, payload, deveui)) + cursor.connection.commit() + conn.close() + except Exception as e: + print("DB Insert failed: %s" % e) + + +if __name__ == '__main__': + l = Loriot() + l.connect() + l.listen() diff --git a/sodaq_one_gps_battery_loudness/.gitignore b/sodaq_one_gps_battery_loudness/.gitignore new file mode 100644 index 0000000..236536f --- /dev/null +++ b/sodaq_one_gps_battery_loudness/.gitignore @@ -0,0 +1 @@ +internal.ino diff --git a/sodaq_one_gps_battery_loudness/battery.ino b/sodaq_one_gps_battery_loudness/battery.ino new file mode 100644 index 0000000..f395699 --- /dev/null +++ b/sodaq_one_gps_battery_loudness/battery.ino @@ -0,0 +1,11 @@ +#define ADC_AREF 3.3f +#define BATVOLT_R1 2.0f +#define BATVOLT_R2 2.0f +#define BATVOLT_PIN BAT_VOLT + +uint16_t getBatteryVoltage() +{ + uint16_t voltage = (uint16_t)((ADC_AREF / 1.023) * (BATVOLT_R1 + BATVOLT_R2) / BATVOLT_R2 * (float)analogRead(BATVOLT_PIN)); + + return voltage; +} diff --git a/sodaq_one_gps_battery_loudness/buzzer.ino b/sodaq_one_gps_battery_loudness/buzzer.ino new file mode 100644 index 0000000..c39519c --- /dev/null +++ b/sodaq_one_gps_battery_loudness/buzzer.ino @@ -0,0 +1,20 @@ +#include + +void setupBuzzer() +{ + pinMode(BUZZER_PIN, OUTPUT); +} + +void buzzerOn() { + digitalWrite(BUZZER_PIN, HIGH); +} + +void buzzerOff() { + digitalWrite(BUZZER_PIN, LOW); +} + +void buzz(int ms) { + buzzerOn(); + delay(ms); + buzzerOff(); +} diff --git a/sodaq_one_gps_battery_loudness/compass.ino b/sodaq_one_gps_battery_loudness/compass.ino new file mode 100644 index 0000000..bfc6761 --- /dev/null +++ b/sodaq_one_gps_battery_loudness/compass.ino @@ -0,0 +1,23 @@ +#include +#include + +LSM303 compass; + +char compassReport[80]; + +void setupCompass() +{ + Wire.begin(); + compass.init(); + compass.enableDefault(); +} + +void readCompass() +{ + compass.read(); + + snprintf(compassReport, sizeof(compassReport), "A: %6d %6d %6d M: %6d %6d %6d", + compass.a.x, compass.a.y, compass.a.z, + compass.m.x, compass.m.y, compass.m.z); + +} diff --git a/sodaq_one_gps_battery_loudness/ledcolours.h b/sodaq_one_gps_battery_loudness/ledcolours.h deleted file mode 100644 index 54b232a..0000000 --- a/sodaq_one_gps_battery_loudness/ledcolours.h +++ /dev/null @@ -1,2 +0,0 @@ - -void blink(int length); diff --git a/sodaq_one_gps_battery_loudness/ledcolours.ino b/sodaq_one_gps_battery_loudness/ledcolours.ino index e6c59df..ceed6b8 100644 --- a/sodaq_one_gps_battery_loudness/ledcolours.ino +++ b/sodaq_one_gps_battery_loudness/ledcolours.ino @@ -36,20 +36,19 @@ void CLEAR() { digitalWrite(LED_BLUE, HIGH); } - void blink(int length) { switch(deviceNo) { case 1: BLUE(); break; case 2: - WHITE(); + RED(); break; case 3: GREEN(); break; case 4: - RED(); + WHITE(); break; } delay(length); diff --git a/sodaq_one_gps_battery_loudness/lora.ino b/sodaq_one_gps_battery_loudness/lora.ino new file mode 100644 index 0000000..fc2f80c --- /dev/null +++ b/sodaq_one_gps_battery_loudness/lora.ino @@ -0,0 +1,90 @@ +#include +#include "Sodaq_RN2483.h" + +#define loraSerial Serial1 +#define beePin ENABLE_PIN_IO + + +#ifdef LORA_ABP +void setupLoRaABP(){ + if (LoRaBee.initABP(loraSerial, devAddr, appSKey, nwkSKey, true)) + { + debugSerial.println("Lora: ABP setup ok"); + } + else + { + debugSerial.println("Lora: ABP setup failed"); + } +} +#endif + +#ifdef LORA_OTAA +void setupLoRaOTAA(){ + if (LoRaBee.initOTA(loraSerial, DevEUI, AppEUI, AppKey, true)) + { + debugSerial.println("Lora: OTAA setup ok"); + } + else + { + debugSerial.println("Lora: OTAA Setup failed!"); + } +} +#endif + + +void loraSetup() +{ + pinMode(ENABLE_PIN_IO, OUTPUT); // ONE + digitalWrite(beePin, HIGH); // Lorawan + loraSerial.begin(LoRaBee.getDefaultBaudRate()); + +#ifdef LORA_OTAA + setupLoRaOTAA(); +#endif + +#ifdef LORA_ABP + setupLoRaABP(); +#endif +} + +void loraSend(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."); + loraSetup(); + 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."); + loraSetup(); + 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."); + loraSetup(); + 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."); + loraSetup(); + 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; + } +} diff --git a/sodaq_one_gps_battery_loudness/nsarduino.h b/sodaq_one_gps_battery_loudness/nsarduino.h new file mode 100644 index 0000000..b5f1a53 --- /dev/null +++ b/sodaq_one_gps_battery_loudness/nsarduino.h @@ -0,0 +1,20 @@ + +/* battery.ino */ +uint16_t getBatteryVoltage(); + +void blink(int length); + +void buzzerOn(); +void buzzerOff(); +void buzz(int ms); + +/* compass.ino */ +extern char compassReport[80]; + +/* lora.ino */ +void setupLoRa(); +void sendPacket(String packet); + +/* sunlight.ino */ +String getSunLight(); +void setupSunLight(); diff --git a/sodaq_one_gps_battery_loudness/sodaq_one_gps_battery_loudness.ino b/sodaq_one_gps_battery_loudness/sodaq_one_gps_battery_loudness.ino index 982787a..427b567 100644 --- a/sodaq_one_gps_battery_loudness/sodaq_one_gps_battery_loudness.ino +++ b/sodaq_one_gps_battery_loudness/sodaq_one_gps_battery_loudness.ino @@ -2,26 +2,17 @@ #include #include -/* sunlight sensor */ -#include "SI114X.h" -#include "ledcolours.h" - -#include "Sodaq_RN2483.h" +#include "nsarduino.h" #define debugSerial SerialUSB -#define loraSerial Serial1 /* The number of the device: 1,2,3,4 */ #define deviceNo 2 -#define beePin ENABLE_PIN_IO - #define LOUDNESS_SENSOR 0 - #define USE_LOUDNESS 1 - -void sendPacket(String packet); +#define BUZZER_PIN 2 String msg_gps; int loudness; @@ -31,27 +22,11 @@ int readLoudness() return analogRead(LOUDNESS_SENSOR); } - - -#define ADC_AREF 3.3f -#define BATVOLT_R1 2.0f -#define BATVOLT_R2 2.0f -#define BATVOLT_PIN BAT_VOLT - -uint16_t getBatteryVoltage() -{ - uint16_t voltage = (uint16_t)((ADC_AREF / 1.023) * (BATVOLT_R1 + BATVOLT_R2) / BATVOLT_R2 * (float)analogRead(BATVOLT_PIN)); - - return voltage; -} - - void sendBatVoltage() { String battery = String("bat1=") + String(getBatteryVoltage()); sendPacket(battery); } - void setupLED() { pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); @@ -99,123 +74,14 @@ boolean get_gps(long waittime) return true; } -uint8_t DevEUI[8] = { 0x9c, 0xd9, 0x0b, 0xb5, 0x2b, 0x6a, 0x1d, deviceNo }; - - -// OTAA -// Random numbers chosen + device id +/* +* 1 Sodaq +* 2 sodaq +* 3 sodaq +* 4 rpi +*/ // 9cd90bb52b6a1d -// 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 -uint8_t AppEUI[8] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x0E, 0x9B }; - -// ttn id=1 -// uint8_t AppKey[16] = { 0x27, 0x29, 0xCC, 0x46, 0xAD, 0xFA, 0xFA, 0x95, 0xA1, 0x8A, 0xA1, 0x21, 0x57, 0x21, 0x23, 0x6E }; - -// ttn id=2 -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(){ - if (LoRaBee.initOTA(loraSerial, DevEUI, AppEUI, AppKey, true)) - { - debugSerial.println("Communication to LoRaBEE successful."); - } - else - { - debugSerial.println("OTAA Setup failed!"); - } -} - -SI114X SI1145 = SI114X(); - -void setupSunlight() { - - while (!SI1145.Begin()) { - debugSerial.println("Si1145 is not ready!"); - delay(1000); - } - debugSerial.println("Si1145 is ready!"); -} - - -void setupLoRa() -{ -// setupLoRaOTAA(); - setupLoRaABP(); -} - -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 signal_loop_start() { blink(30); delay(50); @@ -229,55 +95,58 @@ void signal_loop_end() 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()); + /* Enable the pins 2/3, 6/7 and 8/9 */ + pinMode(11, OUTPUT); + digitalWrite(11, HIGH); + + setupLED(); setup_gps(); - setupLED(); + setupBuzzer(); + setupCompass(); + + blink(60); - setupSunlight(); - //connect to the LoRa Network - setupLoRa(); + setupSunLight(); + + loraSetup(); } - int mybyte; - int i; +String tmp; + void loop() { signal_loop_start(); - String msg_id = String("node=") + String(deviceNo); - sendPacket(msg_id); + /* String msg_id = String("node=") + String(deviceNo); */ + /* sendPacket(msg_id); */ - sendBatVoltage(); - sendLight(); + /* sendBatVoltage(); */ + // sendLight(); - loudness = readLoudness(); - String msg_loudness = "loudness=" + String(loudness); - debugSerial.println(msg_loudness); + /* loudness = readLoudness(); */ + /* String msg_loudness = "loudness=" + String(loudness); */ + /* debugSerial.println(msg_loudness); */ - sendPacket(msg_loudness); + tmp = getSunLight(); + debugSerial.println(tmp); + loraSend(tmp); + + readCompass(); + debugSerial.println(compassReport); + + // buzz(30); + +// sendPacket(msg_loudness); /* if(get_gps(60)) { */ /* sendPacket(msg_gps); */ @@ -299,3 +168,22 @@ void loop() { hex: 3030303441333042 */ + +/* temp / humidity code (not functional) + +/* humid / temperature */ +/* +#include + +HDC1000 hdc; + +float temperature; +float humidity; + + +*/ + +// temperature = hdc.getTemperature(); +// humidity = hdc.getHumidity(); +// String msg_tmphumid = String("tmp=") + temperature + String(" humid=") + humidity; +// debugSerial.println(msg_tmphumid); diff --git a/sodaq_one_gps_battery_loudness/sunlight.ino b/sodaq_one_gps_battery_loudness/sunlight.ino new file mode 100644 index 0000000..b52de14 --- /dev/null +++ b/sodaq_one_gps_battery_loudness/sunlight.ino @@ -0,0 +1,17 @@ +#include +#include "SI114X.h" + +SI114X SI1145 = SI114X(); + +void setupSunLight() { + while (!SI1145.Begin()) { + debugSerial.println("Si1145 is not ready!"); + delay(1000); + } + debugSerial.println("Si1145 is ready!"); +} + +String getSunLight() +{ + return String("vis=") + SI1145.ReadVisible() + String(" ir=") + SI1145.ReadIR() + String(" uv=") + ((float)SI1145.ReadUV()/100); +}