• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

IP Unset using NTPClient.h

Q&A Forum › Category: ESP8266 › IP Unset using NTPClient.h
0 Vote Up Vote Down
Paul French asked 2 years ago

Hi, I am trying to incorporate the currentHour in my sketch, so that the system will only run in daylight hours. It’s essential a PIR Relay system which opens the dog flap if movement is detected inside or outside the door, when the door is closed and  I am using a state machine and if statements to check various conditions. Is the currentHour between 8am and 11pm, is the door closed, is the master switch on?
I have been following the tutorials, and tried to incorporate the syntax from getTime and date tutorial, which runs fine for me in PlatfomIO.  However when I try to run my sketch, I get a message in the terminal saying “IP Unset”
I also tried include a webpage control, using the ESP Async WebServer tutorial, but that ended in confusion, so I thought I should try to get the timer functionality working first.
I would really appreciate some help and pointed on which webserver approach to use and organizing my sketch correctly!
Here is the code I use:- (with apologies as I am still trying to learn this stuff!)

Start your code here

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid     = "b;lah blah";
const char* password = "password blahblah";
// Current time
unsigned long currentTime = millis();
// prior time for web timeout and door flap timing
unsigned long priorTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
const int relay2 = D1; //drives linear actuator to close flap GPIO 5
const int relay1 = D2; //drives linear actuator to open flap  GPIO 4
const int pIR2 = D5;  //outputs HIGH signal when motion detected GPIO 14
const int pIR1 = D6; //outputs HIGH signal when motion detected GPIO 12
const int masterSwitch = D7; // manual switch to close flap GPIO13
const int doorSensor = D8; //outputs HIGH is closed - reed switch GPIO15
const unsigned long flapOpenTime = 30000; //in addition to timing of piR pot
unsigned long previousTime = 0; //lasttime flap opened
int currentHour;
byte lastPIR1Status;
byte lastPIR2Status;
byte pIR1Status = digitalRead(pIR1);
byte pIR2Status = digitalRead(pIR2);
byte doorSensorStatus = digitalRead(doorSensor);
byte masterSwitchStatus = digitalRead(masterSwitch);
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

void openFlap()
{
digitalWrite(relay1, LOW);// turn relay 1 ON
digitalWrite(relay2, HIGH);// turn relay 2 OFF
}//openFlap()

void closeFlap()
{
digitalWrite(relay1, HIGH);// turn relay 1 OFF
digitalWrite(relay2, LOW);// turn relay 2 ON
}//closeFlap()

void turnOFF()
{
digitalWrite(relay1, HIGH);// turn relay 1 OFF
digitalWrite(relay2, HIGH);// turn relay 2 OFF
}//turnOFF()
enum _state_enum
{
READY,
IDLE,
CMD_OPEN_FLAP,
MONITOR_OPEN_FLAP,
CMD_CLOSE_FLAP,
FLAP_OFF,
};
_state_enum systemState = READY;

void setup() {
Serial.begin(115200);
{
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
pinMode(relay1, OUTPUT);// set pin as output for relay 1
pinMode(relay2, OUTPUT);// set pin as output for relay 2
pinMode(pIR1, INPUT);
pinMode(pIR2, INPUT);
pinMode(doorSensor, INPUT_PULLUP);
pinMode(masterSwitch, INPUT_PULLUP);
lastPIR1Status = digitalRead(pIR1);
lastPIR2Status = digitalRead(pIR2);
doorSensorStatus = digitalRead(doorSensor);

// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(0);
}

void loop() {

switch (systemState)
{
// Wait for motion
case READY:
//make sure master switch is on and door closed and daytime
if ((doorSensorStatus == 0
&& masterSwitchStatus == 0)
&&
(currentHour > 8 && currentHour < 23))
{
systemState = IDLE;
}
break;

case IDLE:
// If either PIR sensor went from LOW to HIGH then open the flap.
if ((pIR1Status != lastPIR1Status && pIR1Status == 1)
||
(pIR2Status != lastPIR2Status  && pIR2Status == 1))
{
systemState = CMD_OPEN_FLAP;
previousTime = currentTime;
}
Serial.println ("IDLE");
Serial.print (" PIR1 - ");
Serial.print (pIR1Status);
Serial.print (" PIR2 - ");
Serial.print (pIR2Status);

break;

case CMD_OPEN_FLAP:

openFlap();
systemState = MONITOR_OPEN_FLAP;
Serial.println ("CMD_OPEN_FLAP");
Serial.print (" PIR1 - ");
Serial.print (pIR1Status);
Serial.print (" PIR2 - ");
Serial.print (pIR2Status);

break;

case MONITOR_OPEN_FLAP:

if ((pIR1Status == 0 && pIR2Status == 0) && (currentTime - previousTime >= flapOpenTime))

{
systemState = CMD_CLOSE_FLAP;
}
Serial.println ("MONITOR_OPEN_FLAP");
Serial.print (" PIR1 - ");
Serial.print (pIR1Status);
Serial.print (" PIR2 - ");
Serial.print (pIR2Status);

break;

case CMD_CLOSE_FLAP:

closeFlap();
systemState = READY;
Serial.println ("CMD_CLOSE_FLAP");
Serial.print (" PIR1 - ");
Serial.print (pIR1Status);
Serial.print (" PIR2 - ");
Serial.print (pIR2Status);

break;

case FLAP_OFF:

turnOFF();
systemState = FLAP_OFF;
Serial.println ("CMD_CLOSE_FLAP");
Serial.print (" PIR1 - ");
Serial.print (pIR1Status);
Serial.print (" PIR2 - ");
Serial.print (pIR2Status);

break;
lastPIR1Status = pIR1Status;
lastPIR2Status = pIR2Status;
}
}

 
4 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
Can you paste the exact error that you’re getting?
Regards,
Sara

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
I’ve taken a look at your code and there’s something missing: you don’t have any lines of code that connect to wi-fi.

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

Regards,
Sara

0 Vote Up Vote Down
Paul French answered 2 years ago

Thank you Sara – that worked fine!, kind regards, Paul

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.