adafruit / Adafruit_FONA

Arduino library for the Adafruit FONA
https://www.adafruit.com/products/1946
211 stars 238 forks source link

TCPconnected Does not work on FONA 3G #116

Open OscarVanL opened 4 years ago

OscarVanL commented 4 years ago

The TCPconnected function sends AT+CIPSTATUS, however this is not a valid command for the FONA 3G's SIM5320. As a result, this fails every time.

In some other libraries like Adafruit_MQTT_FONA it checks the TCP connection to the MQTT server is open with TCPconnected(), this fails every time for the above issue.

I have not yet found which AT command is a substitute for AT+CIPSTATUS for the FONA 3G.

kukukk commented 4 years ago

Hi,

If you are using only 1 active connection at a time, you should try something like this:

boolean Adafruit_FONA_3G::TCPconnected(void) {
  float index;
  return sendParseReply(F("AT+CIPOPEN?"), F("+CIPOPEN: "), &index, ',', 4);
}

Btw, are the TCP functions working for you with FONA 3G?

OscarVanL commented 4 years ago

That looks like it should work :)

The TCP functions in this fork do not work on the FONA 3G without modification, as it uses different AT conmands. I adapted them and have got it to work. I can open a TCP socket successfully and send TCP packets. I also got the MQTT Connect and Publish functionality to work too.

I've committed some of the changes to my fork of this repo, if you're interested you can have a look.

kukukk commented 4 years ago

They say that the TCP functions are working with FONA 3G: https://forums.adafruit.com/viewtopic.php?f=54&t=162392

But I had to implement the following TCP functions to be able to work with FONA 3G:

bool Adafruit_FONA_3G::TCPconnect(char *server, uint16_t port)
{
  flushInput();

  // Close old connection
  getReply(F("AT+CIPCLOSE=0"));

  // Get data manually
  if (! sendCheckReply(F("AT+CIPRXGET=1"), ok_reply) ) return false;

  char sendcmd[50];
  sprintf(sendcmd, "AT+CIPOPEN=0,\"TCP\",\"%s\",%u", server, port);

  return sendCheckReply(sendcmd, F("Connect ok"), 5000);
}
bool Adafruit_FONA_3G::TCPclose(void)
{
  return sendCheckReply(F("AT+CIPCLOSE=0"), ok_reply);
}
boolean Adafruit_FONA_3G::TCPconnected(void) {
  float index;
  return sendParseReply(F("AT+CIPOPEN?"), F("+CIPOPEN: "), &index, ',', 4);
}
boolean Adafruit_FONA_3G::TCPsend(char *packet, uint8_t len) {
  flushInput();

  char sendcmd[20];
  sprintf(sendcmd, "AT+CIPSEND=0,%u", len);
  if (! sendCheckReply(sendcmd, F(">"))) return false;

  return sendCheckReply(packet, ok_reply);
}
uint16_t Adafruit_FONA_3G::TCPavailable(void) {
  float avail;

  if (! sendParseReply(F("AT+CIPRXGET=4,0"), F("+CIPRXGET: 4,0,"), &avail, ',', 0) ) return 0;

  DEBUG_PRINT (avail); DEBUG_PRINTLN(F(" bytes available"));

  return (uint16_t)avail;
}
uint16_t Adafruit_FONA_3G::TCPread(uint8_t *buff, uint8_t len) {
  float avail;

  mySerial->print(F("AT+CIPRXGET=2,0,"));
  mySerial->println(len);
  readline();

  if (! parseReply(F("+CIPRXGET: 2,0,"), &avail, ',', 0)) return false;

  readRaw((uint16_t)avail);

#ifdef ADAFRUIT_FONA_DEBUG
  DEBUG_PRINT (avail); DEBUG_PRINTLN(F(" bytes read"));
  for (uint8_t i=0;i<avail;i++) {
  DEBUG_PRINT(F(" 0x")); DEBUG_PRINT(replybuffer[i], HEX);
  }
  DEBUG_PRINTLN();
#endif

  memcpy(buff, replybuffer, (uint16_t)avail);

  return (uint16_t)avail;
}
OscarVanL commented 4 years ago

Looks similar to (but better/more complete) than the stuff I had to do. Wish I'd had those functions 1 week ago today, would have saved me a lot of time.

It seems Adafruit have abandoned this Adafruit_FONA library, fortunately there's a dude called botletics who has made a fork of this FONA repo which adds a lot of support for other chips and is still given attention, this claims to support the FONA 3G / SIM5320, but it's still not great. https://github.com/botletics/SIM7000-LTE-Shield

You should make a PR to add this support for the FONA 3G / SIM5320 :) This code in botletics repo would make a pretty decent library for the FONA 3G.

TheRobertTalley commented 3 years ago

This is what im looking for! im using tim woo's botletics library, esp32 and a sim7000 trying to connect to AIO. any ideas what is wrong with this?

https://github.com/TheRobertTalley/lilygo-sim7000-ESP32/commit/cde2727706193dbcb42b19255d14c1e45d8da83f

I dont know how to use what kukukk posted directly. An example would be well appreciated, deffinatley a free beer in order. Im not sure how to call functions from the library. I have posted here also if the nice folks here would take a look:

https://www.reddit.com/r/esp32/comments/oykl6r/adafruit_io_esp32sim7000_lilygo_sim7000x_aio_over/