Fetching data from server | Arduino/ESP8266

Arduino on ESP8266

Arduino/ESP8266Arduino/ESP8266: Fetching data to Server (LOCAL and Online) using WifiClient on from the ESP8266 module using Arduino IDE.

The chip first came to the attention of western makers in August 2014 with the ESP-01 module, made by a third-party manufacturer, AI-Thinker. This small module allows microcontrollers to connect to a Wi-Fi network and make simple TCP/IP connections using Hayes-style commands. However, at the time there was almost no English-language documentation on the chip and the commands it accepted.The very low price and the fact that there were very few external components on the module which suggests that it could eventually be very inexpensive in volume, attracted many hackers to explore the module, chip, and the software on it, as well as to translate the Chinese documentation.
Source: Wikipedia (bit.ly/2cORmj0)

 

Links :

Sketch: bit.ly/2giOJtU

#include <ESP8266WiFi.h>

const char* ssid     = "oksbwn-world"; //Wi-Fi SSID
const char* password = "omisoksbwn"; //Wi-Fi Password

//Only used if using Static IP
IPAddress ip(192, 168, 0, 6); //IP
IPAddress gatewayDNS(192, 168, 0, 1);//DNS and Gewateway
IPAddress netmask(255, 255, 255,0); //Netmask

//Server IP or domain name
const char* host = "192.168.0.1";

void setup() {
  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.config(ip,gatewayDNS,netmask,gatewayDNS); //Only used if using Static IP 
  WiFi.begin(ssid, password); //Connecting to the network
  
  while (WiFi.status() != WL_CONNECTED) { //Wait till connects
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  //Serial.println(WiFi.localIP()); //Use if using DHCP to know the IP
}

void loop() {
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);
  
  WiFiClient client; //Client to handle TCP Connection
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) { //Connect to server using port httpPort
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/test/hello.php?data1=787348&data2=7387"; //File or Server page you want to communicate with. along with data
  
  // 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");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 25000) { //Try to fetch response for 25 seconds
      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");
  client.stop(); //Close Connection
}

PHP Script: hello.php

<?php 

     $data1= $_GET["data1"]; 
     $data2= $_GET["data2"];

     echo 'Data 1 is '. $data1.' Data 2 is '.$data2;

?>

This is a simple script running on a local server that I have made by installing XAMPP into my laptop. If you are interested you can do so by installing XAMPP into your system. To see how to do so you can check out my earlier video on this  bit.ly/2pGB0zC

 

ESP8266 Arduino Core : bit.ly/2fcezB9
Schematic : bit.ly/2fcgNR0
Buy ESP8266 : amzn.to/2cZfUv8

********************************************************************
Related VIdeos:
Programming ESP8266 using Arduino IDE : bit.ly/2hdARij
Getting started with ESP8266 : bit.ly/2fchgTc
ESp8266 with Arduino: bit.ly/2fclx8Y
ESP8266 with Raspberry Pi : bit.ly/2fchHgb
—————————————————————————————————
Subscribe YouTube : bit.ly/2gsIZ2g

Guys Subscribe to my channel for latest contents into your inbox.
Support me to keep going.
___________________________________________

Website : www.weargenius.in
Twitter : twitter.com/geekybikash
YouTube : www.youtube.com/weargenius
Instagram : www.instagram.com/weargenius/
GIT : github.com/oksbwn

Suvendu Swain
Catch Me On

Suvendu Swain

Blogger/ Educationist at WeArGenius
A person passionate about tech stuffs.
Suvendu Swain
Catch Me On

Latest posts by Suvendu Swain (see all)

Related posts