# include <base64.hpp>
//char p[105] = "011fbfd05e30cd0f0800d4879e41865c1b42470d7283b8201608fec181981dd007f3919460218247b631784c1c9e87b8e17600";
//define variable for storing 23 hourly volumes
unsigned int dV[23];
//define input base64 coded string
const unsigned char* p_base64 = "AR+/0F4wzQ8IANSHnkGGXBtCRw1yg7ggFgj+wYGYHdAH85GUYCGCR7YxeEwcnoe44XYA";
//define decoded hexadecimal payload string
char p[105] ="";
void setup() {
// start serial port at 115200 bps:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB only
}
// decode base64 string to binary character using Base64 library
char p_decoded[105];
int p_decodedLength = decode_base64(p_base64, p_decoded);
// convert binary character to hex character string
String p_str = "";
for(int i = 0; i < p_decodedLength; i++) {
byte p_bin = p_decoded[i];
if(p_bin < 0x10) {
p_str += '0';
}
p_str += String(p_bin, HEX);
}
p_str.toCharArray(p, p_str.length()+1);
Serial.println(p);
}
void loop() {
// extract day from payload and convert to big endian
char s[9];
int hex1 = 8;
for (int i = 0; i <= 6; i+=2){
s[i] = p[hex1-i];
s[i+1] = p[hex1+1-i];
}
// convert hex data to unsigned long integar
unsigned long time01 = strtoul(s, NULL, 16);
// dag = day(time01);
Serial.println(time01);
// extract volume data from payload and convert to big endian
hex1 = 18;
for (int i = 0; i <= 6; i+=2){
s[i] = p[hex1-i];
s[i+1] = p[hex1+1-i];
}
// convert hex data to unsigned long integar
unsigned long volume01 = strtoul(s, NULL,16);
Serial.println(volume01);
// extract first 4 delta volume data from payload and convert to big endian
char s1[15] ="";
hex1 = 32;
for (int i = 0; i <= 12; i+=2){
s1[i] = p[hex1-i];
s1[i+1] = p[hex1+1-i];
}
Serial.println(s1);
// convert hex data to 4 delta values
uint64_t x1 = charToNum(s1);
for (int i = 0; i <= 3; i++){
dV[i] = x1 & 0x3fff;
x1 = x1 >> 14;
}
// extract second 4 delta volume data from payload and convert to big endian
hex1 = 46;
for (int i = 0; i <= 12; i+=2){
s1[i] = p[hex1-i];
s1[i+1] = p[hex1+1-i];
}
Serial.println(s1);
// convert hex data to 4 delta values
x1 = charToNum(s1);
for (int i = 4; i <= 7; i++){
dV[i] = x1 & 0x3fff;
x1 = x1 >> 14;
}
// extract third 4 delta volume data from payload and convert to big endian
hex1 = 60;
for (int i = 0; i <= 12; i+=2){
s1[i] = p[hex1-i];
s1[i+1] = p[hex1+1-i];
}
// convert hex data to 4 delta values
x1 = charToNum(s1);
for (int i = 8; i <= 11; i++){
dV[i] = x1 & 0x3fff;
x1 = x1 >> 14;
}
// extract fourth 4 delta volume data from payload and convert to big endian
hex1 = 74;
for (int i = 0; i <= 12; i+=2){
s1[i] = p[hex1-i];
s1[i+1] = p[hex1+1-i];
}
// convert hex data to 4 delta values
x1 = charToNum(s1);
for (int i = 12; i <= 15; i++){
dV[i] = x1 & 0x3fff;
x1 = x1 >> 14;
}
// extract fifth 4 delta volume data from payload and convert to big endian
hex1 = 88;
for (int i = 0; i <= 12; i+=2){
s1[i] = p[hex1-i];
s1[i+1] = p[hex1+1-i];
}
Serial.println(s1);
// convert hex data to 4 delta values
x1 = charToNum(s1);
for (int i = 16; i <= 19; i++){
dV[i] = x1 & 0x3fff;
x1 = x1 >> 14;
}
// extract sixt 3 delta volume data from payload and convert to big endian
hex1 = 100;
for (int i = 0; i <= 10; i+=2){
s1[i+2] = p[hex1-i];
s1[i+1+2] = p[hex1+1-i];
}
Serial.println(s1);
// convert hex data to 3 delta values
x1 = charToNum(s1);
for (int i = 20; i <= 22; i++){
dV[i] = x1 & 0x3fff;
x1 = x1 >> 14;
}
for (int i=0; i <=22; i++){
Serial.println(dV[i]);
}
while(1){}
}
uint64_t charToNum(char cstr[]){
char cstr1[7];
for (int i = 0; i <=5; i++){
cstr1[i] = cstr[i];
}
uint64_t x1 = strtoul(cstr1, NULL, 16);
x1 = x1 << 32;
x1 = x1 & 0x00ffffff00000000;
char cstr2[9];
for (int i = 0; i <=7; i++){
cstr2[i] = cstr[6+i];
}
x1 = x1 + strtoul(cstr2, NULL, 16);
return x1;
}