DFRobot / DFRobot_SIM808

SIM808
MIT License
69 stars 56 forks source link

I tried SIM808_TCPConnection example. But "Hello World!" did not return. #28

Open burakkorman opened 4 years ago

burakkorman commented 4 years ago

The code I wrote:

`#include

include

define PIN_TX 10

define PIN_RX 11

SoftwareSerial mySerial(PIN_TX,PIN_RX); DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; char buffer[512];

void setup(){ mySerial.begin(9600); Serial.begin(9600);

//**** Initialize sim808 module ***** while(!sim808.init()) { delay(1000); Serial.print("Sim808 init error\r\n"); } delay(3000);

//* Attempt DHCP ***** while(!sim808.join(F("cmnet"))) { Serial.println("Sim808 join network error"); delay(2000); }

//**** Successful DHCP **** Serial.print("IP Address is "); Serial.println(sim808.getIPAddress());

//*** Establish a TCP connection **** if(!sim808.connect(TCP,"mbed.org", 80)) { Serial.println("Connect error"); }else{ Serial.println("Connect mbed.org success"); }

//* Send a GET request *** Serial.println("waiting to fetch..."); sim808.send(http_cmd, sizeof(http_cmd)-1); while (true) { int ret = sim808.recv(buffer, sizeof(buffer)-1); if (ret <= 0){ Serial.println("fetch over..."); break; } buffer[ret] = '\0'; Serial.print("Recv: "); Serial.print(ret); Serial.print(" bytes: "); Serial.println(buffer); break; }

//***** Close TCP or UDP connections ** sim808.close();

//* Disconnect wireless connection, Close Moving Scene ***** sim808.disconnect(); }

void loop(){

}`

But I get such a result : Screenshot_1`

hoerup commented 4 years ago

It doesn't seems that mbed.org likes HTTP 1.0 (which really shouldn't come as a surprise) Try sending it as a http 1.1 request, and remember to send the host header

burakkorman commented 4 years ago

@hoerup I tried, but I get a 400 (Bad Request) error. Screenshot_2

hoerup commented 4 years ago

Could you paste your updated code

burakkorman commented 4 years ago

` #include

include

define PIN_TX 10

define PIN_RX 11

SoftwareSerial mySerial(PIN_TX,PIN_RX); DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.1\r\n\r\n"; char buffer2[512];

void setup(){ mySerial.begin(9600); Serial.begin(9600);

//******** Initialize sim808 module *************
while(!sim808.init()) {
    delay(1000);
    Serial.print("Sim808 init error\r\n");
}
delay(3000);

//*********** Attempt DHCP *******************
while(!sim808.join(F("cmnet"))) {
    Serial.println("Sim808 join network error");
    delay(2000);
}

//************ Successful DHCP ****************
Serial.print("IP Address is ");
Serial.println(sim808.getIPAddress());

//*********** Establish a TCP connection ************
if(!sim808.connect(TCP,"mbed.org", 80)) {
    Serial.println("Connect error");
}else{
    Serial.println("Connect mbed.org success");
}

//*********** Send a GET request *****************
Serial.println("waiting to fetch...");
sim808.send(http_cmd, sizeof(http_cmd)-1);
while (true) {
    int ret = sim808.recv(buffer2, sizeof(buffer2)-1);
    if (ret <= 0){
        Serial.println("fetch over...");
        break;
    }
    buffer2[ret] = '\0';
    Serial.print("Recv: ");
    Serial.print(ret);
    Serial.println(" bytes: ");
    Serial.println(buffer2);
    break;
}

//************* Close TCP or UDP connections **********
sim808.close();

//*** Disconnect wireless connection, Close Moving Scene *******
sim808.disconnect();

}

void loop(){

}`

hoerup commented 4 years ago

in my previous comment i did write "remember to send the host header" which is a mandatory header in http 1.1

burakkorman commented 4 years ago

Can you share sample code with me?

hoerup commented 4 years ago

char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.1\r\nHost:mbed.org\r\n\r\n";

Fonfon60 commented 2 years ago

I tried with the last comment and the result was: AT AT+CFUN=1 AT+CPIN? AT+CSTT="sl2sfr","","" AT+CIICR AT+CIFSR IP Address is 100.75.179.118 AT+CIPSTART="TCP","mbed.org",80 Connect mbed.org success waiting to fetch... AT+CIPSEND=70 GET /media/uploads/mbed_official/hello.txt HTTP/1.1 Host:mbed.org

Recv: 442 bytes: HTTP/1.1 301 Moved Permanently Date: Tue, 21 Dec 2021 12:16:41 GMT Content-Type: text/html Content-Length: 162 Connection: keep-alive Location: https://mbed.org/media/uploads/mbed_official/hello.txt Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

301 Moved Permanently

301 Moved Permanently


nginx

AT+CIPSTATUS AT+CIPSHUT

So do you have a solution, please?