TheThingsNetwork / arduino-device-lib

Arduino Library for TTN Devices
MIT License
206 stars 96 forks source link

Lorawan 1.0.3 5.5 End-DeviceStatus(DevStatusReq, DevStatusAns. Battery Percentage #271

Open Cloolalang opened 3 years ago

Cloolalang commented 3 years ago

Hi All, I really like this library, very well documented and easy to get going. I was wondering if I could request a feature:

I would like to check battery and DL margin using DevStatusReq.

would it be difficult to expose this in this library as the Microchip can do it MAC SET BAT.

Cheers P

Cloolalang commented 3 years ago

Oh, Maybe not.

Cloolalang commented 3 years ago

OK I did a quick and nasty: in TheThingsNetwork.cpp file I added:

`void TheThingsNetwork::Setbattery()//patricks very crude DEvstatusReq Batter percentage function { readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VDD, buffer, sizeof(buffer));//fetch volts reading from VDD (I dont know where this comes from!) int val = atoi(buffer);//convert to int debugPrint(SENDING); debugPrintLn(); debugPrint(val);//print to debug debugPrintLn(); //Lorawan DevStatus sends battery level up to the netwotk as a percentage, 1-254 = 1-100% , 0 and 255 are reserved for mains power on or no battery infor available
if (val < 3200){ sendMacSet(MAC_BAT, "0"); } else if (val > 3200 && val < 3300){ sendMacSet(MAC_BAT, "64");; } else if (val > 3300 && val < 3400){ sendMacSet(MAC_BAT, "128");; } else if (val > 3400 && val < 3500){ sendMacSet(MAC_BAT, "250");; } else if (val > 3500){ sendMacSet(MAC_BAT, "255");; }

}`