ESP 8266 & ARDUINO + WEBSERVER
Posted: Thu Apr 13, 2017 12:58 pm
Hai guys. I am totally beginner in Arduino programming. I need a small help from all expertise here. I need to develop a web server so by using it I can control on and off a water pump. I have no idea how it can be done. I have tried many methods but still, fail. Currently, i just have this code.
I hope i will get idea in problem. I use above code to send data from arduino to database...
thank you in advanced for help.
Code: Select all
/*
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
*
* You need to get streamId and privateKey at data.sparkfun.com and paste them
* below. Or just customize this script to talk to other HTTP servers.
*
*/
#include <ESP8266WiFi.h>
//char* ssid = "";
//char* password = "";
//char* host = "";
String ssid ="";
String password ="";
String host ="";
String value = "";
String value1 = "";
String value2 = "";
String value3 = "";
void setup() {
Serial.begin(115200);
delay(5000);
Serial.println("Enter ssid: ");
// get ssid
while(Serial.available() == 0){
}
if(Serial.available() > 0)
{
value1 = Serial.readStringUntil('\n');
}
Serial.println(value1);
ssid = value1;
//char _ssid[value1.length()+1];
//value1.toCharArray(ssid,sizeof(_ssid));
Serial.println("Enter password: ");
// get password
while(Serial.available() == 0){
}
if(Serial.available() > 0)
{
value2 = Serial.readStringUntil('\n');
}
Serial.println(value2);
password = value2;
//char _password[value2.length()+1];
//value2.toCharArray(password,sizeof(_password));
Serial.println("Enter host ip: ");
// get host
while(Serial.available() == 0){
}
if(Serial.available() > 0)
{
value3 = Serial.readStringUntil('\n');
}
Serial.println(value3);
host = value3;
//char _host[value3.length()+1];
//value3.toCharArray(host,sizeof(_host));
Serial.println();
Serial.println("Connecting to ");
Serial.println(ssid);
Serial.println(password);
ssid.replace("\n", "");
password.replace("\n", "");
host.replace("\n", "");
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("WiFi connected");
//Serial.println("IP address: ");
//Serial.println(WiFi.localIP());
}
void loop() {
delay(5000);
while(Serial.available() == 0){}
if(Serial.available() > 0)
{
value = Serial.readStringUntil('\n');
}
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host.c_str(), httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "http:///htdocs/mytree/add.php?";
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
//Serial.println("closing connection");
}
I hope i will get idea in problem. I use above code to send data from arduino to database...
thank you in advanced for help.