Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
burakkorman opened this issue Jan 29, 2020 · 8 comments

Comments

@burakkorman
Copy link

burakkorman commented Jan 29, 2020

The code I wrote:

`#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

#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
Copy link

hoerup commented Jan 29, 2020

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
Copy link
Author

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

@hoerup
Copy link

hoerup commented Jan 29, 2020

Could you paste your updated code

@burakkorman
Copy link
Author

` #include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

#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
Copy link

hoerup commented Jan 29, 2020

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

@burakkorman
Copy link
Author

Can you share sample code with me?

@hoerup
Copy link

hoerup commented Jan 30, 2020

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

@Fonfon60
Copy link

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

<title>301 Moved Permanently</title>

301 Moved Permanently


nginx

AT+CIPSTATUS
AT+CIPSHUT

So do you have a solution, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants