Using ESP 01 Wifi in STAtion (STA) mode | ESP wifi tutorial | Part - 2

ESP - 01 Fetch time from NTP server in STA mode

In last tutorial, we use ESP 01 AP mode to control relay switch. In this tutorial we will use STAtion (STA) mode to fetch time from Network Time Protocol (NTP) server and display it on serial monitor. In this mode ESP - 01 will connect to existing wifi network just like any other mobile/device connecting to a home network. This mode allow ESP to connect to internet and send/receive information.

Code
#include "time.h"
#include <ESP8266WiFi.h>


const char* ssid = "Sensorism";  // To be changed
const char* password = "ESP01STAModeExample"; // To be changed

const char* ntpServer = "pool.ntp.org";
const long  indOffSet = 19800;   //Replace with your GMT offset (seconds), current is for INDIA

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  delay(2000);
  while(WiFi.status()!= WL_CONNECTED) {
    delay(500);
    Serial.println("attempting to connect to .....");
  }
  Serial.println("Connected ...");
  Serial.println(WiFi.localIP());
  configTime(indOffSet, 0, ntpServer);
  delay(2000);
  WiFi.disconnect();
}

void loop() {
  delay(3000);
  printNTPTime();
}

void printNTPTime() {
  time_t rawTime;
  struct tm *timeInfo;
  time (&rawTime);
  timeInfo = localtime(&rawTime);
  Serial.println(asctime(timeInfo));
}

Kindly replace with your network name and password in the above code.
Offset is set for IND (GMT +5:30) kindly replace with your country offset.
Daylight offset is also set a 0. Change if needed.

This mode can also be used in master slave configuration to connect all slave nodes to master network. 

Compile and upload the code to ESP - 01. (See tutorial for uploading code.)

After uploading go to Tools -> Serial Monitor



In the next tutorial, we will explore using ESP in dual mode (both AP and STA) using Wifi manager.

Comments

Popular posts from this blog

Microphone Sound Sensor explained | HW-484 | Interfacing with ESP 8266

Using ESP 01 in Access Point (AP) mode to control ESP relay switch | ESP wifi tutorial | Part - 1

[Experiment] DIY Robotic 2 wheel drive car using L298N motor driver and NodeMCU | ESP 8266