Hi.
I’m using a Particle Electron to create a Serial-TCP gateway. I’m not using the included M2M SIM (because of data limit), I use another Company SIM.
My Electron creates a TCP Client that connects to a Twisted socket server, all seems to work fine but every communication takes more than 4 seconds.
I connect a USB-TTL232 in pins C0 and C1 (Serial5) and with a python script answer some long text (200 chars) after receiving a “hello word” string.
Between I send a “hello word” by TCP (twisted) to Electron, and Electron prints at debug console, takes ~ 2 seconds, the script reads from serial and answer quickly, then takes another 2 seconds to return the response with client tcp socket.
I’ve tried without electron (using multiple telnet) and send&response is less than 50 ms.
Can the 3G Modem send and receive byte packages very quick or it’s not designed to do this kind of things?
How can I enable AT debug commands in my code?
Need to build and flash another firmware?
Electron firmware: 0.6.2
Our code:
#include "cellular_hal.h"
STARTUP(cellular_credentials_set("internet", "orange", "orange", NULL));
#include "Serial5/Serial5.h"
#define SERIAL232 Serial5
SYSTEM_MODE(SEMI_AUTOMATIC);
TCPClient client;
byte server[] = {1, 1, 1, 1 }; // the server IP address
int port = 11111; // port
bool tcpConnected=false;
// serial buffer
#define MAX_BUFF 1024
uint8_t serBufdata[MAX_BUFF];
unsigned int indexBufdata=0;
void setup()
{
Serial.begin(9600);
SERIAL232.begin(9600, SERIAL_8N1);
Particle.connect();
}
void ourTCPConnect() {
Serial.print(millis());
Serial.printf(" connecting to ... %d.%d.%d.%d:%d\n", server[0], server[1], server[2], server[3], port);
if (client.connect(server, port))
{
Serial.print(millis());
Serial.println(" connected");
tcpConnected=true;
}
else
{
Serial.print(millis());
Serial.print(" connection failed. Waiting");
tcpConnected=false;
}
}
bool readCharFromTCP() {
unsigned int s1;
s1=indexBufdata;
while (client.available()) {
if(indexBufdata > 1022) {
return false;
}
serBufdata[indexBufdata] = client.read();
indexBufdata++;
}
return indexBufdata > s1;
}
bool readCharFromSerial() {
unsigned int s1;
s1=indexBufdata;
while (SERIAL232.available()) {
if(indexBufdata > MAX_BUFF - 1) {
return false;
}
serBufdata[indexBufdata] = SERIAL232.read();
indexBufdata++;
}
return indexBufdata > s1;
}
void loop()
{
Particle.process();
if (!client.connected()) {
Serial.print(millis());
Serial.println(" lost connection");
tcpConnected=false;
}
if(!tcpConnected) {
ourTCPConnect();
delay(100);
return;
}
// from here we are connected
// read data from TCP
if(client.available()) {
memset(serBufdata, 0, sizeof(serBufdata));
indexBufdata=0;
while(readCharFromTCP()) {
delay(10);
}
if(indexBufdata > 0) {
Serial.print("[");
Serial.print(millis());
Serial.print("] ");
Serial.print("<= TCP num = ");
Serial.print(indexBufdata);
Serial.print(" data = ");
Serial.println((const char*)serBufdata);
// send TCP data to 232
SERIAL232.write(serBufdata, indexBufdata);
SERIAL232.flush();
}
}
if(SERIAL232.available()) {
memset(serBufdata, 0, sizeof(serBufdata));
indexBufdata=0;
while(readCharFromSerial()) {
delay(10);
}
if(indexBufdata > 0) {
Serial.print("[");
Serial.print(millis());
Serial.print("] ");
Serial.print("<= SERIAL232 num = ");
Serial.print(indexBufdata);
Serial.print(" data = ");
Serial.println((const char*)serBufdata);
// return 232 data to TCP
client.write(serBufdata, indexBufdata);
client.flush();
}
}
}
UPDATE:
Seems like TCP Client enters in “Standby” mode.
If you send packages every second, for example, the delay decreases under 1 second.
I’ve tried with a simpler program:
TCPClient client;
byte server[] = { 1, 1, 1, 1 };
void setup() {
Serial.begin(9600);
while(!Serial.available()) Particle.process();
Serial.println("connecting...");
if (client.connect(server, 33000))
{
Serial.println("connected");
client.println();
}
else
{
Serial.println("connection failed");
}
}
void loop() {
Particle.process();
if (client.available())
{
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;);
}
}
We have good 3G signal, but when we try to send ( client.write(someMsg, size) ) some package (size ~ 120), it takes 2~4 seconds (or even more).
How can we increase the send speed atleast?
Thanks in advance!