Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.1 pull branch #1

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MQTT based multiple Reader Support for n Doors
* Open Door over Mqtt
* User Access List Sync over mqtt
* User Acess List  Up/Download over mqtt
  • Loading branch information
marelab committed May 12, 2019
commit c066f79d311aa2430a6754ba186fd79ee009a771
4 changes: 4 additions & 0 deletions src/config.esp
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,16 @@ bool ICACHE_FLASH_ATTR loadConfiguration()
mpas = strdup(mpasString.c_str());
String mqttTopicString = mqtt["topic"];
mqttTopic = strdup(mqttTopicString.c_str());

interval = mqtt["syncrate"];

mqttClient.setServer(mhs, mport);
mqttClient.setCredentials(muser, mpas);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onPublish(onMqttPublish);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onConnect(onMqttConnect);
mqttClient.onMessage(onMqttMessage);
#ifdef DEBUG
Serial.println("[ INFO ] try to call mqttconnect ");
#endif
Expand Down
7 changes: 5 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define VERSION "1.0.2"
#define VERSION "1.1.0"

#include "Arduino.h"
#include <ESP8266WiFi.h>
Expand Down Expand Up @@ -139,7 +139,10 @@ unsigned long activateTime;
int timeZone;

unsigned long nextbeat = 0;
unsigned long interval = 1800;

// Add to html mqtt to control the sync
unsigned long interval = 1800; // 30 min


#include "log.esp"
#include "mqtt.esp"
Expand Down
222 changes: 210 additions & 12 deletions src/mqtt.esp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,25 @@ void mqtt_publish_boot(time_t boot_time, String const &wifi, String const &ip) {
#endif
}



void mqtt_publish_heartbeat(time_t heartbeat) {
const char *topic = mqttTopic;
// const char *topic = mqttTopic;
String stopic (mqttTopic);
stopic = stopic + "/sync";
//char *ptopic = const_cast<char*>(stopic.c_str());

DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = "heartbeat";
root["time"] = heartbeat;
root["type"] = "heartbeat";
root["time"] = heartbeat;
root["ip"] = WiFi.localIP().toString();
root["door"] = deviceHostname;
//root["hostname"] = deviceHostname;
String mqttBuffer4;
root.printTo(mqttBuffer4);
mqttClient.publish(topic, 0, false, mqttBuffer4.c_str());
//mqttClient.publish(topic, 0, false, mqttBuffer4.c_str());
mqttClient.publish(stopic.c_str(), 0, false, mqttBuffer4.c_str());
#ifdef DEBUG
Serial.print("[ INFO ] Mqtt Publish:");
Serial.println(mqttBuffer4);
Expand All @@ -77,7 +87,10 @@ void mqtt_publish_heartbeat(time_t heartbeat) {

void mqtt_publish_access(time_t accesstime, String const &isknown, String const &type, String const &user, String const &uid) {
if (mqttClient.connected()) {
const char *topic = mqttTopic;
//String toppic_path = "" + mqttTopic+"/send";
String stopic (mqttTopic);
stopic = stopic + "/send";

DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["type"] = "access";
Expand All @@ -86,9 +99,10 @@ void mqtt_publish_access(time_t accesstime, String const &isknown, String const
root["access"] = type;
root["username"] = user;
root["uid"] = uid;
root["door"] = deviceHostname;
String mqttBuffer;
root.printTo(mqttBuffer);
mqttClient.publish(topic, 0, false, mqttBuffer.c_str());
mqttClient.publish(stopic.c_str(), 0, false, mqttBuffer.c_str());
#ifdef DEBUG
Serial.print("[ INFO ] Mqtt Publish:");
Serial.println(mqttBuffer);
Expand All @@ -100,15 +114,199 @@ void onMqttPublish(uint16_t packetId) {
writeEvent("INFO", "mqtt", "MQTT publish acknowledged", String(packetId));
}


void ICACHE_FLASH_ATTR getUserList(int todo) {
String stopic (mqttTopic);
if (todo == 0) // Userlist to add to DB
stopic = stopic + "/accesslist";
else if (todo == 1) // Just for List
stopic = stopic + "/send";
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
JsonArray &users = root.createNestedArray("list");
Dir dir = SPIFFS.openDir("/P/");
Serial.println("[ MARE ] getUserList");
while (dir.next()) {
JsonObject &item = users.createNestedObject();
String uid = dir.fileName();
uid.remove(0, 3);
item["uid"] = uid;
File f = SPIFFS.open(dir.fileName(), "r");
size_t size = f.size();
std::unique_ptr<char[]> buf(new char[size]);
f.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer2;
JsonObject &json = jsonBuffer2.parseObject(buf.get());
if (json.success()) {

if (mqttClient.connected()) {
String mqttBuffer;
json.printTo(mqttBuffer);
mqttClient.publish(stopic.c_str(), 0, false, mqttBuffer.c_str());
#ifdef DEBUG
Serial.print("[ MARE ] Mqtt Publish:");
Serial.println(mqttBuffer);
#endif
}
}
}
}

void DeleteAllUserFiles(){
Dir dir = SPIFFS.openDir("/P/");
while (dir.next()) {
String uid = dir.fileName();
uid.remove(0, 3);
SPIFFS.remove(dir.fileName());
}
}

void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {

// JSON PARSING


char jjson[total + 1] ;
//strlcpy (jjson , payload, total);
memcpy(jjson,payload,total);
jjson[total+1]='\0';
String espIp = WiFi.localIP().toString();
Serial.println(espIp);
//JsonBuffer jsonBuffer;
StaticJsonBuffer<255> jsonBuffer;
#ifdef DEBUG
Serial.print("[ MARE ] JSON msg :");
Serial.println(jjson);
//Serial.println(payload);
#endif

JsonObject &root = jsonBuffer.parseObject(jjson);
if (!root.success())
{
#ifdef DEBUG
Serial.print("[ MARE ] Failed parse MQTT message :");
Serial.println(jjson);
#endif
return;
}

// Check if IP was send with command
if (root.containsKey("doorip"))
{
const char *ipadr = root["doorip"];
if (!((strcmp(ipadr, espIp.c_str())==0) && (ipadr != NULL)))
{
#ifdef DEBUG
Serial.print("[ MARE ] ESP IP: ");
Serial.println(espIp);
Serial.print("[ MARE ] recv IP: ");
Serial.println(ipadr);
#endif
return;
}
}else
{
return;
}

////////////////////////////////////////////////////////////
// CASE FOR MQTT
////////////////////////////////////////////////////////////
const char *command = root["cmd"];
// Check whatever the command is and act accordingly
if (strcmp(command, "getuser") == 0)
{
#ifdef DEBUG
Serial.println("[ MARE ] Get User List");
#endif
getUserList(0);
return;
}

else if (strcmp(command, "listusr") == 0)
{
#ifdef DEBUG
Serial.println("[ MARE ] List usees");
#endif
getUserList(1);
return;
}

else if (strcmp(command, "opendoor") == 0)
{
#ifdef DEBUG
Serial.println("[ MARE ] Door open");
#endif
activateRelay = true;
previousMillis = millis();
return;
}


else if (strcmp(command, "deletusers") == 0)
{
#ifdef DEBUG
Serial.println("[ MARE ] Delete all users");
#endif
DeleteAllUserFiles();
return;
}

else if (strcmp(command, "adduser") == 0)
{

#ifdef DEBUG
Serial.print("[ MARE ] Add Users :");
const char *name = root["user"];
Serial.println(name);
#endif

const char *uid = root["uid"];
String filename = "/P/";
filename += uid;
File f = SPIFFS.open(filename, "w+");
// Check if we created the file
if (f)
{
root.printTo(f);
}
f.close();
return;
}
return;
}




void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
#ifdef DEBUG
Serial.println("[ MARE ] Subscribe acknowledged.");
Serial.print("[ MARE ]packetId: ");
Serial.println(packetId);
Serial.print("[ MARE ] qos: ");
Serial.println(qos);
#endif
}

void onMqttConnect(bool sessionPresent) {
#ifdef DEBUG
Serial.println("[ INFO ] MQTT Connected session");
#endif
#ifdef DEBUG
Serial.println("[ INFO ] MQTT Connected session");
#endif
if (sessionPresent == true) {
#ifdef DEBUG
Serial.println("[ INFO ]MQTT session Present: True");
#endif
#ifdef DEBUG
Serial.println("[ INFO ]MQTT session Present: True");
#endif
writeEvent("INFO", "mqtt", "Connected to MQTT Server", "Session Present");
}
mqtt_publish_boot(now(), WiFi.SSID(), WiFi.localIP().toString());

uint16_t packetIdSub = mqttClient.subscribe(mqttTopic, 2);

#ifdef DEBUG
Serial.print("[ MARE ] Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
#endif


}
Loading