I'm able to connect the A7670SA module to the computer directly and run AT commands via UART and accomplish what I need.
When I connect the A7670SA module to the RX/TX of the NodeMCU, I'm able to communicate and send and receive responses.
My sketch utilizes the AT+HTTP commands to get the geo location and store it in a firebase database. When I manually use these commands via UART I'm able to transmit the data to the database.
However when I connect it to the NodeMCU, I am unable as I receive errors and weird characters in the response.
Here is my code:
#include <SoftwareSerial.h>
#define A7670_RX D2 // Connect A7670 TX pin to NodeMCU D2 pin
#define A7670_TX D3 // Connect A7670 RX pin to NodeMCU D3 pin
#define LED_PIN D4 // Define the pin connected to the built-in blue LED
SoftwareSerial A7670Serial(A7670_RX, A7670_TX); // RX, TX
void setup() {
pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
digitalWrite(LED_PIN, HIGH); // Turn on the LED initially
Serial.begin(115200); // Initialize serial communication with Arduino IDE Serial Monitor
A7670Serial.begin(115200); // Initialize SoftwareSerial communication with A7670 module
A7670Serial.write(static_cast<char>(0x00)); // Send a null character to clear any pending data
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
sendATCommand(command);
}
}
void sendATCommand(String command) {
Serial.print("Sending command: ");
Serial.println(command);
digitalWrite(LED_PIN, LOW); // Turn off the LED
A7670Serial.println(command);
delay(1000); // Delay to ensure command is sent
digitalWrite(LED_PIN, HIGH); // Turn on the LED
// Wait for response
delay(1000); // Add delay according to the expected response time
while (A7670Serial.available()) {
String response = A7670Serial.readStringUntil('\n');
Serial.println("Response from A7670: " + response);
}
}
Here is sample output:
For example, when I send AT
Response from A7670: AT OK
when I send AT+HTTPINIT
Response from A7670: AT+HTTPINIT OK
I got it to work, by chunking out requests that were too long. Here is the code for anyone who also encounters this problem:
#include <SoftwareSerial.h>
#define A7670_RX D2 // Connect A7670 TX pin to NodeMCU D2 pin
#define A7670_TX D3 // Connect A7670 RX pin to NodeMCU D3 pin
#define LED_PIN D4 // Define the pin connected to the built-in blue LED
SoftwareSerial A7670Serial(A7670_RX, A7670_TX); // RX, TX
String response = "";
int retrycount = 0;
String dbURL = ("https://database-12a22-default-rtdb.firebaseio.com/.json?auth=POR09fvOhxdrFy23456kle2dFGO34OrolerTSD");
String jsondata = "{\"f_latitude\":\"43.3385544\",\"f_longitude\":\"70.1003989\",\"f_lastupdated\":\"2%507027%50702024+12%516550PM+ET\"}";
int jsonlen = jsondata.length();
void setup() {
pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
digitalWrite(LED_PIN, HIGH); // Turn on the LED initially
Serial.begin(115200); // Initialize serial communication with Arduino IDE Serial Monitor
A7670Serial.begin(115200, SWSERIAL_8N1); // Initialize SoftwareSerial communication with A7670 module, 8 data bits, no parity, 1 stop bit
A7670Serial.write(static_cast<char>(0x00)); // Send a null character to clear any pending data
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
if (command == "send") {
if (sendATCommand("AT", "Check Module")) {
sendATCommand("AT+HTTPINIT", "HTTPINIT");
delay(5000);
sendATCommand("AT+HTTPPARA=\"URL\",\"" + dbURL + "\"", "HTTPPARA URL");
delay(5000);
sendATCommand("AT+HTTPPARA=\"CONTENT\",\"application/json\"", "HTTPPARA JSON");
delay(5000);
sendATCommand("AT+HTTPDATA=" + String(jsonlen) + ",10000", "HTTPDATA COUNT");
delay(5000);
sendATCommand(jsondata, "DATA");
delay(5000);
sendATCommand("AT+HTTPACTION=4", "POST ACTION");
delay(5000);
sendATCommand("AT+HTTPHEAD", "HEAD RESPONSE");
delay(5000);
sendATCommand("AT+HTTPTERM", "HTTP TERMINATE");
} else {
Serial.println("Module not ready.");
}
} else {
sendATCommand(command, command);
}
}
if (A7670Serial.available()) {
response = A7670Serial.readStringUntil('\n');
Serial.println("Response from A7670: " + response);
}
}
bool sendATCommand(String command, String commandName) {
Serial.print("Sending command: ");
Serial.println(commandName);
digitalWrite(LED_PIN, LOW); // Turn off the LED
// Clear serial buffer
while (A7670Serial.available() > 0) {
A7670Serial.read();
}
// Send command in chunks
int len = command.length();
int index = 0;
while (index < len) {
int chunkSize = min(32, len - index); // Limit chunk size to 32 characters
A7670Serial.print(command.substring(index, index + chunkSize));
index += chunkSize;
delay(100); // Add a small delay between sending chunks
}
A7670Serial.println(); // Send newline at the end
unsigned long startTime = millis();
String response;
do {
while (A7670Serial.available() > 0) {
response = A7670Serial.readStringUntil('\n');
Serial.println(response);
if (response.startsWith("OK") || response.startsWith("DOWNLOAD")) {
Serial.println(); // Print a newline after the response
digitalWrite(LED_PIN, HIGH); // Turn on the LED
retrycount = 0;
return true; // Exit the loop and function
}
if (response.startsWith("ERROR") && commandName != "HEAD RESPONSE") {
if (retrycount < 3) {
retrycount++;
Serial.println("\nError response from A7670 ... retry #" + String(retrycount));
delay(3000); // Wait for 3 seconds
// Retry sending the command again
sendATCommand(command, commandName);
return true; // Exit the loop and function
}
}
}
// If no response yet, print dots
Serial.print(".");
delay(500); // Wait for half a second
} while (millis() - startTime < 10000); // Timeout after 10 seconds
// If timeout reached
digitalWrite(LED_PIN, HIGH); // Turn on the LED
Serial.println("\nTimeout waiting for response from A7670.");
retrycount = 0;
return false;
}