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 (http://bit.ly/2cORmj0)

 

Links :

Sketch: http://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  http://bit.ly/2pGB0zC

 

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

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

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

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

Related posts

2 Thoughts to “Fetching data from server | Arduino/ESP8266”

  1. Rutvik

    Thanks for the video, its really helpful. But what i needed is to fetch data from web, store it in string & then based on that data some operation is to be done like controlling motor. So there is need of arduino & ESP is used to get that data. So can u help me for that.

    1. If you are using Arduino on ESP8266 not ESP8266 with Arduino u can do it simply by following the tutorial.

      // 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);
      }

      In the above code add all line returned to form a string and thereafter do your operations on the returned data. Hope this will help you.

Leave a Comment