9 Replies Latest reply on May 31, 2017 8:17 AM by Intel Corporation

    How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2

    geonyt

      Hello, I am trying to GET the value/s from the web using SIM5218E 3G shield with Intel GalGen2 and send it thru SMS. Do you have any documents or info regarding this matter? This is my only problem for it to work (I think, for now ).

       

      Thank you in advance.

        • 1. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
          FGT

          Hi geonyt,

           

          I don't have any 3G shield to replicate what you are trying to get. However, AT commands usually follow a fairly simple request-repy text protocol (I've used it in the ESP8266 wifi module, e.g. Wi-Fi module ESP8266 in Galileo). I',m assuming from a previous thread (How to send AT command using intel galileo gen 2 with SIM5218E 3G shield) you have the 3G shield "Up&Running".

           

          Maybe if you post your current code someone would be able to help.

           

          BR,

           

          Fernando.

          • 2. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
            geonyt

            Hi @FGT,

             

            Yes. AT command works from my previous post. But, I'm working on HTTP GET which is not on the sample manual. I've tried replicating the arduino HTTP GET but no luck. Maybe I don't know when to use Serial and Serial1 for Intel Galileo.

            • 3. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
              FGT

              geonyt wrote:

               

              Hi @FGT,

               

              Yes. AT command works from my previous post. But, I'm working on HTTP GET which is not on the sample manual. I've tried replicating the arduino HTTP GET but no luck. Maybe I don't know when to use Serial and Serial1 for Intel Galileo.

              I see... please send the code you are currently trying to get it working... were you able to run the example in the document pointed out by Sergio in the other thread (https://communities.intel.com/external-link.jspa?url=http%3A%2F%2Fwww.intel.com%2Fcontent%2Fwww%2Fus%2Fen%2Fsupport%2Fbo… )? More specifically, did you manage to use GPRS/IP setting?

               

              Fernando.

              • 4. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
                geonyt

                Hello FGT, these are the raw codes that I am working and of course changing the asterisks fields with my data. I added Serial1.begin(115200);  under setup and changing all Serial to Serial1 and no luck unlike from the GPRS example.

                 

                RAW Codes from 3G/GPRS shield over Arduino and Raspberry Pi Section 15 HTTP and HTTPS Arduino

                //Change here your data
                const char pin_number[] = "****";
                const char apn[] = "*********";
                const char user_name[] = "*********";
                const char password[] = "*********";
                
                char url[ ]="test.libelium.com";
                int port= 80;
                char request[ ]="GET /test-get-post.php?a=1&b=2 HTTP/1.1\r\nHost: test.libelium.com\r\nContent-Length: 0\r\n\r\n";
                
                
                int8_t answer;
                int onModulePin = 2, aux;
                int data_size = 0;
                int end_file = 0;
                char aux_str[50];
                char data[400];
                int http_x;
                int http_status; // 0: end, 1 waiting data with header, 2 waiting data without header, 3 timeout waiting data, 4 unknow response
                int x = 0;
                long previous;
                
                void setup(){
                  pinMode(onModulePin, OUTPUT);
                  Serial.begin(115200);
                
                  Serial.println("Starting...");
                  power_on();
                
                  delay(3000);
                
                  //sets the PIN code
                  sprintf(aux_str, "AT+CPIN=%s", pin_number);
                  sendATcommand(aux_str, "OK", 2000);
                
                  delay(3000);
                
                  while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
                  sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
                
                  // sets APN, user name and password
                  sprintf(aux_str, "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
                  sendATcommand(aux_str, "OK", 2000);
                
                  sprintf(aux_str, "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
                  sendATcommand(aux_str, "OK", 2000);
                }
                
                void loop(){
                
                  // request the url
                  
                  previous = millis();
                  sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port);
                  answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", 60000);
                
                  // Sends the request
                  Serial.println(request);
                  // Sends <Ctrl+Z>
                  Serial.write(0x1A);
                  http_status = 1;
                
                  while ((http_status == 1) || (http_status == 2))
                  {
                  answer = sendATcommand("", "+CHTTPACT: ", 60000);
                   if (answer == 0)
                  {
                   if (http_status == 1)
                  {
                  http_status = 3;
                  }
                   if (http_status == 2)
                  {
                  http_status = 5;
                  }
                  }
                   else
                  {
                   // answer == 1
                   while(Serial.available()==0);
                  aux_str[0] = Serial.read();
                   
                   if ((aux_str[0] == 'D') && (http_status == 1))
                  {
                   // Data packet with header
                   while(Serial.available()<4);
                  Serial.read();  // A
                  Serial.read();  // T
                  Serial.read();  // A
                  Serial.read();  // ,
                
                   // Reads the packet size
                  x=0;
                   do{
                   while(Serial.available()==0);
                  aux_str[x] = Serial.read();
                  x++;
                  }
                   while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));
                
                  aux_str[x-1] = '\0';
                  data_size = atoi(aux_str);
                
                   // Now, search the end of the HTTP header (\r\n\r\n)
                   do{
                   while (Serial.available() < 3);
                
                  data_size--;
                   if (Serial.read() == '\r')
                  {
                  data_size--;
                   if (Serial.read() == '\n')
                  {
                  data_size--;
                   if (Serial.read() == '\r')
                  {
                  data_size--;
                   if (Serial.read() == '\n')
                  {
                   // End of the header found
                  http_status = 2;
                  }
                  }
                  }
                  }
                  }
                   while ((http_status == 1) && (data_size > 0));
                
                   if (http_status == 2)
                  {
                   // Reads the data
                  http_x = 0;
                   do{
                   if(Serial.available() != 0){
                  data[http_x] = Serial.read();
                  http_x++;
                  data_size--;
                  }
                   else
                  {
                  delay(1);
                  }
                  }
                   while(data_size > 0);
                  data[http_x] = '\0';
                  }
                  }
                   else if ((aux_str[0] == 'D') && (http_status == 2))
                   {
                   // Data packet with header
                   while(Serial.available()<4);
                  Serial.read();  // A
                  Serial.read();  // T
                  Serial.read();  // A
                  Serial.read();  // ,
                
                   // Reads the packet size
                  x=0;
                   do{
                   while(Serial.available()==0);
                  aux_str[x] = Serial.read();
                  x++;
                  }
                   while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));
                
                  aux_str[x-1] = '\0';
                  data_size = atoi(aux_str);
                
                   do{
                   if(Serial.available() != 0){
                  data[http_x] = Serial.read();
                  http_x++;
                  }
                   else
                  {
                  delay(1);
                  }
                  }
                   while(data_size > 0);
                  data[http_x] = '\0';
                
                  }
                   else if (aux_str[0] == '0')
                   {
                   // end of the AT command
                  http_status = 0;
                  }
                   else
                  {
                   // unknow response
                  http_status = 4;
                  Serial.print(char(aux_str[0]));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  Serial.print(char(Serial.read()));
                  }
                  }
                  }
                
                  previous = millis() - previous;
                  
                  Serial.println(previous, DEC);
                  if (http_status == 0)
                  {
                  Serial.print(F("HTTP data: "));
                  Serial.println(data);
                  }
                  else
                  {
                  Serial.print(F("http_status: "));
                  Serial.println(http_status, DEC);
                  }
                
                
                
                  delay(10000);
                
                }
                
                void power_on(){
                
                  uint8_t answer=0;
                
                  // checks if the module is started
                  answer = sendATcommand("AT", "OK", 2000);
                  if (answer == 0)
                  {
                   // power on pulse
                  digitalWrite(onModulePin,HIGH);
                  delay(3000);
                  digitalWrite(onModulePin,LOW);
                
                   // waits for an answer from the module
                   while(answer == 0){
                   // Send AT every two seconds and wait for the answer
                  answer = sendATcommand("AT", "OK", 2000);
                  }
                  }
                
                }
                
                
                int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) {
                
                  uint8_t x = 0, answer = 0;
                  char response[100];
                  unsigned long previous;
                
                  memset(response, '\0', 100);  // Initialize the string
                
                  delay(100);
                
                  while ( Serial.available() > 0) Serial.read();  // Clean the input buffer
                
                  Serial.println(ATcommand);  // Send the AT command
                
                
                  x = 0;
                  previous = millis();
                
                  // this loop waits for the answer
                  do {
                   // if there are data in the UART input buffer, reads it and checks for the asnwer
                   if (Serial.available() != 0) {
                  response[x] = Serial.read();
                  x++;
                   // check if the desired answer is in the response of the module
                   if (strstr(response, expected_answer1) != NULL)
                  {
                  answer = 1;
                  }
                  }
                   // Waits for the asnwer with time out
                  }
                  while ((answer == 0) && ((millis() - previous) < timeout));
                
                  return answer;
                }
                
                int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout)
                {
                
                  uint8_t x=0, answer=0;
                  char response[100];
                  unsigned long previous;
                
                  memset(response, '\0', 100);  // Initialize the string
                
                  delay(100);
                
                 // while( Serial.available() > 0) Serial.read(); // Clean the input buffer
                
                  Serial.println(ATcommand);  // Send the AT command
                
                
                  x = 0;
                  previous = millis();
                
                  // this loop waits for the answer
                  do{
                
                   if(Serial.available() != 0){
                  response[x] = Serial.read();
                  x++;
                   // check if the desired answer is in the response of the module
                   if (strstr(response, expected_answer1) != NULL)
                  {
                  answer = 1;
                  }
                   // check if the desired answer is in the response of the module
                   if (strstr(response, expected_answer2) != NULL)
                  {
                  answer = 2;
                  }
                  }
                   // Waits for the asnwer with time out
                  }
                  while((answer == 0) && ((millis() - previous) < timeout));
                
                  return answer;
                }
                

                GPRS from https://communities.intel.com/external-link.jspa?url=http%3A%2F%2Fwww.intel.com%2Fcontent%2Fwww%2Fus%2Fen%2Fsupport%2Fbo… 

                 

                Serial1.println("AT+CPIN?"); // Is SIM ready to use?
                delay(1000);
                ShowSerialData();
                Serial1.println("AT+CGREG?"); // Is device registered?
                delay(1000);
                ShowSerialData();
                Serial1.println("AT+COPS?"); // Does SIM info match network?
                delay(1000);
                ShowSerialData();
                Serial.println("Check signal quality");
                Serial1.println("AT+CSQ"); // Check signal quality
                delay(1000);
                ShowSerialData();
                Serial1.println("AT+cgatt=1"); // GPRS attach
                delay(1000);
                ShowSerialData();
                // define a PDP context with IP connection, ID is 1
                Serial1.println("AT+CGDCONT=1,\"IP\",\"apn\"");
                delay(1000);
                ShowSerialData();
                
                // list PDP contexts that are defined
                Serial1.println("at+cgdcont?");
                delay(3000);
                ShowSerialData();
                // setup the session using the appropriate PDP context
                Serial1.println("AT+CGACT=1,1");
                delay(1000);
                ShowSerialData();
                Serial.println("session is setup delay 5 seconds");
                delay(5000);
                // deactivate the PDP context
                Serial1.println("AT+CGACT=0,1");
                delay(1000);
                ShowSerialData();
                // detach from GPRS newtork
                Serial1.println("AT+CGATT=0");
                delay(1000);
                ShowSerialData();
                
                • 5. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
                  FGT

                  The contents and code at

                  geonyt wrote:

                  ...

                  RAW Codes from 3G/GPRS shield over Arduino and Raspberry Pi Section 15 HTTP and HTTPS Arduino...

                  seem to be interesting and useful, but right now I think they only add "noise". Please refer to my previous question:

                  did you manage to use GPRS/IP setting?

                  Just in case: use the code at section 22 of Shield Testing Report for Intel® Galileo Board, Intel® Galileo Gen 2... specifically about GPSR/IP setting and send the result, so maybe we'll be able to figure out the steps to follow and maybe using the code at 3G/GPRS shield over Arduino and Raspberry Pi.

                   

                  HTH,

                   

                  Fernando.

                  • 6. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
                    Intel Corporation
                    This message was posted on behalf of Intel Corporation

                    Hello geonyt,

                    Do you have any updates about this? I agree with Fernando's last reply. Have you tried his suggestion? Let us know if you still need help and we'll try to assist you.

                    Pedro M.

                    • 7. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
                      geonyt

                      Hello, I was able to make it work using section 22 and some modifications. Now, I'm working on sending SMS to multiple recipients. I think this is another thread.

                       

                      Thank you for the help FGT and Intel Corp.

                      • 8. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
                        FGT

                        geonyt wrote:

                         

                        Hello, I was able to make it work using section 22 and some modifications. Now, I'm working on sending SMS to multiple recipients. I think this is another thread.

                         

                        Thank you for the help FGT and Intel Corp.

                        Great, and you are welcome,

                         

                        Fernando.

                        • 9. Re: How to HTTP GET and POST using 3G shield with Intel Galileo Gen 2
                          Intel Corporation
                          This message was posted on behalf of Intel Corporation

                          Hi geonyt,

                           

                          That's great to know. Thanks for the keeping us informed.

                           

                          If you have further questions please don't hesitate to ask in the community. We are here to help.

                           

                          Have a nice day.

                           

                          Regards,
                          Diego V.