-->
Page 4 of 8

Re: WiFiClient example

PostPosted: Wed Aug 19, 2015 2:53 pm
by martinayotte
torntrousers wrote:The perils of just typing code on the fly...

:D

Re: WiFiClient example

PostPosted: Wed Aug 19, 2015 2:55 pm
by lajolo
It works, thanks!

Re: WiFiClient example

PostPosted: Fri Aug 28, 2015 10:28 pm
by Torah
Hi, lajolo

I changed your code for my use in the following way. But failed many times to send GET request. I can see that esp8266 connected wifi. BTW - not a module or home network issue. I've ran some piece of code doing the same stuff but using httpd. So from your experience, can you help out of this strange problem? Merci beaucoup.

the changed code is here:

// Import required libraries
#include <ESP8266WiFi.h>

// WiFi parameters
const char* ssid = "TorahYongquan";
const char* password = "PlantoWang";

// Host
const char* host = "planto.oicp.net";

void setup() {
// Start Serial
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {

Serial.print("Connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

// This will send the request to the server
client.print(String("GET /add.php?temper=22") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);

Serial.print("client.status(): ");
Serial.println(client.status());
Serial.print("client.available(): ");
Serial.println(client.available());
Serial.print("client.connected(): ");
Serial.println(client.connected());

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r\n');
Serial.print(line);
}

Serial.println();
Serial.println("closing connection");
delay(5000);

}




lajolo wrote:That seems to work fine, thanks!

And I have left '\r' here:

while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Re: WiFiClient example

PostPosted: Sat Aug 29, 2015 6:04 am
by lajolo
Hello,

in your code you want to access the website:
http://planto.oicp.net/add.php?temper=22

From my browser I cannot access it.

Are you sure that the address is correct?

Can you access it from your browser?