Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <SoftwareSerial.h>
- #include <inttypes.h>
- #define PD0 5
- #define PD1 4
- char c=' ';
- static char recv_buf[512];
- static bool is_exist = false;
- static bool is_join = false;
- static int led = 0;
- class mySerial : public SoftwareSerial {
- public:
- mySerial(uint8_t receivePin, uint8_t transmitPin,
- bool inverse_logic = false) :
- SoftwareSerial(receivePin, transmitPin, inverse_logic) {}
- virtual size_t write(uint8_t byte) {
- return SoftwareSerial::write(byte);
- }
- int printf(char* fmt, ...) {
- char buff[256];
- va_list args;
- va_start(args, fmt);
- int return_status = vsnprintf(buff, sizeof(buff), fmt, args);
- va_end(args);
- uint8_t *s = (uint8_t *)&buff;
- while (*s) write(*s++);
- return return_status;
- }
- };
- mySerial SerialE5(PD0, PD1);
- static int at_send_check_response(char *p_ack, int timeout_ms, char *p_cmd, ...)
- {
- int ch;
- int num = 0;
- int index = 0;
- int startMillis = 0;
- va_list args;
- memset(recv_buf, 0, sizeof(recv_buf));
- va_start(args, p_cmd);
- SerialE5.printf(p_cmd, args);
- Serial.printf(p_cmd, args);
- va_end(args);
- delay(200);
- startMillis = millis();
- if (p_ack == NULL)
- {
- return 0;
- }
- do
- {
- while (SerialE5.available() > 0)
- {
- ch = SerialE5.read();
- recv_buf[index++] = ch;
- Serial.print((char)ch);
- delay(2);
- }
- if (strstr(recv_buf, p_ack) != NULL)
- {
- return 1;
- }
- } while (millis() - startMillis < timeout_ms);
- return 0;
- }
- static void recv_prase(char *p_msg)
- {
- if (p_msg == NULL)
- {
- return;
- }
- char *p_start = NULL;
- int data = 0;
- int rssi = 0;
- int snr = 0;
- p_start = strstr(p_msg, "RX");
- if (p_start && (1 == sscanf(p_start, "RX: \"%d\"\r\n", &data)))
- {
- Serial.println(data);
- }
- p_start = strstr(p_msg, "RSSI");
- if (p_start && (1 == sscanf(p_start, "RSSI %d,", &rssi)))
- {
- Serial.print("rssi:");
- Serial.println(rssi);
- }
- p_start = strstr(p_msg, "SNR");
- if (p_start && (1 == sscanf(p_start, "SNR %d", &snr)))
- {
- Serial.print("snr :");
- Serial.println(snr);
- }
- }
- void setup(void)
- {
- Serial.begin(9600);
- SerialE5.begin(9600);
- Serial.print("E5 LORAWAN TEST\r\n");
- if (at_send_check_response("+AT: OK", 100, "AT\r\n"))
- {
- is_exist = true;
- at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n");
- at_send_check_response("+MODE: LWOTAA", 1000, "AT+MODE=LWOTAA\r\n");
- at_send_check_response("+DR: EU868", 1000, "AT+DR=EU868\r\n");
- at_send_check_response("+CH: NUM", 1000, "AT+CH=NUM,0-2\r\n");
- at_send_check_response("+KEY: APPKEY", 1000, "AT+KEY=APPKEY,\"2B7E151628AED2A6ABF7158809CF4F3C\"\r\n");
- at_send_check_response("+CLASS: C", 1000, "AT+CLASS=A\r\n");
- at_send_check_response("+PORT: 8", 1000, "AT+PORT=8\r\n");
- delay(200);
- is_join = true;
- }
- else
- {
- is_exist = false;
- Serial.print("No E5 module found.\r\n");
- }
- }
- void loop(void)
- {
- if (is_exist)
- {
- int ret = 0;
- if (is_join)
- {
- ret = at_send_check_response("+JOIN: Network joined", 12000, "AT+JOIN\r\n");
- if (ret)
- {
- is_join = false;
- }
- else
- {
- at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n");
- Serial.print("JOIN failed!\r\n\r\n");
- delay(5000);
- }
- }
- else
- //Ex: Copy and Paste --> AT+MSGHEX="68 65 6c 6c 6f 20 77 6f 72 6c 64" <-- to serial monitor
- {
- if (SerialE5.available())
- {
- c = SerialE5.read();
- Serial.write(c);
- }
- if (Serial.available())
- {
- c = Serial.read();
- SerialE5.write(c);
- }
- }
- }
- else
- {
- delay(1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement