Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

POST request ESP32 with SIM7000

Status
Not open for further replies.

JGLJGL

New Member
Hi, I'm new and not able to make a POSTt request.
I'm working with the arduino IDE and attached to the ESP32 a SIM7000E.
I 've tried the "SIM7000-LTE-Shield-master"->"ESP32_LTE_Demo" , but not being able to make it work (POST request).

Here it is what I have:
Code:
Code:
void loop() {

 Serial.print(F("FONA> "));
 
  while (! Serial.available() ) {
    if (fona.available()) {
      Serial.write(fona.read());
    }
  }
          // Post data to website
  
        uint16_t statuscode;
        int16_t length;
        char url[150];
        char data[150];


        flushSerial();
        Serial.println(F("NOTE: in beta! Use simple websites to post!"));
        Serial.println(F("URL to post (e.g. httpbin.org/post):"));
        Serial.print(F("http://")); readline(url, 79);
        Serial.println(url);
        Serial.println(F("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):"));
        readline(data, 79);
        Serial.println(data);

        Serial.println(F("****"));
        if (!fona.HTTP_POST_start(url, F("text/plain"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
          Serial.println("Failed!");

        }
        while (length > 0) {
          while (fona.available()) {
            char c = fona.read();

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
            loop_until_bit_is_set(UCSR0A, UDRE0); // Wait until data register empty.
            UDR0 = c;
#else
            Serial.write(c);
#endif

            length--;
            if (! length) break;
          }
        }
        Serial.println(F("\n****"));
        fona.HTTP_POST_end();
        

      
// flush input
  flushSerial();
  while (fona.available()) {
    Serial.write(fona.read());
  }
}

Serial monitor response:
Code:
12:10:28.794 -> NOTE: in beta! Use simple websites to post!
12:10:28.794 -> URL to post (e.g. httpbin.org/post):
12:10:28.794 -> http://www.atesens.com/post
12:10:40.286 -> Data to post (e.g. "foo" or "{"simple":"json"}"):
12:10:43.242 -> password
12:10:43.242 -> ****
12:10:43.276 ->     ---> AT+HTTPTERM
12:10:43.311 ->     <--- OK
12:10:43.346 ->     ---> AT+HTTPINIT
12:10:43.381 ->     <--- OK
12:10:43.416 ->     ---> AT+HTTPPARA="CID"
12:10:43.449 ->     <--- OK
12:10:43.482 ->     ---> AT+HTTPPARA="UA"
12:10:43.515 ->     <--- OK
12:10:43.582 ->     ---> AT+HTTPPARA="URL"
12:10:43.615 ->     <--- OK
12:10:43.649 ->     ---> AT+HTTPPARA="REDIR"
12:10:43.682 ->     <--- OK
12:10:43.751 ->     ---> AT+HTTPSSL=1
12:10:43.785 ->     <--- ERROR
12:10:43.785 -> Failed!
 
I dont have any experience of sim7000.
However I have tried to use espressifs server library, I'm sure its perfectly fine but I had trouble uploading large datablocks with it, even with send_chunk.
 
Ok thanks, but is it posible to use it with Arduino IDE? As I have to use the Arduino IDE and the file is in .c, which I can open with Visual Studio.
 
12:10:43.751 -> ---> AT+HTTPSSL=1
12:10:43.785 -> <--- ERROR

It looks like that may be an HTTPS redirect?
If the code you are using does not support HTTPS it could be the problem.

Try a site that does not enforce https:// access ?
 
rjenkinsgb yes, I have tried with an http but also the same error.
Anyway, this is what the code shows, that's why I thought I could try with https:
Code:
  // Optionally configure HTTP gets to follow redirects over SSL.
  // Default is not to follow SSL redirects, however if you uncomment
  // the following line then redirects over SSL will be followed.
  fona.setHTTPSRedirect(true);

If I comment that line, this is the response, maybe I'm not sending the data in a proper way.
Code:
20:58:04.286 -> FONA> NOTE: in beta! Use simple websites to post!
20:59:47.631 -> URL to post (e.g. httpbin.org/post):
20:59:47.631 -> http://www.irun.org/post
20:59:54.691 -> Data to post (e.g. "foo" or "{"simple":"json"}"):
20:59:57.034 -> password
20:59:57.034 -> ****
20:59:57.081 ->     ---> AT+HTTPTERM
20:59:57.081 ->     <--- OK
20:59:57.128 ->     ---> AT+HTTPINIT
20:59:57.128 ->     <--- OK
20:59:57.200 ->     ---> AT+HTTPPARA="CID"
20:59:57.200 ->     <--- OK
20:59:57.269 ->     ---> AT+HTTPPARA="UA"
20:59:57.305 ->     <--- OK
20:59:57.351 ->     ---> AT+HTTPPARA="URL"
20:59:57.398 ->     <--- OK
20:59:57.445 ->     ---> AT+HTTPPARA="CONTENT"
20:59:57.492 ->     <--- OK
20:59:57.539 ->     ---> AT+HTTPDATA=8,10000
20:59:57.586 ->     <--- DOWNLOAD
21:00:07.556 ->     <---
21:00:07.556 -> Failed!
21:00:07.556 ->
21:00:07.556 -> ****
21:00:07.589 ->     ---> AT+HTTPTERM
21:00:08.076 ->     <--- OK
21:00:08.076 -> FONA>

However, here goes the whole code if it helps.
Code:
#include "Adafruit_FONA.h" // https://github.com/botletics/SIM7000-LTE-Shield/tree/master/Code

// Define *one* of the following lines:
//#define SIMCOM_2G // SIM800/808/900/908, etc.
//#define SIMCOM_3G // SIM5320A/E
#define SIMCOM_7000 // SIM7000A/C/E/G
//#define SIMCOM_7500 // SIM7500A/E

// For SIM7000 shield with ESP32
#define FONA_PWRKEY 18
#define FONA_RST 5
#define FONA_TX 16 // ESP32 hardware serial RX2 (GPIO16)
#define FONA_RX 17 // ESP32 hardware serial TX2 (GPIO17)

// For ESP32 hardware serial
#include <HardwareSerial.h>
HardwareSerial fonaSS(1);

// Use this for 2G modules
#ifdef SIMCOM_2G
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

// Use this one for 3G modules
#elif defined(SIMCOM_3G)
Adafruit_FONA_3G fona = Adafruit_FONA_3G(FONA_RST);

// Use this one for LTE CAT-M/NB-IoT modules (like SIM7000)
// Notice how we don't include the reset pin because it's reserved for emergencies on the LTE module!
#elif defined(SIMCOM_7000) || defined(SIMCOM_7500)
Adafruit_FONA_LTE fona = Adafruit_FONA_LTE();
#endif

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
char replybuffer[255]; // this is a large buffer for replies
char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
void setup() {

  //  while (!Serial);

  pinMode(FONA_RST, OUTPUT);
  digitalWrite(FONA_RST, HIGH); // Default state

  pinMode(FONA_PWRKEY, OUTPUT);

  // Turn on the module by pulsing PWRKEY low for a little bit
  // This amount of time depends on the specific module that's used
  powerOn(); // See function definition at the very end of the sketch

  Serial.begin(115200);
  Serial.println(F("ESP32 Basic Test"));
  Serial.println(F("Initializing....(May take several seconds)"));

  // Note: The SIM7000A baud rate seems to reset after being power cycled (SIMCom firmware thing)
  // SIM7000 takes about 3s to turn on but SIM7500 takes about 15s
  // Press reset button if the module is still turning on and the board doesn't find it.
  // When the module is on it should communicate right after pressing reset
 
  // Start at default SIM7000 shield baud rate
  fonaSS.begin(9600, SERIAL_8N1, FONA_TX, FONA_RX); // baud rate, protocol, ESP32 RX pin, ESP32 TX pin

  Serial.println(F("Configuring to 9600 baud"));
  fonaSS.println("AT+IPR=9600"); // Set baud rate
  delay(100); // Short pause to let the command run
  fonaSS.begin(9600, SERIAL_8N1, FONA_TX, FONA_RX); // Switch to 9600
  if (! fona.begin(fonaSS)) {
    Serial.println(F("Couldn't find FONA"));
    while (1); // Don't proceed if it couldn't find the device
  }

  type = fona.type();
  Serial.println(F("FONA is OK"));
  Serial.print(F("Found "));
  switch (type) {
    case SIM800L:
      Serial.println(F("SIM800L")); break;
    case SIM800H:
      Serial.println(F("SIM800H")); break;
    case SIM808_V1:
      Serial.println(F("SIM808 (v1)")); break;
    case SIM808_V2:
      Serial.println(F("SIM808 (v2)")); break;
    case SIM5320A:
      Serial.println(F("SIM5320A (American)")); break;
    case SIM5320E:
      Serial.println(F("SIM5320E (European)")); break;
    case SIM7000A:
      Serial.println(F("SIM7000A (American)")); break;
    case SIM7000C:
      Serial.println(F("SIM7000C (Chinese)")); break;
    case SIM7000E:
      Serial.println(F("SIM7000E (European)")); break;
    case SIM7000G:
      Serial.println(F("SIM7000G (Global)")); break;
    case SIM7500A:
      Serial.println(F("SIM7500A (American)")); break;
    case SIM7500E:
      Serial.println(F("SIM7500E (European)")); break;
    default:
      Serial.println(F("???")); break;
  }

  // Print module IMEI number.
  uint8_t imeiLen = fona.getIMEI(imei);
  if (imeiLen > 0) {
    Serial.print("Module IMEI: "); Serial.println(imei);
  }

  // Set modem to full functionality
  fona.setFunctionality(1); // AT+CFUN=1

  // Configure a GPRS APN, username, and password.
  // You might need to do this to access your network's GPRS/data
  // network.  Contact your provider for the exact APN, username,
  // and password values.  Username and password are optional and
  // can be removed, but APN is required.
  fona.setNetworkSettings(F("orangeworld"), F("orange"), F(""));
  //fona.setNetworkSettings(F("m2m.com.attz")); // For AT&T IoT SIM card
  //fona.setNetworkSettings(F("telstra.internet")); // For Telstra (Australia) SIM card - CAT-M1 (Band 28)
 // fona.setNetworkSettings(F("hologram")); // For Hologram SIM card

  // Optionally configure HTTP gets to follow redirects over SSL.
  // Default is not to follow SSL redirects, however if you uncomment
  // the following line then redirects over SSL will be followed.
  fona.setHTTPSRedirect(true);

}

void loop() {

 Serial.print(F("FONA> "));
 
  while (! Serial.available() ) {
    if (fona.available()) {
      Serial.write(fona.read());
    }
  }


          // Post data to website
  
        uint16_t statuscode;
        int16_t length;
        char url[80];
        char data[80];


        flushSerial();
        Serial.println(F("NOTE: in beta! Use simple websites to post!"));
        Serial.println(F("URL to post (e.g. httpbin.org/post):"));
        Serial.print(F("http://")); readline(url, 79);
        Serial.println(url);
        Serial.println(F("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):"));
        readline(data, 79);
        Serial.println(data);

        Serial.println(F("****"));
        if (!fona.HTTP_POST_start(url, F("text/plain"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
          Serial.println("Failed!");

        }
        while (length > 0) {
          while (fona.available()) {
            char c = fona.read();

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
            loop_until_bit_is_set(UCSR0A, UDRE0); // Wait until data register empty.
            UDR0 = c;
#else
            Serial.write(c);
#endif

            length--;
            if (! length) break;
          }
        }
        Serial.println(F("\n****"));
        fona.HTTP_POST_end();
        

      
// flush input
  flushSerial();
  while (fona.available()) {
    Serial.write(fona.read());
  }
 
}

 void flushSerial() {
  while (Serial.available())
    Serial.read();
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}
uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
  uint16_t buffidx = 0;
  boolean timeoutvalid = true;
  if (timeout == 0) timeoutvalid = false;

  while (true) {
    if (buffidx > maxbuff) {
      //Serial.println(F("SPACE"));
      break;
    }

    while (Serial.available()) {
      char c =  Serial.read();

      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0)   // the first 0x0A is ignored
          continue;

        timeout = 0;         // the second 0x0A is the end of the line
        timeoutvalid = true;
        break;
      }
      buff[buffidx] = c;
      buffidx++;
    }

    if (timeoutvalid && timeout == 0) {
      //Serial.println(F("TIMEOUT"));
      break;
    }
    delay(1);
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}

// Power on the module
void powerOn() {
  digitalWrite(FONA_PWRKEY, LOW);
  // See spec sheets for your particular module
#if defined(SIMCOM_2G)
  delay(1050);
#elif defined(SIMCOM_3G)
  delay(180); // For SIM5320
#elif defined(SIMCOM_7000)
  delay(100); // For SIM7000
#elif defined(SIMCOM_7500)
  delay(500); // For SIM7500
#endif

  digitalWrite(FONA_PWRKEY, HIGH);
}
 
Yes you can use it in arduino ide, I have, it comes in the esp32 core.
However you need to do some basic mods to make a .c file work, if thats what your working with.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top