Skip to content

Instantly share code, notes, and snippets.

@vixtory09678
Created June 4, 2020 04:52
Show Gist options
  • Save vixtory09678/6cc8a2f680272385f107c6a3aaf80da1 to your computer and use it in GitHub Desktop.
Save vixtory09678/6cc8a2f680272385f107c6a3aaf80da1 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <WiMODLoRaWAN.h>
#include <ArduinoJson.h>
#define LOT "20120102"
#define ITEM "1010201010"
#define MC "0224"
#define OP "10"
static TWiMODLORAWAN_TX_Data txData;
WiMODLoRaWAN wimod(Serial2);
#define DEV_ADR 0x0010F7C1 // Device address
#define STR_APPSKEY "EC84AD3B44832EDFF5A8223FB8F406CB"
#define STR_NWKSKEY "EC84AD3B44832EDFF5A8223FB8F406CB"
unsigned char APPSKEY[16];
unsigned char NWKSKEY[16];
UINT32 dev_adr = DEV_ADR;
int counter = 0;
void setHEXStrToByte (const char* strID, uint8_t *byteID) {
if (strlen (strID) % 2 != 0) return;
for (size_t i = 0,j = 0; i < strlen (strID); i += 2, j++) {
String byteHex = String (strID[i]) + String (strID[i+1]);
byteID[j] = strtol(byteHex.c_str(), NULL, 16);
}
}
void onRxData(TWiMODLR_HCIMessage& rxMsg){
TWiMODLORAWAN_RX_Data radioMsg;
bool isConverted = wimod.convert(rxMsg, &radioMsg);
if (!isConverted) return;
for (int i = 0; i < radioMsg.Length; i++ ){
Serial.print((char)radioMsg.Payload[i]);
} Serial.println();
Serial.printf("RSSI: %d\n",radioMsg.RSSI);
}
void loraMessage (uint8_t port, String msg) {
txData.Port = port;
txData.Length = msg.length();
strcpy((char *)txData.Payload, (const char *)msg.c_str());
TWiMODLORAWAN_NwkStatus_Data nwkStatus;
if (!wimod.GetNwkStatus(&nwkStatus)) return;
if (msg.length() > nwkStatus.MaxPayloadSize){
wimod.ReactivateDevice(&dev_adr);
}
Serial.println(wimod.SendCData(&txData) ? "Success" : "Failed");
}
void setup() {
setHEXStrToByte(STR_APPSKEY, APPSKEY);
setHEXStrToByte(STR_NWKSKEY, NWKSKEY);
Serial.begin(115200); // setup for debug
Serial2.begin(115200);
wimod.begin();
delay(100); wimod.Reset(); delay(100);
wimod.DeactivateDevice();
Serial.print(F("Ping WiMOD: "));
if (wimod.Ping() != true) {
Serial.println("FAILED");
while(1);
}
Serial.println("Ok");
TWiMODLORAWAN_ActivateDeviceData activationData;
// set activate data
activationData.DeviceAddress = DEV_ADR;
memcpy(activationData.NwkSKey, NWKSKEY, 16);
memcpy(activationData.AppSKey, APPSKEY, 16);
bool isActivated = wimod.ActivateDevice(activationData);
if (isActivated == false) {
Serial.print("Fail Activate => code : ");
Serial.println((int) wimod.GetLastResponseStatus());
while(1);
}
// Activated section //
wimod.RegisterRxUDataIndicationClient(onRxData);
Serial.println("Activated");
}
unsigned long c_time = 0;
void loop() {
unsigned long now = millis();
if (now - c_time > 60000)
{
Serial.println("Send..");
c_time = now;
counter += random(20,35);
// c_time = now;
StaticJsonDocument<256> docRun;
JsonArray data = docRun.createNestedArray("data");
JsonArray label = docRun.createNestedArray("label");
data.add(counter);
label.add("c");
String jsonRun = "";
serializeJson(docRun, jsonRun);
loraMessage(0x02, jsonRun);
}
delay(100);
wimod.Process();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment