Last active
January 17, 2020 08:31
-
-
Save choonewza/fbe7ee8d7b55ef54afb7fbaf67a96218 to your computer and use it in GitHub Desktop.
Content of CAT LoRa Lab 5
This file contains 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
#include "CatLoRaS76S.h" | |
#define _DEBUG 1 | |
#define _PIN_RX 11 | |
#define _PIN_TX 10 | |
#define _LED_BUIDIN 13 | |
#define _MAC_SIP_RESET "sip reset" | |
#define _MAC_SET_CH_FREQ "mac set_ch_freq" | |
#define _MAC_SET_RX2 "mac set_rx2" | |
#define _MAC_SAVE "mac save" | |
#define _MAC_SET_CLASS "mac set_class" | |
#define _MAC_SET_DEVEUI "mac set_deveui" | |
#define _MAC_SET_DEVADDR "mac set_devaddr" | |
#define _MAC_SET_NWKSKEY "mac set_nwkskey" | |
#define _MAC_SET_APPSKEY "mac set_appskey" | |
#define _MAC_JOIN_ABP "mac join abp" | |
#define _MAC_TX_UCNF "mac tx ucnf" | |
#define _MAC_SET_APPEUI "mac set_appeui" | |
#define _MAC_SET_APPKEY "mac set_appkey" | |
#define _MAC_JOIN_OTAA "mac join otaa" | |
Uart Serial2(&sercom1, _PIN_RX, _PIN_TX, SERCOM_RX_PAD_0, UART_TX_PAD_2); | |
CatLoRaS76S::~CatLoRaS76S() {} | |
CatLoRaS76S::CatLoRaS76S() | |
{ | |
pinMode(_LED_BUIDIN, OUTPUT); | |
} | |
void SERCOM1_Handler(void) | |
{ | |
Serial2.IrqHandler(); | |
} | |
void CatLoRaS76S::macCommand(String comm) | |
{ | |
Serial2.print(comm); | |
delay(250); | |
String msg = Serial2.readString(); | |
#ifdef _DEBUG | |
Serial.print("_DEBUG: "); | |
Serial.print(comm); | |
Serial.println(msg); | |
#endif | |
delay(100); | |
} | |
void CatLoRaS76S::begin(uint32_t bundRate) | |
{ | |
Wire.begin(); | |
Serial2.begin(bundRate); | |
pinPeripheral(_PIN_TX, PIO_SERCOM); | |
pinPeripheral(_PIN_RX, PIO_SERCOM); | |
delay(500); | |
freqLoraConfig(); | |
smeHumidity.begin(); | |
} | |
void CatLoRaS76S::freqLoraConfig(void) | |
{ | |
int freq[] = {923200000, 923400000, 922000000, 922200000, 922400000, 922600000, 922800000, 923000000}; | |
macCommand(String(_MAC_SIP_RESET)); | |
delay(1000); | |
//chanel 0 to 7 | |
for (int i = 0; i < sizeof freq / sizeof freq[0]; i++) | |
{ | |
macCommand(String(_MAC_SET_CH_FREQ) + " " + i + " " + freq[i]); | |
} | |
macCommand(String(_MAC_SET_RX2) + " 2 923200000"); | |
macCommand(String(_MAC_SAVE)); | |
} | |
void CatLoRaS76S::joinABP(String loraClass, String devEui, String devAddr, String nwksKey, String appsKey) | |
{ | |
macCommand(String(_MAC_SET_CLASS) + " " + loraClass); | |
macCommand(String(_MAC_SET_DEVEUI) + " " + devEui); | |
macCommand(String(_MAC_SET_DEVADDR) + " " + devAddr); | |
macCommand(String(_MAC_SET_NWKSKEY) + " " + nwksKey); | |
macCommand(String(_MAC_SET_APPSKEY) + " " + appsKey); | |
macCommand(String(_MAC_JOIN_ABP)); | |
macCommand(String(_MAC_SAVE)); | |
} | |
void CatLoRaS76S::joinOTAA(String loraClass, String devEui, String appEui, String appKey) | |
{ | |
macCommand(String(_MAC_SET_CLASS) + " " + loraClass); | |
macCommand(String(_MAC_SET_DEVEUI) + " " + devEui); | |
macCommand(String(_MAC_SET_APPEUI) + " " + appEui); | |
macCommand(String(_MAC_SET_APPKEY) + " " + appKey); | |
macCommand(String(_MAC_JOIN_OTAA)); | |
macCommand(String(_MAC_SAVE)); | |
} | |
void CatLoRaS76S::transmit(uint8_t port, String payLoad) | |
{ | |
String comm = String(_MAC_TX_UCNF) + String(" ") + String(port) + String(" ") + payLoad; | |
Serial2.print(comm); | |
#ifdef _DEBUG | |
Serial.print("_DEBUG_TX: "); | |
Serial.println(comm); | |
#endif | |
delay(250); | |
} | |
String CatLoRaS76S::receive(void) | |
{ | |
String msg = Serial2.readString(); | |
if (msg == "") | |
{ | |
return ""; | |
} | |
#ifdef _DEBUG | |
Serial.print("_DEBUG_RX: "); | |
Serial.println(msg); | |
#endif | |
String rawData = ""; | |
String checkDL = ">> mac rx "; | |
int checkStart = msg.indexOf(checkDL); | |
if (checkStart != -1) | |
{ | |
int checkEND = msg.indexOf("\n", checkStart + 1); | |
rawData = msg.substring(checkStart + 10, checkEND); | |
} | |
return rawData; | |
} | |
unsigned int CatLoRaS76S::getPortReceive(String dataFromReceive) | |
{ | |
String macComm = ""; | |
if (dataFromReceive != "") | |
{ | |
int checkStart = dataFromReceive.indexOf(' '); | |
if (checkStart > -1) | |
{ | |
macComm = dataFromReceive.substring(0, checkStart); | |
} | |
} | |
return (unsigned int)macComm.toInt(); | |
} | |
String CatLoRaS76S::getPayloadReceive(String dataFromReceive) | |
{ | |
String macComm = ""; | |
if (dataFromReceive != "") | |
{ | |
int checkStart = dataFromReceive.indexOf(' '); | |
if (checkStart > -1) | |
{ | |
int checkEND = dataFromReceive.indexOf("\n", checkStart + 1); | |
macComm = dataFromReceive.substring(checkStart + 1, checkEND); | |
} | |
} | |
return macComm; | |
} | |
double CatLoRaS76S::getTemp() | |
{ | |
return smeHumidity.readTemperature(); | |
} | |
double CatLoRaS76S::getHumi() | |
{ | |
return smeHumidity.readHumidity(); | |
} | |
String CatLoRaS76S::getTempHumiCayenneLPPformat() | |
{ | |
String temp = ""; | |
String humi = ""; | |
String cayenneLPPformat = ""; | |
String cayenTemp = "0067"; //3digi (2byte) *0.1(10) | |
String cayenHumi = "0168"; //2digi (1byte) *0.5(2) | |
temp = uint16_t(this->getTemp() * 10); | |
humi = uint16_t(this->getHumi() * 2); | |
char tempHex[5]; | |
char humiHex[3]; | |
sprintf(tempHex, "%04x", temp.toInt()); | |
sprintf(humiHex, "%02x", humi.toInt()); | |
cayenneLPPformat = cayenTemp + tempHex + cayenHumi + humiHex; | |
return cayenneLPPformat; | |
} |
This file contains 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
#ifndef CatLoRaS76S_h | |
#define CatLoRaS76S_h | |
#include "Arduino.h" | |
#include <Wire.h> | |
#include <HTS221.h> | |
#include "wiring_private.h" // pinPeripheral() function | |
class CatLoRaS76S | |
{ | |
private: | |
void SERCOM1_Handler(void); | |
void macCommand(String comm); | |
void freqLoraConfig(void); | |
public: | |
~CatLoRaS76S(); | |
CatLoRaS76S(); | |
void begin(uint32_t bundRate); | |
void joinABP(String loraClass, String devEui, String devAddr, String nwksKey, String appsKey); | |
void joinOTAA(String loraClass, String devEui, String appEui, String appKey); | |
void transmit(uint8_t port, String payLoad); | |
String receive(void); | |
unsigned int getPortReceive(String dataFromReceive); | |
String getPayloadReceive(String dataFromReceive); | |
double getTemp(); | |
double getHumi(); | |
String getTempHumiCayenneLPPformat(); | |
}; | |
#endif |
This file contains 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
#include "CatLoRaS76S.h" | |
#include "LedModule.h" | |
#define RED_LED_PIN 13 | |
#define GREEN_LED_PIN 12 | |
#define LORA_ABP_CLASS "C" | |
#define LORA_DEV_EUI "????????????????" | |
#define LORA_DEV_ADDR "????????" | |
#define LORA_NWKS_KEY "????????????????????????????????" | |
#define LORA_APPS_KEY "????????????????????????????????" | |
LedModule redLed(RED_LED_PIN); | |
LedModule greenLed(GREEN_LED_PIN); | |
CatLoRaS76S lora; | |
int sampleRate = 1024; | |
void setup() { | |
Serial.begin(115200); | |
delay(2000); | |
redLed.begin(); | |
greenLed.begin(); | |
tcStartTimer(sampleRate); | |
Serial.println("-> Lora Setting..."); | |
lora.begin(115200); | |
Serial.println("-> Lora ABP Join..."); | |
lora.joinABP(String(LORA_ABP_CLASS), | |
String(LORA_DEV_EUI), | |
String(LORA_DEV_ADDR), | |
String(LORA_NWKS_KEY), | |
String(LORA_APPS_KEY)); | |
tcStopTimer(); | |
greenLed.on(); | |
Serial.println("->Ready Go."); | |
} | |
void loop() { | |
} | |
void TC5_Handler (void) { | |
//----this code will run every second---- | |
redLed.toggle(); | |
//-------------------------------------- | |
TC5->COUNT16.INTFLAG.bit.MC0 = 1; //Writing a 1 to INTFLAG.bit.MC0 clears the interrupt so that it will run again | |
} | |
void tcSetTimerFrequency(int sampleRate) | |
{ | |
//set TC5 timer counter based off of the system clock and the user defined sample rate or waveform | |
TC5->COUNT16.CC[0].reg = (uint16_t) (SystemCoreClock / sampleRate - 1); | |
while (tcIsSyncing()); | |
} | |
//Function that is used to check if TC5 is done syncing | |
//returns true when it is done syncing | |
bool tcIsSyncing() | |
{ | |
return TC5->COUNT16.STATUS.reg & TC_STATUS_SYNCBUSY; | |
} | |
//This function enables TC5 and waits for it to be ready | |
void tcStartTimer(int sampleRate) | |
{ | |
// Enable GCLK for TCC2 and TC5 (timer counter input clock) | |
GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TC4_TC5)) ; | |
while (GCLK->STATUS.bit.SYNCBUSY); | |
tcReset(); //reset TC5 | |
// Set Timer counter Mode to 16 bits | |
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16; | |
while (TC5->COUNT16.STATUS.bit.SYNCBUSY == 1); // wait for sync | |
// Set TC5 mode as match frequency | |
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ; | |
while (TC5->COUNT16.STATUS.bit.SYNCBUSY == 1); // wait for sync | |
//set prescaler and enable TC5 | |
//you can use different prescaler divisons here like TC_CTRLA_PRESCALER_DIV1 to get different ranges of frequencies | |
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1024 | TC_CTRLA_ENABLE; | |
while (TC5->COUNT16.STATUS.bit.SYNCBUSY == 1); // wait for sync | |
tcSetTimerFrequency(sampleRate); | |
// Configure interrupt request | |
NVIC_DisableIRQ(TC5_IRQn); | |
NVIC_ClearPendingIRQ(TC5_IRQn); | |
NVIC_SetPriority(TC5_IRQn, 0); | |
NVIC_EnableIRQ(TC5_IRQn); | |
// Enable the TC5 interrupt request | |
TC5->COUNT16.INTENSET.bit.MC0 = 1; | |
while (tcIsSyncing()); //wait until TC5 is done syncing | |
TC5->COUNT16.CTRLA.reg |= TC_CTRLA_ENABLE; //set the CTRLA register | |
while (tcIsSyncing()); //wait until snyc'd | |
} | |
//Reset TC5 | |
void tcReset() | |
{ | |
TC5->COUNT16.CTRLA.reg = TC_CTRLA_SWRST; | |
while (tcIsSyncing()); | |
while (TC5->COUNT16.CTRLA.bit.SWRST); | |
} | |
//disable TC5 | |
void tcStopTimer() | |
{ | |
TC5->COUNT16.CTRLA.reg &= ~TC_CTRLA_ENABLE; | |
while (tcIsSyncing()); | |
tcReset(); | |
} |
This file contains 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
#include "LedModule.h" | |
LedModule::~LedModule() | |
{ | |
Serial.print(F("Debug: LedModule[")); | |
Serial.print(getName()); | |
Serial.println(F("] is destroyed.")); | |
} | |
LedModule::LedModule() {} | |
LedModule::LedModule(const char *name) | |
{ | |
this->name = String(name); | |
} | |
LedModule::LedModule(uint8_t pin) | |
{ | |
this->pin = pin; | |
} | |
LedModule::LedModule(const char *name, uint8_t pin) | |
{ | |
this->name = String(name); | |
this->pin = pin; | |
} | |
void LedModule::begin() | |
{ | |
pinMode(this->pin, OUTPUT); | |
off(); //Turn-off LedModule | |
delay(100); | |
} | |
void LedModule::begin(uint8_t pin) | |
{ | |
this->pin = pin; | |
begin(); | |
} | |
uint8_t LedModule::getState() | |
{ | |
return this->state; | |
} | |
uint8_t LedModule::setState(uint8_t state) | |
{ | |
state == HIGH ? on() : off(); | |
} | |
uint8_t LedModule::getPinout() | |
{ | |
return this->pin; | |
} | |
String LedModule::getName() | |
{ | |
return this->name; | |
} | |
void LedModule::setName(const char *name) | |
{ | |
this->name = String(name); | |
} | |
void LedModule::on() | |
{ | |
if (this->state == LOW) | |
{ | |
this->state = HIGH; | |
digitalWrite(this->pin, this->state); | |
} | |
} | |
void LedModule::off() | |
{ | |
this->state = LOW; | |
digitalWrite(this->pin, this->state); | |
} | |
void LedModule::toggle() | |
{ | |
this->state == HIGH ? off() : on(); | |
} | |
bool LedModule::equal(uint8_t pin) | |
{ | |
return this->pin == pin; | |
} |
This file contains 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
#ifndef LedModule_h | |
#define LedModule_h | |
#include "Arduino.h" | |
class LedModule | |
{ | |
protected: | |
String name; | |
uint8_t pin; | |
uint8_t state = LOW; | |
public: | |
virtual ~LedModule(); | |
LedModule(); | |
LedModule(const char *name); | |
LedModule(uint8_t pin); | |
LedModule(const char *name, uint8_t pin); | |
void begin(); | |
void begin(uint8_t pin); | |
uint8_t getState(); | |
uint8_t setState(uint8_t state); | |
uint8_t getPinout(); | |
String getName(); | |
void setName(const char *name); | |
virtual void on(); | |
virtual void off(); | |
virtual void toggle(); | |
bool equal(uint8_t pin); | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment