Created
August 16, 2017 18:22
-
-
Save ItKindaWorks/09fafa8cd4575cdc6236ccade000de48 to your computer and use it in GitHub Desktop.
A small program to help keep track of whether or not your pet has been fed breakfast and dinner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
petFeedingMon.ino | |
Copyright (c) 2016 ItKindaWorks All right reserved. | |
github.com/ItKindaWorks | |
petFeedingMon.ino is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
petFeedingMon.ino is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with petFeedingMon.ino. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include "ESPHelper.h" | |
#include "Metro.h" | |
#include <TimeLib.h> | |
#include <WiFiUdp.h> | |
//pin defs | |
const int led1 = 0; | |
const int led2 = 2; | |
const int button = 3; | |
//set this info for your own network | |
netInfo homeNet = { .mqttHost = "YOUR MQTT-IP", //can be blank if not using MQTT | |
.mqttUser = "YOUR MQTT USERNAME", //can be blank | |
.mqttPass = "YOUR MQTT PASSWORD", //can be blank | |
.mqttPort = 1883, //default port for MQTT is 1883 - only chance if needed. | |
.ssid = "YOUR SSID", | |
.pass = "YOUR NETWORK PASS"}; | |
ESPHelper myESP(&homeNet); | |
//NTP setup vars | |
static const char ntpServerName[] = "us.pool.ntp.org"; | |
const int timeZone = -4; | |
WiFiUDP Udp; | |
unsigned int localPort = 8888; // local port to listen for UDP packets | |
time_t getNtpTime(); | |
void sendNTPpacket(IPAddress &address); | |
//LED state | |
int state = 0; | |
//button state | |
bool buttonState = false; | |
//whether the NTP has been initialized | |
bool initDone = false; | |
void setup() { | |
//setup the pins | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
pinMode(button, INPUT); | |
//init to lights off | |
digitalWrite(led1, HIGH); | |
digitalWrite(led2, HIGH); | |
//setup ESPHelper OTA | |
myESP.OTA_enable(); | |
myESP.OTA_setPassword("OTA_PASSWORD"); | |
myESP.OTA_setHostnameWithVersion(netHostName); | |
//disable hopping | |
myESP.setHopping(false); | |
//start ESPHelper | |
myESP.begin(); | |
//setup NTP | |
Udp.begin(localPort); | |
setSyncProvider(getNtpTime); | |
setSyncInterval(300); | |
} | |
void loop(){ | |
if(myESP.loop() >= WIFI_ONLY){ | |
//once we have a WiFi connection trigger some extra setup | |
if(!initDone){ | |
delay(100); | |
//setup the NTP connection and get the current time | |
setupNTP(); | |
delay(200); | |
//only set initDone to true if the time is set | |
if(timeStatus() != timeNotSet){ | |
initDone = true; | |
} | |
} | |
} | |
//keep track of the previous button state and get the current state | |
bool prevButtonState = buttonState; | |
buttonState = digitalRead(button); | |
//get the time and reset the lights if needed | |
time_t t = now(); | |
if(hour(t) == 0){ | |
state = 0; | |
} | |
//check for a button press and update the LED state if needed | |
if(buttonState && prevButtonState == false){ | |
delay(50); | |
if(digitalRead(button)){ | |
state++; | |
//reset state back to off if pushed too many times | |
if(state == 3){state = 0;} | |
} | |
} | |
//Lights off | |
if(state == 0){ | |
digitalWrite(led1, HIGH); | |
digitalWrite(led2, HIGH); | |
} | |
//lou has been fed in the morning | |
else if(state == 1){ | |
digitalWrite(led1, LOW); | |
digitalWrite(led2, HIGH); | |
} | |
//lou has been fed in the evening | |
else if(state == 2){ | |
digitalWrite(led1, LOW); | |
digitalWrite(led2, LOW); | |
} | |
yield(); | |
} | |
/*-------- NTP code ----------*/ | |
void setupNTP(){ | |
delay(200); | |
Udp.begin(localPort); | |
setSyncProvider(getNtpTime); | |
setSyncInterval(300); | |
} | |
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message | |
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets | |
time_t getNtpTime() | |
{ | |
IPAddress ntpServerIP; // NTP server's ip address | |
while (Udp.parsePacket() > 0) ; // discard any previously received packets | |
//Serial.println("Transmit NTP Request"); | |
// get a random server from the pool | |
WiFi.hostByName(ntpServerName, ntpServerIP); | |
//Serial.print(ntpServerName); | |
//Serial.print(": "); | |
//Serial.println(ntpServerIP); | |
sendNTPpacket(ntpServerIP); | |
uint32_t beginWait = millis(); | |
while (millis() - beginWait < 1500) { | |
int size = Udp.parsePacket(); | |
if (size >= NTP_PACKET_SIZE) { | |
//Serial.println("Receive NTP Response"); | |
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer | |
unsigned long secsSince1900; | |
// convert four bytes starting at location 40 to a long integer | |
secsSince1900 = (unsigned long)packetBuffer[40] << 24; | |
secsSince1900 |= (unsigned long)packetBuffer[41] << 16; | |
secsSince1900 |= (unsigned long)packetBuffer[42] << 8; | |
secsSince1900 |= (unsigned long)packetBuffer[43]; | |
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; | |
} | |
} | |
//Serial.println("No NTP Response :-("); | |
return 0; // return 0 if unable to get the time | |
} | |
// send an NTP request to the time server at the given address | |
void sendNTPpacket(IPAddress &address) | |
{ | |
// set all bytes in the buffer to 0 | |
memset(packetBuffer, 0, NTP_PACKET_SIZE); | |
// Initialize values needed to form NTP request | |
// (see URL above for details on the packets) | |
packetBuffer[0] = 0b11100011; // LI, Version, Mode | |
packetBuffer[1] = 0; // Stratum, or type of clock | |
packetBuffer[2] = 6; // Polling Interval | |
packetBuffer[3] = 0xEC; // Peer Clock Precision | |
// 8 bytes of zero for Root Delay & Root Dispersion | |
packetBuffer[12] = 49; | |
packetBuffer[13] = 0x4E; | |
packetBuffer[14] = 49; | |
packetBuffer[15] = 52; | |
// all NTP fields have been given values, now | |
// you can send a packet requesting a timestamp: | |
Udp.beginPacket(address, 123); //NTP requests are to port 123 | |
Udp.write(packetBuffer, NTP_PACKET_SIZE); | |
Udp.endPacket(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment