0

[WORKING] Code for FONA 3G HTTP GET requests
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

[WORKING] Code for FONA 3G HTTP GET requests

by EPD on Fri Feb 19, 2021 7:05 pm

ive had the Adafruit FONA 3G Cellular Breakout for a little while, and finally found a way to make HTTP GET Requests, all the other code i found didnt seem to work, and not sure why, so went to work and pieced this code together based on https://simcom.ee/documents/SIM5320/sim ... _v0.02.pdf and https://cdn-learn.adafruit.com/download ... 1613768661 datasheets.

i hope this code will help others who have faced the same frustrations as i have in enabling 3G interfaces on my projects.

i have attached a copy of the .ino file aswell for ease of use.


[EDIT] I realised that I only talked about the SIM5320E (European) Version of the module as that's the one I've used being in the UK, as far as I'm aware the code should still work on the SIM5320A for all American users

Code: Select all | TOGGLE FULL SIZE
/* Experimentally derived code for making HTTP GET requests from SIM5320E module in Adafruit 3G Module https://www.adafruit.com/product/2691
 *
 * Disclamer:
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-NESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
 *
 * CONNECTIONS:
 * Arduino D2 <-> Fona RX
 * Arduino D3 <-> Fona TX
 * Arduino D4 <-> Fona RST
 *
 * Produced by Ed PD, any modifications are welcome,
 * this code is in the public domain and free to be modified in any way.
 *
 */

 
#include <SoftwareSerial.h>
SoftwareSerial softSerial(3,2);

const int resetpin = 4;
String line = "";
String incoming = "";

void simple_send_to_fona(String command){
  for(i=0; i<command.length(); i++){
    softSerial.write(command.charAt(i));
  } //writes the command to the SIM5320E over the softSerial char by char
  softSerial.write(13);
  softSerial.write(10); //send CR and LF to SIM5320E to finalise that command
  while(!softSerial.available()){
    delay(1);
  } //wait for response from SIM5320E
  Serial.print("("); Serial.print(command); Serial.println(")"); //debugging, shows what command was sent on the serial monitor
  while(softSerial.available()){
    incoming = softSerial.readString();
    Serial.print(incoming);
  } //while softSerial available from SIM5320E, print the data to serial monitor
  Serial.println("");
  Serial.println("");
}

void setup() {
  pinMode(resetpin, OUTPUT);
  digitalWrite(resetpin, HIGH);
  Serial.begin(9600);
  softSerial.begin(4800);

  Serial.println("--- START: CONFIGURE NETWORK CONNECTION ---");
 
  simple_send_to_fona("AT+CHTTPSCLSE"); //Debugging Step, future commands can fail if the last http connection is still active/open (AT+CHTTPSSTATE response 4). if returns 'ERROR', this is okay, ideally will return 'OK'
  simple_send_to_fona("AT+NETCLOSE"); //Debugging Step, future commands can fail by trying to configure the network while the network is still active. this will respond 'OK' when no network sockets are active, hence using AT+CHTTPSCLSE first
  simple_send_to_fona("AT+CGDCONT=1,\"IP\",\"prepay.tesco-mobile.com\",\"0.0.0.0\""); //First required command, sets up the mobile connection APN etc. I am using a SIM Card from Tesco (in UK) but you need to replace 'prepay.tesco-mobile.com' with the ASN of your 3G SIM provider
  simple_send_to_fona("AT+CGSOCKCONT=1,\"IP\",\"prepay.tesco-mobile.com\""); //simular command to last, again replace 'prepay.tesco-mobile.com' with the ASN of your 3G SIM provider
  simple_send_to_fona("AT+CSOCKSETPN=1"); //sets a profile number on the SIM5320E, i dont know exactly what it does but i seem to need it
  simple_send_to_fona("AT+CIPMODE=0"); //dont know exactly what it does but sets the tcp/ip configuration of SIM5320E
  simple_send_to_fona("AT+NETOPEN=,,1"); // creates a netwoek instance with your 3G provider
  simple_send_to_fona("AT+IPADDR"); //Debugging step, not required but says what the IP address of current active socket is
  simple_send_to_fona("AT+CHTTPSSTOP"); //stops any active http sockets, not neccicary but it prevents unexpected behaviour later
  simple_send_to_fona("AT+CHTTPSSTART"); //starts a new http socket
 
  Serial.println("--- FINISH: CONFIGURE NETWORK CONNECTION ---");
}

void loop() { 
  Serial.println("--- REQUEST STARTED ---");
 
  simple_send_to_fona("AT+CHTTPACT= \"www.yoururl.com\",80"); //start a HTTP request, on port 80 (HTTP ONLY) can use 443 for HTTPS, however untested. replace www.yoururl.com with your desired URL, without 'http(s)://' preceeding and no following '/' or webpages etc
  simple_send_to_fona("GET /dir_1/file.php HTTP/1.1"); // select the webpage on webserver to make get request, using the provided format, any directory&file structure can be requested. if using HTTPS you need to investigate what to do with the 'HTTP/1.1' as i have not tested this
  simple_send_to_fona("Host: www.yoururl.com"); // first header for GET request, same url as 'AT+CHTTPACT' command
  simple_send_to_fona("User-Agent: Mozilla/5.0 (Linux; Android 10; HD1911) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.90 Mobile Safari/537.36"); //second header, choose your user agent, can use this one or find a new one from somewhere like https://developers.whatismybrowser.com/useragents/explore/ , keeping 'User-Agent: ' at the beginning
  for(i=0; i<("Content-Length: 0").length(); i++){ //third header, not sure what it does but is required and should not be modified
    softSerial.write(("Content-Length: 0").charAt(i));
  } //sends command char by char
  softSerial.write(13);
  softSerial.write(10);
  softSerial.write(13);
  softSerial.write(10); //send two sets of CR and LF to SIM5320E to finalise the whole AT+CHTTPACT command
 
  softSerial.write(0x1A); //send CTRL+Z character to SIM5320E to make request to webserver
  Serial.println("--- REQUEST MADE ---");
  Serial.println("--- PRINTING RETURNED DATA ---");

  String inputline = "";
  while(inputline.indexOf("+CHTTPACT: 0") == -1){ // debugging step to prevent the Arduino for endlessly waiting after the last command is recieved
    if(softSerial.available()){
      inputline = softSerial.readString();
      Serial.println(inputline); //you should get lots of data here including the data you requested from the web server
    }
  }
 
  while(true){} //prevent the requests from being endlessly generated (dont want to waste my credit :) )
 
 
}


Enjoy,
Ed PD
Attachments
EXPORT_SIM5320E_HTTP_GET_REQUESTS.ino
ino file for posted code
(6.05 KiB) Downloaded 2 times

EPD
 
Posts: 6
Joined: Fri Feb 19, 2021 6:01 pm

Please be positive and constructive with your questions and comments.