Skip to content

Commit

Permalink
MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
omersiar committed Mar 23, 2018
1 parent 293e4b7 commit f7be464
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 141 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ All notable changes to this project will be documented in this file.
#### Changed
- [webui] Sign in panel now integrated into index.html
- [firmware] Reduced serial outputs.
- [firmware] Switched to Async MQTT Library, needs testing.

#### Fixed
- [firmware] Logs causing Exception 9 because we are delaying async function with NTP sync by WiFi.hostbyname
- [webui] wrong version is shown #80.
- [webui] whole html was shifted with previous css change.

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This project still in its development phase. New features (and also bugs) are in
### What You Will Need
### Hardware
* An ESP8266 module or a development board like **WeMos D1 mini** or **NodeMcu 1.0** with at least **32Mbit Flash (equals to 4MBytes)** (ESP32 does not supported for now)
* A MFRC522 RFID PCD Module or Wiegand based RFID reader
* A MFRC522 RFID PCD Module or PN532 NFC Reader Module or Wiegand based RFID reader
* A Relay Module (or you can build your own circuit)
* n quantity of Mifare Classic 1KB (recommended due to available code base) PICCs (RFID Tags) equivalent to User Number

Expand Down Expand Up @@ -164,11 +164,10 @@ See [ChangeLog](https://github.com/omersiar/esp-rfid/blob/dev/CHANGELOG.md)
## Donations
If this project helps you in a way, you can buy us a beer. You can make a donation to the ESP-RFID community with [Bountysource](https://salt.bountysource.com/teams/esp-rfid)

#### Donators
* 2017-10-03 [steinar-t](https://github.com/steinar-t)
* 2017-12-10 [saschaludwig](https://github.com/saschaludwig)

Thank you for your contributions.
Nothing says better thank you than a donation.

## License
UNLICENSE
Binary file modified bin/firmware.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ env_default = nodemcu
platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
lib_deps =
ArduinoJson
PubSubClient
ESPAsyncTCP
ESPAsyncUDP
ESP Async WebServer
AsyncMqttClient
Time
63
https://github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino.git
Expand Down
78 changes: 41 additions & 37 deletions src/Ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Ntp.h"
#include <ESPAsyncUDP.h>
#include <Dns.h>

char * NtpClient::TimeServerName;
int8_t NtpClient::timezone;
Expand All @@ -17,8 +18,10 @@ AsyncUDP NtpClient::udpListener;

byte NtpClient::NTPpacket[NTP_PACKET_SIZE];

DNSClient dns;

void ICACHE_FLASH_ATTR NtpClient::Ntp(const char * server, int8_t tz, time_t syncSecs) {
TimeServerName = strdup(server);
TimeServerName = strdup(server);
timezone = tz;
syncInterval = syncSecs;
setSyncProvider(getNtpTime);
Expand All @@ -32,30 +35,30 @@ ICACHE_FLASH_ATTR NtpClient::~NtpClient() {

// send an NTP request to the time server at the given address
time_t ICACHE_FLASH_ATTR NtpClient::getNtpTime() {
memset(NTPpacket, 0, sizeof(NTPpacket));
NTPpacket[0]=0b11100011;
NTPpacket[1]=0;
NTPpacket[2]=6;
NTPpacket[3]=0xEC;
NTPpacket[12]=49;
NTPpacket[13]=0x4E;
NTPpacket[14]=49;
NTPpacket[15]=52;
WiFi.hostByName(TimeServerName,timeServer);
if(udpListener.connect(timeServer, 123)) {
udpListener.onPacket([](AsyncUDPPacket packet) {
unsigned long highWord = word(packet.data()[40], packet.data()[41]);
unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
time_t UnixUTCtime = (highWord << 16 | lowWord)-2208988800UL;
setTime(UnixUTCtime);
});
}
else {

}
udpListener.write(NTPpacket, sizeof(NTPpacket));
// ugly
return 0;
dns.getHostByName(TimeServerName,timeServer);
memset(NTPpacket, 0, sizeof(NTPpacket));
NTPpacket[0] = 0b11100011;
NTPpacket[1] = 0;
NTPpacket[2] = 6;
NTPpacket[3] = 0xEC;
NTPpacket[12] = 49;
NTPpacket[13] = 0x4E;
NTPpacket[14] = 49;
NTPpacket[15] = 52;
if (udpListener.connect(timeServer, 123)) {
udpListener.onPacket([](AsyncUDPPacket packet) {
unsigned long highWord = word(packet.data()[40], packet.data()[41]);
unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
time_t UnixUTCtime = (highWord << 16 | lowWord) - 2208988800UL;
setTime(UnixUTCtime);
});
}
else {

}
udpListener.write(NTPpacket, sizeof(NTPpacket));
// ugly
return 0;
}

bool ICACHE_FLASH_ATTR NtpClient::processTime() {
Expand All @@ -64,11 +67,12 @@ bool ICACHE_FLASH_ATTR NtpClient::processTime() {

switch (ts) {
case timeNeedsSync:
return false;
break;
case timeSet:
return true;
break;
default:
//sync
now();
return false;
}
}
Expand All @@ -87,17 +91,17 @@ String ICACHE_FLASH_ATTR NtpClient::iso8601DateTime() {
String colon = ":";

return String(year()) + hyphen +
zeroPaddedIntVal(month()) + hyphen +
zeroPaddedIntVal(day()) + "T" +
zeroPaddedIntVal(hour()) + colon +
zeroPaddedIntVal(minute()) + colon +
zeroPaddedIntVal(second()) +
(timezone == 0 ? "Z" : String(timezone));
zeroPaddedIntVal(month()) + hyphen +
zeroPaddedIntVal(day()) + "T" +
zeroPaddedIntVal(hour()) + colon +
zeroPaddedIntVal(minute()) + colon +
zeroPaddedIntVal(second()) +
(timezone == 0 ? "Z" : String(timezone));
}

time_t NtpClient::getUptimeSec() {
_uptimesec = _uptimesec + (millis () - _uptimesec);
return _uptimesec / 1000;
return _uptimesec / 1000;
}

deviceUptime ICACHE_FLASH_ATTR NtpClient::getDeviceUptime() {
Expand All @@ -119,9 +123,9 @@ String ICACHE_FLASH_ATTR NtpClient::getDeviceUptimeString() {
deviceUptime uptime = getDeviceUptime();

return String(uptime.days) + " days, " +
String(uptime.hours) + " hours, " +
String(uptime.mins) + " mins, " +
String(uptime.secs) + " secs";
String(uptime.hours) + " hours, " +
String(uptime.mins) + " mins, " +
String(uptime.secs) + " secs";

}

Expand Down
Loading

0 comments on commit f7be464

Please sign in to comment.