Anda di halaman 1dari 6

KOMUNIKASI LORA DAN LORA GATEWAY DRAGINO

Pada saat transfer di dalam client data yang dikirim berupa data json yang berisi
data:yang terencript(data json) dan panjang data: yang diperlukan untuk proses decript
setelah terkirim data di sisi server bakal di decript setelah itu data yang berupa json
{"panjang":132,"data":"⸮⸮⸮⸮c⸮⸮\"u⸮N⸮]⸮"} akan di ambil yang berupa data akan di
decode menggunakan aes128 menjadi data semula.

{"host":"http://10.130.1.230:8000/api/data/push?","api_key":"c846d8ff5c3bf62db712
ba8c5d5b7da2","v":22,"id":"QSFOIAD94Wdgwudrfwudfw"}

Setelah itu data yang terdecode menjadi json akan diparse menjadi data yang akan di
proses untuk di upload ke server oleh lora gateway.
Source Code pada client
#include <ArduinoJson.h>
#include <RH_RF95.h>
#include <AES.h>
AES aes ;
RH_RF95 rf95;
byte *key = (unsigned char*)"0123456789010123";
float frequency = 868.0;
const int capacity = JSON_OBJECT_SIZE(4);
StaticJsonDocument<capacity>doc;
void setup()
{
Serial.begin(9600); Serial.println("Start LoRa Client");
if (!rf95.init())
Serial.println("init failed");

rf95.setFrequency(frequency);
rf95.setTxPower(13);
rf95.setSpreadingFactor(7);
rf95.setSignalBandwidth(125000);
rf95.setCodingRate4(5);

rf95.setSyncWord(0x34);

void loop()
{
unsigned long long int my_iv = 36753562;
JsonObject obj = doc.to<JsonObject>();
obj["host"] = "http://10.130.1.230:8000/api/data/push?";
obj["api_key"] = "c846d8ff5c3bf62db712ba8c5d5b7da2";
obj["v"] = 22;
obj["id"] = "QSFOIAD94Wdgwudrfwudfw";

String data = F("");


serializeJson(doc, data);
Serial.println(data);

doc.clear();
obj.clear();
int plainLength = data.length(); // don't count the trailing /0 of the string !
int padedLength = plainLength + N_BLOCK - plainLength % N_BLOCK;
byte cipher [padedLength] ;
byte iv [N_BLOCK] ;
aes.iv_inc();
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_encrypt(data.c_str(), plainLength, cipher, key, 128, iv);
obj = doc.to<JsonObject>();
obj["panjang"] = plainLength;
obj["data"] = cipher;
data = "";
serializeJson(doc, data);
Serial.println(data);
rf95.send((uint8_t *)data.c_str(), data.length());

rf95.waitPacketSent();
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);

if (rf95.waitAvailableTimeout(3000))
{
if (rf95.recv(buf, &len))
{
Serial.print("got reply: ");
Serial.println((char*)buf);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
}
else
{
Serial.println("recv failed");
}
}
else
{
Serial.println("No reply, is LoRa server running?");
}
delay(5000);
}
Source Code Pada Server
#include <ArduinoJson.h>

#define BAUDRATE 115200

#include <Console.h>

#include <HttpClient.h>

#include <SPI.h>

#include <RH_RF95.h>

#include <AES.h>

AES aes ;

RH_RF95 rf95;

byte *key = (unsigned char*)"0123456789010123";

unsigned long long int my_iv = 36753562;

int led = A2;

float frequency = 868.0;

const int capacity = JSON_OBJECT_SIZE(4);

StaticJsonDocument<capacity>doc;

void setup()

pinMode(led, OUTPUT);

Bridge.begin(BAUDRATE);

Console.begin();

while (!Console) ; // Wait for console port to be available

Console.println("Start Sketch");

if (!rf95.init())

Console.println("init failed");

rf95.setFrequency(frequency);

rf95.setTxPower(13);
rf95.setSpreadingFactor(7);

rf95.setSignalBandwidth(125000);

// Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8)

rf95.setCodingRate4(5);

Console.print("Listening on frequency: ");

Console.println(frequency);

void loop()

HttpClient client;

if (rf95.available())

uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];

uint8_t len = sizeof(buf);

if (rf95.recv(buf, &len)) {

deserializeJson(doc, buf);

// JsonObject root = doc.as<JsonObject>();

RH_RF95::printBuffer("request: ", buf, len);

Serial.print("got request: ");

Serial.println((char*)buf);

Console.print("RSSI: ");

Console.println(rf95.lastRssi(), DEC);

String url = "";

int panjang = doc["panjang"];

int pad = doc["pad"];

byte iv [N_BLOCK] ;

aes.set_IV(my_iv);

aes.get_IV(iv);

char * cipher = doc["data"];

byte check [pad] ;

aes.do_aes_decrypt(cipher, pad, check, key, 128, iv);

Console.println((char*)buf);

printf("\n\nPLAIN :");
aes.printArray(cipher, (bool)true);

Console.println(panjang);

client.get(url);

if (client.available()) {

String result = client.readString();

Console.println(result);

LAMPIRAN

Baris Pertama adalah data yang belum terencode aes128 baris kedua adalah baris yang
sudah terencode aes128 yang akan dikirimkan melalui lora ke lora gateway dan akan
di proses sehingga dapat di masukan ke server IOT.