Hello all!
I am attempting to communicate with a SIM7600 G-H using the Software Serial library on an Arduino Uno (ELEGOO UNO R3 to be precise).
I am able to get responses from the modem but the responses don't really make sense when I get to the really important stuff (hitting a public API I'm using for testing). The incoming message is all jumbled up and out of place compared to where I expect it...Please see my code snippet below as well as a response snippet.
#include <Arduino.h>
#include "SoftwareSerial.h"
#include <stdio.h>
#include <string.h>
#define rxp 2
#define txp 3
SoftwareSerial mySerial = SoftwareSerial (rxp,txp);
const unsigned int MAX_MESSAGE_LENGTH = 400;
String data;
void mySerialRead() {
while (!mySerial.available())
{
//wait for data
}
//Check to see if anything is available in the serial receive buffer
while (mySerial.available() > 0)
{
//Read the next available byte in the serial receive buffer
char inByte = mySerial.read();
//Message coming in (check not terminating character) and guard for over message size
static char input_line [MAX_MESSAGE_LENGTH];
static unsigned int input_pos = 0;
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
Serial.println(input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_MESSAGE_LENGTH - 1))
input_line [input_pos++] = inByte;
break;
}
}
}
void apiTest() {
String http_str = "AT+HTTPPARA=\"URL\",\"http://worldtimeapi.org/api/timezone/America/Chicago\"\r";//hitting this API just to see if I can get a chunk of data > buffer size
Serial.println("init");
mySerial.print("AT+HTTPINIT\r"); //starts http service.
delay(200);
mySerialRead();
Serial.println("END INIT");
delay(2000);
Serial.println("http string");
mySerial.print(http_str); //Set URL parameter
delay(200);
mySerialRead();
Serial.println("END HTTP STRING");
delay(2000);
Serial.println("http action");
mySerial.print("AT+HTTPACTION=0\r"); //0 = get, 1 = post
delay(200);
mySerialRead();
Serial.println("END HTTP ACTION");
delay(5000);
Serial.println("read http data");
mySerial.print("AT+HTTPREAD=0,350\r"); //I expect a message of 351 bytes so I chose 350 here for maximum
delay(200);
mySerialRead();
Serial.println("END READ");
Serial.println("term");
delay(500);
mySerial.print("AT+HTTPTERM\r"); //stops http service
delay(500);
//Serial.print(mySerial.readString());
mySerialRead();
Serial.println("END TERM");
}
void setup() {
pinMode(rxp, INPUT);
pinMode(txp, OUTPUT);
Serial.begin(9600);
while(!Serial){}
mySerial.begin(9600);
delay(100);
Serial.print("Starting the test");
}
void loop()
{
Serial.println("\n starting over\n");
apiTest();
delay(3000);
}
Here is the response - I let it run for a few loops to show hopefully show a bigger picture:
Starting the test
starting over
init
OK
END INIT
http string
OK
END HTTP STRING
http action
OK
END HTTP ACTION
read http data
ERROR
END READ
term
ERROR
END TERM
starting over
init
ERROR
END INIT
http string
OK
END HTTP STRING
http action
+HTTPACTION: 0,200,351
OK
END HTTP ACTION
read http data
+HTTPACTION: 0,200,351
OK
+HTTPREAD: DATA,350
END READ
term
END TERM
starting over
init
{"abbrev:null,"dst_offset":0,"dst_until":null,"raw_offset":-21600,"timezone":"America/Chicago","unixtime"
OK
END INIT
http string
OK
END HTTP STRING
http action
OK
END HTTP ACTION
read http data
ERROR
END READ
term
ERROR
END TERM
starting over
init
ERROR
END INIT
http string
OK
END HTTP STRING
http action
+HTTPACTION: 0,200,351
OK
END HTTP ACTION
read http data
+HTTPACTION: 0,200,351
OK
+HTTPREAD: DATA,350
END READ
term
END TERM
starting over
init
{"abbrevull,"dst_offset":0,"dst_until":null,"raw_offset":-21600,"timezone":"America/Chicago","unixtime":1
OK
END INIT
http string
OK
END HTTP STRING
http action
OK
END HTTP ACTION
read http data
+HTTPACTION: 0,200,351
OK
+HTTPREAD: DATA,350
END READ
term
END TERM
My concerns are with the response starting with "{"abbrevull"...
I would expect this to be sent immediately after the HTTP READ and prior to the HTTP TERM but somehow it doesn't get pushed through until after my loop restarts???
ALSO, here is what that API response is SUPPOSED to look like for reference...
{"abbreviation":"CST","client_ip":"2600:1700:6e0:c890:55c0:8c99:730c:b632","datetime":"2022-12-16T00:37:14.882338-06:00","day_of_week":5,"day_of_year":350,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":-21600,"timezone":"America/Chicago","unixtime":1671172634,"utc_datetime":"2022-12-16T06:37:14.882338+00:00","utc_offset":"-06:00","week_number":50}
Any help on this would be GREATLY appreciated!!! Thank you in advance!!