Skip to content

Instantly share code, notes, and snippets.

@gresan-gits
Created April 14, 2020 15:42
Show Gist options
  • Save gresan-gits/55543a847350a450755c2be294517d8e to your computer and use it in GitHub Desktop.
Save gresan-gits/55543a847350a450755c2be294517d8e to your computer and use it in GitHub Desktop.
RAK811 RUI v3.0.0.12.H.T
String prepairData() {
int temperatureAdc = analogRead(SENSOR);
byte adcH = (temperatureAdc & 0xFF00) >> 8;
byte adcL = (temperatureAdc & 0x00FF);
byte inputstate = digitalRead(INP);
DebugSerial.print("<Adc:");
DebugSerial.print(temperatureAdc);
DebugSerial.print(">");
DebugSerial.print("<input:");
DebugSerial.print(inputstate);
DebugSerial.print(">");
DebugSerial.print("<press:");
DebugSerial.print(numPress);
DebugSerial.print(">");
char str[80];
sprintf(str, "%02X%02X%02X%02X", adcH, adcL, inputstate, numPress);
return String(str);
}
void rk_init()
{
ATSerial.setTimeout(2000);
DebugSerial.setTimeout(1000);
}
/*
at+version
Firmware Version: RUI v3.0.0.12.H.T
OK
*/
String rk_getVersion()
{
resend:
sendCommand(F("at+version"));
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
{
return "Error";
}
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
return ret;
}
else
return "Error";
}
void waitJoinNetwork() {
int tick = 0;
while (tick < 10)//duoc timeou+ xuong dong toi da 10 lan
{
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.startsWith("OTAA Join Start")) {
DebugSerial.print("<Network Joinning>");
break;
}
if (ret.length() == 0) {//Xu ly lai ham timeout vi trong ket qua co tra ve empty line
tick++;
//break;//Khong dung duoc do co ham tra ve rong
}
}
DebugSerial.print("<Wait connect status>");
int retry = 0;
ATSerial.setTimeout(15000);
while (retry < 10) {//So lan thu ket noi
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
DebugSerial.print(" . ");
if (ret.startsWith("OK"))
{
DebugSerial.print("<Network Auto Joined>");
networkConnected = true;
break;
}
else if (ret.startsWith("ERROR"))//Joint fail
{
networkConnected = false;
DebugSerial.print("<Network Failer Joined>");
break;
}
}
else {//Time out wait response
break;
}
retry++;
}
ATSerial.setTimeout(2000);
}
bool rk_deviceRestart() {
resend:
sendCommand(F("at+set_config=device:restart"));
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
waitJoinNetwork();
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:work_mode:0
LoRa configure LoRaWAN success
OK
Reset now...
*/
//Set the work mode for LoRa.
bool rk_setWorkingMode(int mode)
{
resend:
switch (mode)
{
case 0:
sendCommand(F("at+set_config=lora:work_mode:0")); //Set LoRaWAN Mode.
break;
case 1:
sendCommand(F("at+set_config=lora:work_mode:1")); //Set LoRaP2P Mode.
break;
default:
return false;
}
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
waitJoinNetwork();
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:join_mode:0
LoRa configure OTAA success
OK
//Neu da ket noi roi se thuc hien reconnect
at+set_config=lora:join_mode:0
LoRa configure OTAA success
OK
OTAA Join Start...
[LoRa]:Join Success
OK
*/
//Set the join mode for LoRaWAN. 0: OTAA, 1: ABP
bool rk_joinLoRaNetwork(int mode) {
resend:
switch (mode)
{
case 0:
sendCommand(F("at+set_config=lora:join_mode:0")); //join Network through OTAA mode.
break;
case 1:
sendCommand(F("at+set_config=lora:join_mode:1")); //join Network through ABP mode.
break;
default:
return false;
}
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
waitJoinNetwork();
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:class:0
LoRa configure ClassA success
OK
*/
//Set the class for LoRa. 0: Class A, 1: Class B, 2: ClassC
bool rk_setLoRaClass(int mode) {
resend:
switch (mode)
{
case 0:
sendCommand(F("at+set_config=lora:class:0"));
break;
case 1:
sendCommand(F("at+set_config=lora:class:1"));
break;
case 2:
sendCommand(F("at+set_config=lora:class:2"));
break;
default:
return false;
}
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:region:EU868
Selected LoRaWAN 1.0.2 Region: EU868
Band switch success
OK
*/
//Set the region for LoRa. region: EU868 EU433, CN470, IN865, EU868, AU915, US915, KR920, AS923.
bool rk_setRegion(String region) {
resend:
String cmd = "at+set_config=lora:region:";
cmd += region;
sendCommand(cmd);
// sendCommand(F("at+set_config=lora:region:EU868"));
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:dev_eui:00CE8AE400F2DFD3
LoRa dev_eui configure success
OK
*/
//Set the device EUI for OTAA.
bool rk_setOtaaDeviceEui(String eui) {
resend:
String cmd = "at+set_config=lora:dev_eui:";
cmd += eui;
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:app_eui:70B3D57ED0027286
LoRa app_eui configure success
OK
*/
//Set the device EUI for OTAA.
bool rk_setOtaaAppEui(String eui) {
resend:
String cmd = "at+set_config=lora:app_eui:";
cmd += eui;
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:app_key:FC4C72D5333D7DFB530E24F43B024FDA
LoRa app_key configure success
OK
*/
//Set the application key for OTAA
bool rk_setOtaaAppKey(String key) {
resend:
String cmd = "at+set_config=lora:app_key:";
cmd += key;
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:dev_addr:2601173B
LoRa dev_addr configure success
OK
*/
//Set the device address for ABP
bool rk_setAbpDeviceAdd(String address) {
resend:
String cmd = "at+set_config=lora:dev_addr:";
cmd += address;
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+set_config=lora:nwks_key:AA11A1044D5119B1AA5AA1CF8265FFDE
LoRa nwks_key configure success
OK
*/
//Set the network session key for ABP
bool rk_setAbpNwks(String key) {
resend:
String cmd = "at+set_config=lora:nwks_key:";
cmd += key;
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+join
OTAA:
DevEui:00CE8AE400F2DFD3
AppEui:70B3D57ED0027286
AppKey:FC4C72D5333D7DFB530E24F43B024FDA
OTAA Join Start...
[LoRa]:Join retry Cnt:1
[LoRa]:Join retry Cnt:2
[LoRa]:Join retry Cnt:3
[LoRa]:Join Success
OK
//
at+join
OTAA:
DevEui:00CE8AE400F2DFD3
AppEui:70B3D57ED0027286
AppKey:FC4C72D5333D7DFB530E24F43B024FDA
OTAA Join Start...
[LoRa]:Join retry Cnt:1
[LoRa]:Join retry Cnt:2
[LoRa]:Join retry Cnt:3
[LoRa]:Join retry Cnt:4
[LoRa]:Join retry Cnt:5
[LoRa]:Join retry Cnt:6
ERROR: RUI_AT_LORA_INFO_STATUS_JOIN_FAIL 99
*/
//Join network
void rk_loraJoin() {
resend:
bool success = false;
sendCommand(F("at+join"));
int retry = 0;
waitJoinNetwork();
}
/*
at+send=lora:2:AABB
[LoRa]: RUI_MCPS_UNCONFIRMED send success
OK
at+recv=0,-47,33,0
*/
bool rk_enableConfirmSend(bool confirm)
{
resend:
String cmd = "at+set_config=lora:confirm:0";
if (confirm)
cmd = "at+set_config=lora:confirm:1";
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
bool rk_loraWanSendData( int channel, String hexData) {
resend:
String cmd = "at+send=lora:";
cmd += String(channel, DEC);
cmd += ":";
cmd += hexData;
sendCommand(cmd);
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
String firstLine = ret; //Igrone first
if (firstLine.startsWith("Wake up"))
{
goto resend;
}
}
else
return false;
ATSerial.setTimeout(60000);
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
//Igrone
}
else
return false;
ATSerial.setTimeout(2000);
ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("OK"))
{
return true;
}
else
{
return false;
}
}
else
return false;
}
/*
at+recv=1,-35,29,1:11
at+recv=0,-47,33,0
*/
String getReceivedLine() {
String ret = ATSerial.readStringUntil('\n');
ret.trim();
if (ret.length() > 0) {
if (ret.startsWith("at+recv"))
{
char* token;
char* rest = ret.c_str();
char * last = NULL;
while ((token = strtok_r(rest, ",", &rest))) {
if (token != NULL) {
last = token;
}
}
if (last != NULL) {
if (last[0] != '0') {
rest = last;
char * data = NULL;
while ((token = strtok_r(rest, ":", &rest))) {
if (token != NULL)
data = token;
}
return String(data);
}
else
{
return "";
}
}
else
{
return "";
}
}
else
{
return "";
}
}
else
{
return "Error";
}
}
void sendCommand(String command) {
delay(200);
// while(Serial.available() > 0) {
// char t = Serial.read();
// }
while (ATSerial.available()) {
char t = ATSerial.read();
}
delay(100);
ATSerial.println(command);
}
#include <stdio.h>
#include <string.h>
#include "SoftwareSerial.h"
#define TXpin 11 // Set the virtual serial port pins
#define RXpin 10
#define ATSerial Serial
//#define DEBUG_MODE
#define ATSerial Serial
#define LED 13
#define SENSOR A0
#define BUTTON 2
#define INP 3
static uint8_t payload[50];//<51 bytes is good
String LocalRegion = "EU868";
const String DevEui = "00CE8AE400F2DFD3";
const String AppEui = "70B3D57ED0027286";
const String AppKey = "FC4C72D5333D7DFB530E24F43B024FDA";
//
const String NwkSKey = "3432567afde4525e7890cfea234a5821";
const String AppSKey = "a48adfc393a0de458319236537a11d90";
const String DevAddr = "00112233";
volatile bool networkConnected = false;
int tryJoin = 0;
volatile byte numPress = 0;
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 30 * 1000;
SoftwareSerial DebugSerial(RXpin, TXpin); // Declare a virtual serial port
void countPress() {
numPress++;
}
void lorawan_otaa_init() {
bool success = false;
DebugSerial.print(">>Get verion ");
String result = rk_getVersion();
DebugSerial.println(result);
//
DebugSerial.print(">>Set Lorawan mode ");//co restart, sau restart tu ket noi, neu dang ket noi ma join mode goi se bao Error,
success = rk_setWorkingMode(0);
DebugSerial.println (success);
delay(100);
if (networkConnected == false) {
DebugSerial.print(">>Set OTAA mode ");//Khong restart nhung goi ham ket noi, khong duoc phep chay khi dang connect. Neu da ket noi thanh cong thi goi lai se active connect lai
success = rk_joinLoRaNetwork(0);
DebugSerial.println (success);
DebugSerial.print(">>Set region to EU 868 ");
success = rk_setRegion(LocalRegion);
DebugSerial.println (success);
//
DebugSerial.print(">>Set class A ");
success = rk_setLoRaClass(0);
DebugSerial.println (success);
//
DebugSerial.print(">>Set the Device EUI ");
success = rk_setOtaaDeviceEui(DevEui);
DebugSerial.println (success);
//
DebugSerial.print(">>Set the Application EUI ");
success = rk_setOtaaAppEui(AppEui);
DebugSerial.println (success);
//
DebugSerial.print(">>Set the Application Key ");
success = rk_setOtaaAppKey(AppKey);
DebugSerial.println (success);
//
DebugSerial.print(">>Join to lora network ");
rk_loraJoin();
DebugSerial.println (networkConnected);
}
//
DebugSerial.print(">>Network Connected ");
DebugSerial.println (networkConnected);
//
DebugSerial.print(">>Enable confirm send data ");
success = rk_enableConfirmSend(true);
DebugSerial.println (success);
if (networkConnected) {
DebugSerial.print(">>Send data");
bool success = rk_loraWanSendData(2, "12345678");
if (success)
{
DebugSerial.println(" success");
}
else
DebugSerial.println(" failer");
}
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(INP, INPUT);
pinMode(BUTTON, INPUT_PULLUP);
DebugSerial.begin(115200);
while (DebugSerial.read() >= 0) {}
while (!DebugSerial);
DebugSerial.println("StartUP");
ATSerial.begin(115200); // Note: Please manually set the baud rate of the WisNode device to 9600.
attachInterrupt(digitalPinToInterrupt(BUTTON), countPress, FALLING);
rk_init();
lorawan_otaa_init();
}
void loop() {
unsigned long currentMillis = millis();
if (networkConnected == false) {
if (tryJoin > 5) {
DebugSerial.print(">>Restart device and connect");
bool success = rk_deviceRestart();
DebugSerial.println (success);
tryJoin = 0;
}
}
if (networkConnected == false) {
tryJoin++;
DebugSerial.print(">>Try Join to lora network ");
rk_loraJoin();
DebugSerial.println (networkConnected);
}
else {
tryJoin = 0;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
if (networkConnected) {
DebugSerial.print(">>Send data");
String dataHex = prepairData();
bool success = rk_loraWanSendData(2, dataHex);
if (success)
{
DebugSerial.println(" success");
DebugSerial.print("Get downlink data:");
String dataIn = getReceivedLine();
if (dataIn.startsWith("00")) {
digitalWrite(LED, LOW);
}
else
digitalWrite(LED, HIGH);
DebugSerial.println(dataIn);
}
else
DebugSerial.println(" failer");
}
previousMillis = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment