0

I have the following code that can read a web page using HTTP get but I can only do it because the website I am doing it to has a static IP address, but in my project I am just hosting it on a free service and IP changes rapidly. So is there a way to do the same with only the URL address?

#include <SoftwareSerial.h>

SoftwareSerial cell(2,3);  // Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin. connect this to the GSM module

const String apn = "web2"; // access-point name for GPRS

const String ip = "212.26.27.181"; // IP address of server we're connecting to
const String host = "kfu.edu.sa"; // required in HTTP 1.1 - what's the name of the host at this IP address?
const String request = "GET /sites/Home HTTP/1.1";
const String useragent = "Mozilla/5.0"; // for our purposes the user agent doesn't matter - if I understand correctly it's helpful to use something generic the server will recognize


void setup()
{
  //Initialize serial ports for communication with computer
  Serial.begin(9600);

  Serial.println("Starting SM5100B Communication...");
  cell.begin(9600);

  waitTil("+SIND: 4"); // keep printing cell output til we get "+SIND: 4"
  Serial.println("Module ready");
}

void loop()
{
  Serial.println("Attaching GPRS...");
  cell.println("AT+CGATT=1");
  waitFor("OK");

  Serial.println("Setting up PDP Context...");
  cell.println("AT+CGDCONT=1,\"IP\",\""+apn+"\"");
  waitFor("OK");

  Serial.println("Activating PDP Context...");
  cell.println("AT+CGACT=1,1");
  waitFor("OK");

  Serial.println("Configuring TCP connection to TCP Server...");
  cell.println("AT+SDATACONF=1,\"TCP\",\""+ip+"\",80");
  waitFor("OK");

  Serial.println("Starting TCP Connection...");
  cell.println("AT+SDATASTART=1,1");
  waitFor("OK");

  delay(5000); // wait for the socket to connect
  • have you tried using a URL in place of IP? ....... ip="google.com"; cell.println("AT+SDATACONF=1,\"TCP\",\""+ip+"\",80"); – jsotola Mar 1 at 0:51
  • Yes, but it does not give "OK" back. – Salman Sayah Mar 1 at 0:52
  • you get absolutely nothing back? – jsotola Mar 1 at 0:54
  • the method getMessage() gives "⸮⸮⸮⸮⸮" – Salman Sayah Mar 1 at 1:06
  • what are the bytes in the returned string? – jsotola Mar 1 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.