Merge branch 'dev' of https://git.steelants.cz/JonatanRek/PHP_SMART_HOME_V3 into dev
This commit is contained in:
commit
6f0fa2f81e
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
@ -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.
Binary file not shown.
After Width: | Height: | Size: 44 KiB |
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
19
api.php
19
api.php
@ -101,16 +101,26 @@ if ($token == null || $token == "") {
|
||||
|
||||
//Vstupní Checky
|
||||
if (!DeviceManager::registeret($token)) {
|
||||
$notificationMng = new NotificationManager;
|
||||
$notificationData = [];
|
||||
$notificationData = [
|
||||
'title' => 'Info',
|
||||
'body' => 'New device Detected',
|
||||
'icon' => '',
|
||||
'body' => 'New device Detected Found',
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
$deviceId = DeviceManager::create($token, $token);
|
||||
foreach ($values as $key => $value) {
|
||||
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $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
|
||||
@ -161,7 +171,7 @@ if ($values != null || $values != "") {
|
||||
$notificationData = [
|
||||
'title' => 'Info',
|
||||
'body' => 'Someone just open up '.$device['name'],
|
||||
'icon' => '',
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
|
||||
break;
|
||||
@ -169,7 +179,7 @@ if ($values != null || $values != "") {
|
||||
$notificationData = [
|
||||
'title' => 'Alert',
|
||||
'body' => 'Wather leak detected by '.$device['name'],
|
||||
'icon' => '',
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
break;
|
||||
}
|
||||
@ -225,7 +235,6 @@ if ($values != null || $values != "") {
|
||||
echo json_encode(array(
|
||||
'device' => [
|
||||
'hostname' => $device['name'],
|
||||
'sleepTime' => $device['sleep_time'],
|
||||
'ipAddress' => $device['ip_address'],
|
||||
'subnet' => $device['subnet'],
|
||||
'gateway' => $device['gateway'],
|
||||
|
@ -129,7 +129,7 @@ class Ajax extends Template
|
||||
$notificationData = [
|
||||
'title' => 'Alert',
|
||||
'body' => 'test notification',
|
||||
'icon' => '',
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
$notificationMng = new NotificationManager;
|
||||
$subscribers = $notificationMng::getSubscription();
|
||||
|
Loading…
Reference in New Issue
Block a user