Tag Archives: esp-12

ESP-8266 and NTP = onboard timekeeping

We can sync the time of a ESP-8266 with a NTP server and then keep time count on the device. It will not need any other additional devices like a DS1307 clock. And once synced with the NTP server it can keep the time even if there is no internet  – very handy.

The time formatting and display functions are standard C++ functions.

#include <ESP8266WiFi.h>

#include "TinyDHT.h"

const char *ssid     = "xxxxx";
const char *password = "xxxxxx";


#define DHTPIN11 14          // What digital pin we're connected to
#define DHTTYPE11 DHT11     // DHT 11

DHT dht11(DHTPIN11, DHTTYPE11);

struct tm* tm;

float temperature, relHumidity;
String currentTime;

#define countof(a) (sizeof(a) / sizeof(a[0]))

#define LED 13

void setup() {

  time_t now = 0;

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  Serial.println("WiFi connected");
  
//sync time with the NTP server. First param 19800 seconds = +5:30 for IST, Second Param 0 = DST 
 configTime(19800, 0, "pool.ntp.org");

//waiting for time to get synced before proceeding. This is not strictly required. Please see after code 
  do
  {
    now = time(nullptr);
    delay(500);
    Serial.print("*");
  }while(now < time(nullptr));
  
  pinMode(LED, OUTPUT);
  
  pinMode(DHTPIN11, INPUT);
  dht11.begin();

  onLedIndicatorChange();
}


void loop() {
  // Your code here 
  time_t now = time(nullptr);
  
  char datestring[30];
  
  strftime(datestring, 
            countof(datestring),
            "%d/%m/%Y %I:%M:%S %p",
            localtime(&now)
            );
   
  currentTime = datestring; 
  Serial.println(currentTime);
  temperature = dht11.readTemperature();
  delay(200);
  relHumidity = dht11.readHumidity();
  delay(200);

}

void onLedIndicatorChange() {
  // Do something
  int ledIndicator = 1; 
  digitalWrite(LED, ledIndicator);
}

configtime listens for connection in the back and will sync as soon as a connection is available. So if a time is not critical for the successive codes or functions then the loop that waits for the sync can be skipped.

configTime(19800, 0, "pool.ntp.org");
do //loop and wait for a sync
{
  now = time(nullptr);
  delay(500);
  Serial.print("*");
}while(now < time(nullptr));

 

Sync Ds1302 with NTP

This code first syncs the DS1302 chip with a NTP server and then reads time from the DS1302 chip. Every time the system is booted the clock is synced with a NTP server.

#include <ESP8266WiFi.h>

#include "TinyDHT.h" // Adafruit library https://github.com/adafruit/TinyDHT

#include <ThreeWire.h> // needed by rtc
#include <RtcDS1302.h> // Makuna library https://github.com/Makuna/Rtc

const char *ssid     = "xxxxxxxxxxxx";
const char *password = "xxxx";


#define DHTPIN11 14          // Pin to which DHT11 connected to
#define DHTTYPE11 DHT11     // DHT 11

DHT dht11(DHTPIN11, DHTTYPE11);

ThreeWire myWire(5,16,4); // IO, SCLK, CE/Reset
RtcDS1302<ThreeWire> Rtc(myWire);
RtcDateTime currentTimeEpoch;

#define countof(a) (sizeof(a) / sizeof(a[0]))

float temperature, relHumidity;
String currentTime;

void initialiseRTC()
{
    //now = time(nullptr);
    //Serial.print(now);
    
    /*if (!Rtc.IsDateTimeValid()) 
    {
        // Common Causes:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing

        Serial.println("RTC lost confidence in the DateTime!");
        //by default the RTC library assumes time from year 2000, but EPOCH starts from 1970. Severs returns EPOCH and that is the standard. So below converting the EPOCH to equivalent RTC date.   
        currentTimeEpoch.InitWithEpoch32Time(time(nullptr));
        Rtc.SetDateTime(currentTimeEpoch);
    }*/

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    if (Rtc.GetIsWriteProtected())
    {
        Serial.println("RTC was write protected, enabling writing");
        Rtc.SetIsWriteProtected(false);
    }

    //here we are always syncing the clock when the device is booted. 
    currentTimeEpoch.InitWithEpoch32Time(time(nullptr));
    Rtc.SetDateTime(currentTimeEpoch);
    
    if (!Rtc.GetIsWriteProtected())
    {
        Serial.println("RTC was not write protected, disabling writing");
        Rtc.SetIsWriteProtected(true);
    }

}

#define LED 13

void setup() {
  time_t now = 0;
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  Serial.println("WiFi connected");
  
// here we are setting up the NTP call. Consequently we can just call time() to get the time. 
//the first parameter is Timezone in seconds, second parameter is DST in seconds and third parameter is the NTP server. 
  configTime(19800, 0, "pool.ntp.org"); 

//wait till the clock is synced, else wrong time will get set in the DS1302 chip 
  do   
  {
    // time(nullptr) - returns the time that was received from NTP server
    now = time(nullptr);
    delay(500);
    Serial.print("*");
  }while(now < time(nullptr));

  Rtc.Begin(); //initialise the RTC library
  initialiseRTC(); // this will set the NTP time in DS1302 clock chip,
  
  pinMode(LED, OUTPUT);
  
  pinMode(DHTPIN11, INPUT);
  dht11.begin();

  onLedIndicatorChange();
}

void loop() {
  // Your code here 
  RtcDateTime now = Rtc.GetDateTime(); // reading the time from DS1302 clock module
  char datestring[20];
  
  snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            now.Month(),
            now.Day(),
            now.Year(),
            now.Hour(),
            now.Minute(),
            now.Second() );
   
  currentTime = datestring; 
  Serial.println(currentTime);
  temperature = dht11.readTemperature();
  delay(200);
  relHumidity = dht11.readHumidity();
  delay(200);

}

void onLedIndicatorChange() {
  // Do something
  int ledIndicator = 1; 
  digitalWrite(LED, ledIndicator);
}

Eagle PCB Library for ESP8266 ESP-12

Eagle PCB Library for ESP-12

Please note though the library file is named as esp-12e, but it is for esp-12. The ESP-12E version will be published soon.

Somehow I couldn’t find a proper a library on the internet. I found only one but that had wrong pin spacing.

Download the esp-12e eagle library here

For more details on ESP-8266 visit this link

ESP8266 Hello World   ESP-8266 AT Commands  GPS Tracker using NEO 6M and ESP-12

 

Package
Symbol

 

GPS Tracker Using NEO 6M and ESP-12

The below code outputs current co-ordinate and speed in km/hr . The code is a first draft and there are optimizations to be done. It uses a Ublox NEO 6M for the GPS module and an ESP-12 (of the ESP 8266 family) for the collecting the GPS data from the NEO 6M module, processing the data and outputting in JSON format.

The ESP-12 is working as a webserver. When connected it first asks for the WiFi password set in the code as “WiFiAPPSK”. And for an added layer of security a password is also needed with the HTTP request. (This will made proper in future).  Without the password the server will reject the request.

Arduino IDE is used for programming the ESP-12.

 

//#include <SoftwareSerial.h> //causing the module to reset automatically intermittently
#include <ESP8266WiFi.h>
#include <EEPROM.h>

//SoftwareSerial gps(14,12);

char WiFiAPPSK[] = "123456#";
char savedPass[9] = "12345678", suppliedPass[9], hashedPass[9];
int addr = 0;

short int resetWifiFlag = 0;

WiFiServer server(80);

void encodePass(char *pass, char *hash) //encoding using XOR, need to change as this generates characters that causes problems while passed through GET method
{
  short int i;
  char key[9] = "ikirmiki"; //8 chars
  
  i = strlen(pass);
  
  //i is pointing at the end of the string. Now fillup the rest string with NULL
  for(; i<=9; i++)
  {
    pass[i] = '\0';
  }
  //at this point the pass is filled with pass and all empty cells are filled with NULL
  //now let's do the XOR
  for(i=0; i<=9;i++)
  {
    //hash[i] = pass[i] ^ key[i];
    *(hash+i) = *(pass+i) /*^ key[i]*/;
  }
}

void decodePass(char *pass, char *hash)
{
  short int i;
  char key[9] = "ikirmiki"; //8 chars
  
  //check the hash by decoding
  //now let's do the XOR
  for(i=0; i<=9;i++)
  {
    *(pass+i) = *(hash+i) /*^ key[i]*/; //Overridden for now as XOR creates some special characters sometimes which is not being interpreted by browser address bar properly.
  }
}

void explodeString(char *delimiter, char *str, char explodedArray[][32]) 
{
  int i = 0;
  char* token;
  
  token = strtok(str, delimiter);
  while(token != NULL)
  {
     //explodedArray[i] = (char *)malloc(strlen(token)+1);
     strcpy(explodedArray[i++],token);delay(1);
     token = strtok(NULL, delimiter);
  }
}


short int ensure8CharPass(char *pass, char *tempPass) //for the XOR ing with 8 chars key we need to ensure the pass is also 8 chars
{
  short int i;

  for(i=0;i<9;i++)
  {
    pass[i]=tempPass[i];
  }
  pass[i] = '\0';
  
  if(strlen(pass) != 8)
  {
    return 0;
  }
  else
  {
    return 1;
  }
}

String getSpeed(String rawLatLong)
{
  int firstPos, lastPos = 0;
  int i;

  firstPos = rawLatLong.indexOf("$GPVTG,");
  for(i=0; i < 7; i++)
  {
    firstPos = rawLatLong.indexOf(",", firstPos+1); //$GPVTG,279.67,T,,M,4.903,N,9.080,K,A*3F -- go to 9.080
    delay(1); 
  }
  firstPos = firstPos + 1; //skip the comma that is infront of 9.080
  lastPos = firstPos;
  for(i=0; i < 2; i++) //go to the comma after K
  {
    lastPos = rawLatLong.indexOf(",", lastPos+1);
    delay(1);
  }
  return rawLatLong.substring(firstPos,lastPos);
}


String getLatLong(String rawLatLong)
{
  int firstPos = 0, lastPos = 0, i, degree;
  char latLong[5][32], buff[64];
  float latitude, longitude, minute;
  String temp;

  firstPos = rawLatLong.indexOf("$GPRMC,"); //same concept as in above ($GPVTG)
  for(i=0; i < 3; i++)
  {
    firstPos = rawLatLong.indexOf(",", firstPos+1);
    delay(1);
  }
  firstPos = firstPos + 1; //skip the comma -- same concept as in above ($GPVTG)
  lastPos = firstPos;
  for(i=0; i < 4; i++)
  {
    lastPos = rawLatLong.indexOf(",", lastPos+1);
    delay(1);
  }
  
  temp = rawLatLong.substring(firstPos,lastPos); delay(1); // delay(1) need to checked if necessary or not. 
  temp.toCharArray(buff,64); delay(1); // delay(1) need to checked if necessary or not.
  explodeString(",",buff,latLong); delay(1);  // delay(1) need to checked if necessary or not.
  //at this point latLong must have all the elemnts in consequetive array
  //convert the values in position 0 and 2
  //fist check if the values at those positions are valid or not
  if(strlen(latLong[0]) && strlen(latLong[2])) //convert to decimal format from degree, minute
  {
    latitude = atof(latLong[0]);
    longitude = atof(latLong[2]);
    latitude = latitude / 100;
    longitude = longitude / 100;

    degree = (int)latitude;
    minute = latitude - degree;
    minute = minute * 100;
    latitude = degree + (minute/60);

    degree = (int)longitude;
    minute = longitude - degree;
    minute = minute * 100;
    longitude = degree + (minute/60);

    delay(1); //need to check if needed
    //Serial.println(String(latitude,6)+","+latLong[1]+","+String(longitude)+","+latLong[3]);
    return (String(latitude,6)+","+latLong[1]+","+String(longitude,6)+","+latLong[3]); //String(latitude,6) the second parameter to string sets the precision after decimal. 

    
  }
  else
  {
    return String("Yet to get a fix");
  }
  
}

void writeSettings(int addr, char *toWrite)
{
  int i;
  for(i=0; i<9; i++)
  {
    EEPROM.write((addr+i), toWrite[i]);
  }
  EEPROM.commit();
}

void readSettings(int addr, char *toRead) // will have to change this function - need to make it such t
{
  int i;
  for(i=0; i<9; i++)
  {
    toRead[i] = EEPROM.read((addr+i));
  }
  toRead[i] = '\0';
}

void setupWiFi()
{
  WiFi.mode(WIFI_AP);

  // Do a little work to get a unique-ish name. Append the
  // last two bytes of the MAC (HEX'd) to "Thing-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "GPS Tracker " + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i=0; i<AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);

  resetWifiFlag = 0; //look in loop for explanation
}


void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  EEPROM.begin(64); //initiate 64 bytes to write/read
  //gps.begin(9600);
 
  readSettings(addr+0, hashedPass); //read the saved pass at addr+0 location
  if(strlen(hashedPass) == 8) //id the saved password is of proper length then decode it and use it.
    decodePass(savedPass, hashedPass);
    
  strcpy(WiFiAPPSK,savedPass); //set the Wifi Password also
  setupWiFi(); //start the wifi
  
  server.begin();
}

void loop() 
{
  //check if any variable is redundant
  volatile unsigned long startTime, currentTime;
  String serialBuffer, latLong;
  int pos,flag,i,j,temp;
  short int passError = 1, len, latLongFlag = 0, speedFlag = 0;
  char serialDataByte;

  if(resetWifiFlag == 1) // if the password is changed then this will be called to re-initialize the WIFI-AP with the new password
   {
      setupWiFi();
   }
  
  //start the webserver and send the data
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) 
  {
    delay(1);
    return;
  }
  else
  {
    delay(1);

    //Read the first line of the request
    String req = client.readStringUntil('\r');

    //now check if password change requested or not
    pos = req.indexOf("/changepass/");
    if(pos != -1)
    {
      //get the new pass
      len = pos+12+8; //12 chars of /changepass/ AND 8 of password
      for(i=pos+12,j=0; i<len; i++,j++)
      {
        if(req.charAt(i) == '/')
          break;
        else
          hashedPass[j] = req.charAt(i);
      }
      hashedPass[j] = '\0';
      decodePass(suppliedPass, hashedPass);
      strcpy(savedPass,suppliedPass);
      //save the password
      //Serial.println("New Pass: "+ String(hashedPass));
      writeSettings(addr+0, hashedPass); //save the password

      //not calling a function for sending the data to save the function call overhead.
      // Prepare the response. Start with the common header:
      String s = "HTTP/1.1 200 OK\r\n";
      s += "Content-Type: application/json\r\n\r\n";
      s += "{"succcess":"Password Changed To: "+ String(suppliedPass)+""}";
      // Send the response to the client
      client.print(s);
      client.flush();
      delay(1);
      client.stop();
      //ESP.restart(); //to make the new password in effect immediately - USING THIS WILL NOT DISPLAY THE RESULT. It seems the loop needs to be exited to send the result to client Using a delay of 10 seconds also didn't work
      //AS THE restart method is not available so setting up the wifi again with the new password
      strcpy(WiFiAPPSK,suppliedPass); //set the Wifi Password also
      resetWifiFlag =1; // if the wifi pass is changed here now then client will loose connection before printing the result. the loop() will have to be exited to print the result. so we are setting the flag here and changing the password in the next iteration of the loop()
      delay(1); //safe side
      return;
    }

    pos = req.indexOf("/pass/");
    if(pos == -1) // that means no password supplied
    {
      //not calling a function for sending the data to save the function call overhead.
      client.flush();
      // Prepare the response. Start with the common header:
      String s = "HTTP/1.1 200 OK\r\n";
      s += "Content-Type: application/json\r\n\r\n";
      s += "{"error":"Auhentication Error 1"}";
      // Send the response to the client
      client.print(s);
      client.flush();
      delay(1);
      client.stop();
      return;
    }
    else
    {
      //Serial.println("Client Connected");
      //read upto the next slash
      len = pos+6+8; //6 chars of /changepass/ AND 8 of password
      for(i=pos+6,j=0; i<len; i++,j++)
      {
        if(req.charAt(i) == '/')
          break;
        else
          hashedPass[j] = req.charAt(i);
      }
      hashedPass[j] = '\0';
      
      //now decode the pass 
      decodePass(suppliedPass, hashedPass);
      if(strcmp(suppliedPass,savedPass))
      {
        //not calling a function for sending the data to save the function call overhead.
        client.flush();
        // Prepare the response. Start with the common header:
        String s = "HTTP/1.1 200 OK\r\n";
        s += "Content-Type: application/json\r\n\r\n";
        s += "{"error":"Auhentication Error 2"}";
        // Send the response to the client
        client.print(s);
        client.flush();
        delay(1);
        client.stop();
        return;
      }
    }
   
    startTime = currentTime = 0;
    serialBuffer = latLong = "";
  
    flag = -1;
    
    startTime = millis();
    
    // put your main code here, to run repeatedly:
    while(flag == -1)  //read 5 seconds of data
    {
      if(Serial.available() > 0)
      {
        serialBuffer += (char)Serial.read();
        if(serialBuffer.indexOf("$GPRMC,") >=0)
        {

          while(1)
          {
            serialDataByte = (char)Serial.read();
            if(serialDataByte == '\r' || serialDataByte == '$') //need to optimize - check if \r is sufficient
            {
              latLong = getLatLong(serialBuffer);
              serialBuffer = serialDataByte; //reset the serial buffer for the next line
              break;
            }
            else
            {
              serialBuffer += serialDataByte;
            }
            delay(1);
          }
        }

        if(serialBuffer.indexOf("$GPVTG,") >=0)
        {
          while(1)
          {
            serialDataByte = (char)Serial.read();
            if(serialDataByte == '\r' || serialDataByte == '$') //need to optimize - check if \r is sufficient
            {
              flag = 1;
              latLong = latLong + "," + getSpeed(serialBuffer);
              latLong = "{"success":""+latLong+""}";
              break;
            }
            else
            {
              serialBuffer += serialDataByte;
            }
            delay(1);
          }
        }
      }

      if(millis()- startTime > 5000)
      {
        //Serial.println("TimeOut");
        latLong = "Timeout"; 
        latLong = "{"error":""+latLong+""}";
        latLongFlag = speedFlag = 0;
        break;
      }
      delay(1);
    } 
    //Serial.println("LN: "+latLong);
  }

  client.flush();
  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: application/json\r\n\r\n";
  //s += "<!DOCTYPE HTML>\r\n<html>\r\n<body>\r\n";
  s += latLong;
  //s += "</body>\r\n</html>\n";
  // Send the response to the client
  client.print(s);
  client.flush();
  //delay(100); Serial.println("Client disonnected");
  latLongFlag = speedFlag = 0;  
  delay(1);
  client.stop();
}

 

Notes:

  • The setupWifi code is from Sparkfun’s code
  • The Software Serial is causing trouble. The WiFi Access Point (AP) is dying after sometime if the Software Serial Library is used.
  • The delays are needed to allow the ESP8266 to do it’s own internal tasks (like maintaining the wifi connection). The delays are needed where there can be long loops.

ESP8266 Programming Using Arduino IDE

The ESP8266 can be programmed using the Arduino IDE. The best part is the codes are similar to Arduino. To start programming a ESP8266 using Arduino IDE follow the below steps

Prerequisites

Steps

  • Start Arduino and goto File-> Preferences.
  • In the “Additional Boards Manager URLs” Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json  (You can add multiple URLs, separating them with commas)
  • Click “OK”
  • Open Boards Manager from Tools -> Board menu and find esp8266 platform.
  • Select the version you need from drop-down box.
  • Click install button.
  • ——————————————–
  • Upload a blank sketch to the Arduino.
  • Connect RX to RX and TX to TX
  • GPIO_0 will go to GND
  • VCC and CH_PD will go to the 3.3v supply line
  • Select the appropriate ESP8266 board from Tools > Board menu.
  • Select the appropriate Flash mode (DOUT or QIO. See notes below)
  • Other parameters works mostly as it is – but still check once.
  • Start upload
  • RESET the module. This is a critical step and the timing matters. It may need a few tries to get it perfect.

After the code uploaded

  • Disconnect GPIO_0
  • Reset

 

For further details please check http://esp8266.github.io/Arduino/versions/2.3.0/

Please see this article for a basic code (Blinking LED that is the Hello World of micro-controllers)

Notes

  • While uploading codes through Arduino IDE select DOUT for new chip and either DOUT or QIO for old chips.
  • Keeping it to DOUT will work for both New and Old chips.
  • Burning new chips with QIO (or inappropriate modes) will result in a successful burning but the code will not run.

 

Here is a Troubleshooting Guide and parameters that worked for me.

Flashing guide for new chips.