From bfe87b34a5414ffb2cdb2d2005cb9d10b5a719f0 Mon Sep 17 00:00:00 2001 From: Dhaval Gujar Date: Tue, 2 Aug 2022 14:17:10 +0530 Subject: [PATCH 1/3] Updated Arduino Sketch - The sketch has been updated to include a timeout value for command execution. - This has been done to aid the execution and response reading for long-running commands like AT+CONNECT. - AT commands in the sketch have been updated to work with new releases of ExpressLink. --- sketches/arduino_sample_sketch.ino | 48 +++++++++++++++++++----------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/sketches/arduino_sample_sketch.ino b/sketches/arduino_sample_sketch.ino index eacde39..f1efb7e 100644 --- a/sketches/arduino_sample_sketch.ino +++ b/sketches/arduino_sample_sketch.ino @@ -7,6 +7,10 @@ SoftwareSerial mySerial(8, 9); // RX, TX #define MY_AWS_IOT_ENDPOINT "-ats.iot..amazonaws.com" +#define HOST_RESPONSE_BUFFER_SIZE 500 + +char buffer[HOST_RESPONSE_BUFFER_SIZE]; + typedef enum event_t { EVENT_NONE = 0, EVENT_STARTUP = 2, @@ -17,7 +21,6 @@ typedef enum event_t { EVENT_CONFMODE, } event_t; - typedef enum { STATE_INIT = 0, STATE_WAIT_EL_READY, @@ -30,12 +33,19 @@ typedef enum { static state_t state; static unsigned long got_connected; +static unsigned long saved_timeout_value_ms; -String execute_command(String command) +String execute_command(String command, unsigned long timeout_ms) { - Serial.print(command); - String response = Serial.readString(); - return response; + saved_timeout_value_ms = Serial.getTimeout(); + Serial.setTimeout(timeout_ms); + memset(buffer, 0, HOST_RESPONSE_BUFFER_SIZE); + Serial.println(command); + Serial.readBytesUntil('\n', buffer, HOST_RESPONSE_BUFFER_SIZE); + String s(buffer); + s.trim(); + Serial.setTimeout(saved_timeout_value_ms); + return s; } void expresslink_reset() @@ -53,8 +63,8 @@ event_t process_event() val = digitalRead(EVENT_PIN); if(val) { - response = execute_command("AT+EVENT?\n"); - if (response.equals("OK\n")) + response = execute_command("AT+EVENT?", 3000); + if (response.equals("OK")) { return EVENT_NONE; } @@ -65,7 +75,6 @@ event_t process_event() total_read = sscanf(response.c_str(), "%s %d %d %*s" , ok_string, &event_number, &topic_index); - return event_number; } } @@ -83,8 +92,9 @@ state_t process_ssid(String response) if(total_read > 1) { return STATE_PROVISIONED; } - else + else { return STATE_UNPROVISIONED; + } } void setup() @@ -101,6 +111,7 @@ void setup() ; } got_connected = 0; + saved_timeout_value_ms = 0; } void loop() @@ -127,16 +138,16 @@ void loop() break; case STATE_EL_READY: - response = execute_command("AT+CONF EndPoint="MY_AWS_IOT_ENDPOINT"\n"); - response = execute_command("AT+CONF Topic1=TEST\n"); - response = execute_command("AT+CONF? SSID\n"); + response = execute_command("AT+CONF Endpoint="MY_AWS_IOT_ENDPOINT"", 3000); + response = execute_command("AT+CONF Topic1=TEST", 3000); + response = execute_command("AT+CONF? SSID", 3000); state = process_ssid(response); break; case STATE_UNPROVISIONED: - response = execute_command("AT+CONFMODE\n"); - if (response.equals("OK CONFMODE ENABLED\n")) + response = execute_command("AT+CONFMODE", 5000); + if (response.equals("OK CONFMODE ENABLED")) { state = STATE_PROVISIONING; } @@ -149,10 +160,11 @@ void loop() break; case STATE_PROVISIONED: - response = execute_command("AT+CONNECT\n"); - if (response.equals("OK CONNECTED\n")) { + response = execute_command("AT+CONNECT", 30000); + if (response.equals("OK 1 CONNECTED")) { state = STATE_CONNECTED; } + break; case STATE_CONNECTED: if (event == EVENT_CONLOST) @@ -161,9 +173,9 @@ void loop() } if (millis() - got_connected >= 10000) { - response = execute_command("AT+SEND1 Hello World\n"); + response = execute_command("AT+SEND1 Hello World", 5000); got_connected = millis(); } break; } -} +} \ No newline at end of file From 96f96d566297d2a93acbd61bc50e14c3711d4f75 Mon Sep 17 00:00:00 2001 From: Dhaval Gujar Date: Tue, 2 Aug 2022 14:26:49 +0530 Subject: [PATCH 2/3] Updated README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 77e29ef..4d10df2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Getting Started Guide for Espressif's AWS IoT ExpressLink Evaluation Kit +> **IMPORTANT**: ExpressLink firmware releases can only be flashed on ExpressLink modules and DevKits. + # Table of Contents 1. [Document Information](#1-document-information) From 18366cc36b3c9eecfeba2ded1afcca0d91893ab7 Mon Sep 17 00:00:00 2001 From: Amit Sheth Date: Thu, 4 Aug 2022 18:54:39 +0530 Subject: [PATCH 3/3] Minor refactoring 1. readBytesUntil => readStringUntil 2. Moved certain globals to local 3. Declaring variables closer to their respective uses, unless declaring at top provides easy configurability 4. Avoid publishing in case of conlost --- sketches/arduino_sample_sketch.ino | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/sketches/arduino_sample_sketch.ino b/sketches/arduino_sample_sketch.ino index f1efb7e..f946347 100644 --- a/sketches/arduino_sample_sketch.ino +++ b/sketches/arduino_sample_sketch.ino @@ -1,16 +1,13 @@ #include -SoftwareSerial mySerial(8, 9); // RX, TX +#define SERIAL_RX_PIN 8 +#define SERIAL_TX_PIN 9 #define EVENT_PIN 2 #define RESET_PIN 4 #define MY_AWS_IOT_ENDPOINT "-ats.iot..amazonaws.com" -#define HOST_RESPONSE_BUFFER_SIZE 500 - -char buffer[HOST_RESPONSE_BUFFER_SIZE]; - typedef enum event_t { EVENT_NONE = 0, EVENT_STARTUP = 2, @@ -31,18 +28,12 @@ typedef enum { STATE_CONNECTED, } state_t; -static state_t state; -static unsigned long got_connected; -static unsigned long saved_timeout_value_ms; - String execute_command(String command, unsigned long timeout_ms) { - saved_timeout_value_ms = Serial.getTimeout(); + unsigned long saved_timeout_value_ms = Serial.getTimeout(); Serial.setTimeout(timeout_ms); - memset(buffer, 0, HOST_RESPONSE_BUFFER_SIZE); Serial.println(command); - Serial.readBytesUntil('\n', buffer, HOST_RESPONSE_BUFFER_SIZE); - String s(buffer); + String s = Serial.readStringUntil('\n'); s.trim(); Serial.setTimeout(saved_timeout_value_ms); return s; @@ -99,7 +90,7 @@ state_t process_ssid(String response) void setup() { - state = STATE_INIT; + SoftwareSerial mySerial(SERIAL_RX_PIN, SERIAL_TX_PIN); Serial.begin(115200); mySerial.begin(115200); pinMode(EVENT_PIN, INPUT); @@ -110,14 +101,13 @@ void setup() while (!mySerial) { ; } - got_connected = 0; - saved_timeout_value_ms = 0; } void loop() { event_t event = EVENT_NONE; String response; + static state_t state = STATE_INIT; if (state != STATE_INIT) { @@ -170,12 +160,14 @@ void loop() if (event == EVENT_CONLOST) { state = STATE_PROVISIONED; + break; } - if (millis() - got_connected >= 10000) + static unsigned long last_send_time; + if (millis() - last_send_time >= 10000) { response = execute_command("AT+SEND1 Hello World", 5000); - got_connected = millis(); + last_send_time = millis(); } break; } -} \ No newline at end of file +}