Advertisement
Guest User

Untitled

a guest
Dec 16th, 2021
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include <inttypes.h>
  4.  
  5. #define PD0  5
  6. #define PD1  4
  7. char c=' ';
  8.  
  9.  
  10. static char recv_buf[512];
  11. static bool is_exist = false;
  12. static bool is_join = false;
  13. static int led = 0;
  14.  
  15. class mySerial : public SoftwareSerial {
  16.  
  17.   public:
  18.     mySerial(uint8_t receivePin, uint8_t transmitPin,
  19.              bool inverse_logic = false) :
  20.       SoftwareSerial(receivePin, transmitPin, inverse_logic) {}
  21.  
  22.     virtual size_t write(uint8_t byte) {
  23.       return SoftwareSerial::write(byte);
  24.     }
  25.  
  26.     int printf(char* fmt, ...) {
  27.       char buff[256];
  28.       va_list args;
  29.       va_start(args, fmt);
  30.       int return_status = vsnprintf(buff, sizeof(buff), fmt, args);
  31.       va_end(args);
  32.       uint8_t *s = (uint8_t *)&buff;
  33.       while (*s) write(*s++);
  34.       return return_status;
  35.     }
  36. };
  37.  
  38. mySerial SerialE5(PD0, PD1);
  39.  
  40. static int at_send_check_response(char *p_ack, int timeout_ms, char *p_cmd, ...)
  41. {
  42.   int ch;
  43.   int num = 0;
  44.   int index = 0;
  45.   int startMillis = 0;
  46.   va_list args;
  47.   memset(recv_buf, 0, sizeof(recv_buf));
  48.   va_start(args, p_cmd);
  49.   SerialE5.printf(p_cmd, args);
  50.   Serial.printf(p_cmd, args);
  51.   va_end(args);
  52.   delay(200);
  53.   startMillis = millis();
  54.  
  55.   if (p_ack == NULL)
  56.   {
  57.     return 0;
  58.   }
  59.  
  60.   do
  61.   {
  62.     while (SerialE5.available() > 0)
  63.     {
  64.       ch = SerialE5.read();
  65.       recv_buf[index++] = ch;
  66.       Serial.print((char)ch);
  67.       delay(2);
  68.     }
  69.  
  70.     if (strstr(recv_buf, p_ack) != NULL)
  71.     {
  72.       return 1;
  73.     }
  74.  
  75.   } while (millis() - startMillis < timeout_ms);
  76.   return 0;
  77. }
  78.  
  79. static void recv_prase(char *p_msg)
  80. {
  81.   if (p_msg == NULL)
  82.   {
  83.     return;
  84.   }
  85.   char *p_start = NULL;
  86.   int data = 0;
  87.   int rssi = 0;
  88.   int snr = 0;
  89.  
  90.   p_start = strstr(p_msg, "RX");
  91.   if (p_start && (1 == sscanf(p_start, "RX: \"%d\"\r\n", &data)))
  92.   {
  93.     Serial.println(data);
  94.  
  95.   }
  96.  
  97.   p_start = strstr(p_msg, "RSSI");
  98.   if (p_start && (1 == sscanf(p_start, "RSSI %d,", &rssi)))
  99.   {
  100.  
  101.     Serial.print("rssi:");
  102.     Serial.println(rssi);
  103.   }
  104.   p_start = strstr(p_msg, "SNR");
  105.   if (p_start && (1 == sscanf(p_start, "SNR %d", &snr)))
  106.   {
  107.  
  108.     Serial.print("snr :");
  109.     Serial.println(snr);
  110.   }
  111. }
  112.  
  113. void setup(void)
  114. {
  115.  
  116.   Serial.begin(9600);
  117.   SerialE5.begin(9600);
  118.   Serial.print("E5 LORAWAN TEST\r\n");
  119.  
  120.   if (at_send_check_response("+AT: OK", 100, "AT\r\n"))
  121.   {
  122.     is_exist = true;
  123.         at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n");
  124.         at_send_check_response("+MODE: LWOTAA", 1000, "AT+MODE=LWOTAA\r\n");
  125.         at_send_check_response("+DR: EU868", 1000, "AT+DR=EU868\r\n");
  126.         at_send_check_response("+CH: NUM", 1000, "AT+CH=NUM,0-2\r\n");
  127.         at_send_check_response("+KEY: APPKEY", 1000, "AT+KEY=APPKEY,\"2B7E151628AED2A6ABF7158809CF4F3C\"\r\n");
  128.         at_send_check_response("+CLASS: C", 1000, "AT+CLASS=A\r\n");
  129.         at_send_check_response("+PORT: 8", 1000, "AT+PORT=8\r\n");
  130.     delay(200);
  131.  
  132.     is_join = true;
  133.   }
  134.   else
  135.   {
  136.     is_exist = false;
  137.     Serial.print("No E5 module found.\r\n");
  138.  
  139.   }
  140. }
  141. void loop(void)
  142. {  
  143.   if (is_exist)
  144.   {
  145.     int ret = 0;
  146.     if (is_join)
  147.     {
  148.  
  149.       ret = at_send_check_response("+JOIN: Network joined", 12000, "AT+JOIN\r\n");
  150.       if (ret)
  151.       {
  152.         is_join = false;
  153.       }
  154.       else
  155.       {
  156.         at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n");
  157.         Serial.print("JOIN failed!\r\n\r\n");
  158.         delay(5000);
  159.       }
  160.     }
  161.     else
  162.     //Ex: Copy and Paste --> AT+MSGHEX="68 65 6c 6c 6f 20 77 6f 72 6c 64" <--  to serial monitor
  163.     {
  164.       if (SerialE5.available())
  165.       {
  166.         c = SerialE5.read();
  167.         Serial.write(c);
  168.       }
  169.       if (Serial.available())
  170.       {
  171.         c = Serial.read();
  172.         SerialE5.write(c);
  173.       }
  174.     }
  175.   }
  176.   else
  177.   {
  178.     delay(1000);
  179.   }
  180. }
Advertisement
RAW Paste Data Copied
Advertisement