This commit is contained in:
unknown 2020-01-11 11:23:01 +01:00
commit 6f0fa2f81e
9 changed files with 31713 additions and 6 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,88 @@
//Includes
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
//USER CONFIGURATION
const char* WIFI_SSID = "";
const char* WIFI_PASS = "";
const char* HW_TOKEN = "";
const char* SERVER_ADRESS = "";
StaticJsonDocument<250> jsonBuffer;
String requestJson = "";
ADC_MODE(ADC_VCC);
//Pins
#define SENZORPIN 12 //12
void setup() {
Serial.begin(9600);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
checkConnection();
Serial.print("TEST");
jsonBuffer = {};
jsonBuffer["token"] = HW_TOKEN;
jsonBuffer["values"]["door"]["value"] = 0;
int batteryVoltage = (ESP.getVcc() + 600) / 1000;
jsonBuffer["values"]["battery"]["value"] = batteryVoltage;
serializeJson(jsonBuffer, requestJson);
sendHttpRequest(requestJson);
delay(500);
ESP.deepSleep(0);
}
void loop() {
}
void setStaticIp(String ipAddress, String subnet, String gateway){
//Set static ip
IPAddress staticIpAddress;
IPAddress subnetIpAddress;
IPAddress gatewayIpAddress;
if (
staticIpAddress.fromString(ipAddress) &&
subnetIpAddress.fromString(subnet) &&
gatewayIpAddress.fromString(gateway) &&
WiFi.localIP() != staticIpAddress
) {
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
Serial.print("STATIC IP address:");
Serial.println(WiFi.localIP());
}
}
String sendHttpRequest (String requestJson) {
HTTPClient http;
http.setReuse(true);
http.begin(SERVER_ADRESS);
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
int httpCode = http.POST(requestJson);
String payload = http.getString(); //Get the response payload
http.end();
if (httpCode == -1) {
ESP.restart();
}
return payload;
}
bool checkConnection() {
int count = 0;
while ( count < 30 ) {
delay(500);
if (WiFi.status() == WL_CONNECTED) {
return true;
}
count++;
}
return false;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

19
api.php
View File

@ -101,16 +101,26 @@ if ($token == null || $token == "") {
//Vstupní Checky //Vstupní Checky
if (!DeviceManager::registeret($token)) { if (!DeviceManager::registeret($token)) {
$notificationMng = new NotificationManager;
$notificationData = [];
$notificationData = [ $notificationData = [
'title' => 'Info', 'title' => 'Info',
'body' => 'New device Detected', 'body' => 'New device Detected Found',
'icon' => '', 'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
]; ];
$deviceId = DeviceManager::create($token, $token); $deviceId = DeviceManager::create($token, $token);
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) { if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) {
SubDeviceManager::create($deviceId, $key, UNITS[$key]); SubDeviceManager::create($deviceId, $key, UNITS[$key]);
} }
if ($notificationData != []) {
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ");
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}
} }
//Notification for newly added Device //Notification for newly added Device
@ -161,7 +171,7 @@ if ($values != null || $values != "") {
$notificationData = [ $notificationData = [
'title' => 'Info', 'title' => 'Info',
'body' => 'Someone just open up '.$device['name'], 'body' => 'Someone just open up '.$device['name'],
'icon' => '', 'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
]; ];
break; break;
@ -169,7 +179,7 @@ if ($values != null || $values != "") {
$notificationData = [ $notificationData = [
'title' => 'Alert', 'title' => 'Alert',
'body' => 'Wather leak detected by '.$device['name'], 'body' => 'Wather leak detected by '.$device['name'],
'icon' => '', 'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
]; ];
break; break;
} }
@ -225,7 +235,6 @@ if ($values != null || $values != "") {
echo json_encode(array( echo json_encode(array(
'device' => [ 'device' => [
'hostname' => $device['name'], 'hostname' => $device['name'],
'sleepTime' => $device['sleep_time'],
'ipAddress' => $device['ip_address'], 'ipAddress' => $device['ip_address'],
'subnet' => $device['subnet'], 'subnet' => $device['subnet'],
'gateway' => $device['gateway'], 'gateway' => $device['gateway'],

View File

@ -129,7 +129,7 @@ class Ajax extends Template
$notificationData = [ $notificationData = [
'title' => 'Alert', 'title' => 'Alert',
'body' => 'test notification', 'body' => 'test notification',
'icon' => '', 'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
]; ];
$notificationMng = new NotificationManager; $notificationMng = new NotificationManager;
$subscribers = $notificationMng::getSubscription(); $subscribers = $notificationMng::getSubscription();