diff --git a/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino b/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino similarity index 69% rename from _FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino rename to _FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino index 5035cbe..c7fd49d 100644 --- a/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino/ESP_OS_NODE_V3.ino.ino +++ b/_FIRMWARE/firmwares/NodeMCU Sensor/ESP_OS_NODE_V3.ino @@ -1,3 +1,4 @@ +//Includes #include #include #include @@ -20,16 +21,25 @@ DHT DHTs(pinDHT, DHT11); void setup() { Serial.begin(9600); while (!Serial) continue; + delay(10); + Serial.println('\n'); + //Show start up Configuration + Serial.println("HW: " + String(hwId)); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); + Serial.print("MAC address:\t"); + Serial.println(WiFi.macAdress()); pinMode(LIGHTPIN, INPUT); } void loop() { + //Start Conection to wifi WiFi.begin(ssid, pasw); checkConnection(); //HTTP CLIENT HTTPClient http; - http.begin(url); + http.begin(url); //Begun HTTP Request http.addHeader("Content-Type", "text/plain"); //Specify content-type header DHTs.begin(); @@ -38,6 +48,7 @@ void loop() { StaticJsonDocument<265> doc; doc["token"] = hwId; + //Read and Handle DHT values float tem = DHTs.readTemperature(); float hum = DHTs.readHumidity(); Serial.println("TEMP" + String(tem) + ";HUMI" + String(hum)); @@ -50,6 +61,7 @@ void loop() { doc["values"]["humi"]["unit"] = "%"; } + //Handle Photo Rezistor Values doc["values"]["light"]["value"] = analogRead(LIGHTPIN); doc["values"]["light"]["unit"] = ""; @@ -59,16 +71,17 @@ void loop() { Serial.print("JSON: "); Serial.println(jsonPayload); - int httpCode = http.POST(jsonPayload); + int httpCode = http.POST(jsonPayload); //Get Http response code String httpPayload = http.getString(); //Get the response payload Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code Serial.println("HTTP BODY: " + String(httpPayload) + ""); //Print request response payload - DeserializationError error = deserializeJson(doc, httpPayload); + DeserializationError error = deserializeJson(doc, httpPayload); //Get deserialization Error if exists //configuration setup String hostName = doc["device"]["hostname"]; int sleepTime = doc["device"]["sleepTime"]; + String ipAddress = doc["device"]["ipAddress"]; String state = doc["state"]; if (state != "succes") { @@ -78,25 +91,37 @@ void loop() { unsuccessfulRounds = 0; } - WiFi.hostname(hostName); + //Set static ip + IPAddress staticIpAddress; + IPAddress subnetIpAddress(192,168,0,1); + IPAddress gatewayIpAddress(255, 255, 255, 0); + + if (staticIpAddress.fromString(ipAddress)) { + WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress); + Serial.print("STATIC IP address:\t"); + Serial.println(WiFi.localIP()); + } + + WiFi.hostname(hostName); //Set HostName http.end(); //Close connection + WiFi.disconnect(); //Disconect from WIFI Serial.println("DISCONECTED FROM WIFI"); - WiFi.disconnect(); - if(unsuccessfulRounds == 5) { + if(unsuccessfulRounds == 5) { //after 5 unsucessful request restart ESP Serial.println("RESTARTING ESP"); ESP.restart() } - Serial.println("GOING TO SLEEP FOR " + String(sleepTime)); - if (sleepTime > 0) { + if (sleepTime > 0) { //if deep sleepTime > 0 use deep sleep + Serial.println("GOING TO SLEEP FOR " + String(sleepTime)); ESP.deepSleep((sleepTime * 60) * 1000000, RF_DEFAULT); } else { delay(5000); } } +//checking if connection is working bool checkConnection() { int count = 0; Serial.print("Waiting for Wi-Fi connection"); diff --git a/_FIRMWARE/firmwares/Sonoff_Basic/Sonoff_Basic_v1.ino b/_FIRMWARE/firmwares/Sonoff_Basic/Sonoff_Basic_v1.ino new file mode 100644 index 0000000..247bd47 --- /dev/null +++ b/_FIRMWARE/firmwares/Sonoff_Basic/Sonoff_Basic_v1.ino @@ -0,0 +1,92 @@ +//Includes +#include +#include +#include + +//Variables +const char* ssid = ""; +const char* pasw = ""; +const char* server = "http://ESP:ESP@dev.steelants.cz/projekty/rest_vasek/api/out.php"; +const char* hwId = ""; +int lastState = 0; + +//Constant +#define SONOFF 12 +#define SONOFF_LED 13 +#define SONOFF_BUT 0 + +void setup() { + Serial.begin(9600); + Serial.println("HW: " + String(hwId)); + pinMode(SONOFF, OUTPUT); + pinMode(SONOFF_LED, OUTPUT); + pinMode(SONOFF_BUT, OUTPUT); + pinMode(SONOFF_BUT, INPUT); + // WI-FI CONECTING + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, pasw); +} + +void loop() { + if(WiFi.status() != WL_CONNECTED){ + WiFi.begin(ssid, pasw); + while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect + digitalWrite(SONOFF_LED, LOW); // LOW will turn on the LED + delay(1000); + digitalWrite(SONOFF_LED, HIGH); // HIGH will turn off the LED + delay(1000); + } + Serial.println("\nCONECTED TO WIFI"); + Serial.println("IP: " + String(WiFi.localIP())); + } + + bool buttonState = digitalRead(SONOFF_BUT); + HTTPClient http; + http.begin(server); + http.addHeader("Content-Type", "text/plain"); //Specify content-type header + String requestJson = "{"; + requestJson += "\"tocken\":\"" + String(hwId) + "\""; + + if (buttonState == true) { + requestJson += ","; + requestJson += "\"on/off\":{"; + requestJson += "\"value\":\"" + String(~lastState) + "\","; + requestJson += "\"unit\":\"\""; + requestJson += "}"; + } + + while(buttonState == true) { + delay(50); // keeps a small delay + } + + requestJson += "}"; + Serial.println("JSON: " + requestJson); + + int httpCode = http.POST(requestJson); + String payload = http.getString(); //Get the response payload + + Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code + Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload + + DynamicJsonDocument doc(1024); + deserializeJson(doc, payload); + + string hostname = doc["hostname"]; + WiFi.hostname(hostname); + + int state = doc["state"]; + + if (state == 1 && lastState == 0) { + Serial.println("ON"); + digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW + digitalWrite(SONOFF_LED, LOW); // Turn the LED on by making the voltage LOW + } else { + Serial.println("OFF"); + digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW + digitalWrite(SONOFF_LED, HIGH); // Turn the LED on by making the voltage LOW + } + + lastState = state; + delay(1000); +} diff --git a/_FIRMWARE/firmwares/Sonoff_Basic/Sonoff_Basic_v2.ino b/_FIRMWARE/firmwares/Sonoff_Basic/Sonoff_Basic_v2.ino new file mode 100644 index 0000000..51d7e43 --- /dev/null +++ b/_FIRMWARE/firmwares/Sonoff_Basic/Sonoff_Basic_v2.ino @@ -0,0 +1,94 @@ +//Includes +#include +#include +#include + +//Variables +const char* ssid = ""; +const char* pasw = ""; +const char* server = "http://ESP:ESP@dev.steelants.cz/projekty/rest_vasek/api/out.php"; +const char* hwId = ""; +int lastState = 0; + +//Constant +#define SONOFF 12 +#define SONOFF_LED 13 +#define SONOFF_BUT 0 + +void setup() { + Serial.begin(9600); + Serial.println("HW: " + String(hwId)); + pinMode(SONOFF, OUTPUT); + pinMode(SONOFF_LED, OUTPUT); + pinMode(SONOFF_BUT, OUTPUT); + pinMode(SONOFF_BUT, INPUT); + // WI-FI CONECTING + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, pasw); +} + +void loop() { + if(WiFi.status() != WL_CONNECTED){ + WiFi.begin(ssid, pasw); + while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect + digitalWrite(SONOFF_LED, LOW); // LOW will turn on the LED + delay(1000); + digitalWrite(SONOFF_LED, HIGH); // HIGH will turn off the LED + delay(1000); + } + Serial.println("\nCONECTED TO WIFI"); + Serial.println("IP: " + String(WiFi.localIP())); + } + + bool buttonState = digitalRead(SONOFF_BUT); + HTTPClient http; + http.begin(server); + http.addHeader("Content-Type", "text/plain"); //Specify content-type header + String requestJson = "{"; + requestJson += "\"tocken\":\"" + String(hwId) + "\""; + + if (buttonState == true) { + requestJson += ","; + requestJson += "\"on/off\":{"; + requestJson += "\"value\":\"" + String(~lastState) + "\","; + requestJson += "\"unit\":\"\""; + requestJson += "}"; + } + + while(buttonState == true) { + delay(50); // keeps a small delay + } + + requestJson += "}"; + Serial.println("JSON: " + requestJson); + + int httpCode = http.POST(requestJson); + String payload = http.getString(); //Get the response payload + + Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code + Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload + + DynamicJsonDocument doc(1024); + deserializeJson(doc, payload); + + string hostname = doc["device"]["hostname"]; + sleepTime = doc["device"]["sleepTime"]; + + WiFi.hostname(hostname); + + int state = doc["state"]; + + if (state == 1 && lastState == 0) { + Serial.println("ON"); + digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW + digitalWrite(SONOFF_LED, LOW); // Turn the LED on by making the voltage LOW + } else { + Serial.println("OFF"); + digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW + digitalWrite(SONOFF_LED, HIGH); // Turn the LED on by making the voltage LOW + } + + lastState = state; + delay(1000); +} \ No newline at end of file diff --git a/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v1.ino b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v1.ino new file mode 100644 index 0000000..d11e6b1 --- /dev/null +++ b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v1.ino @@ -0,0 +1,94 @@ +//Includes +#include +#include +#include + +//Variables +const char* ssid = " "; +const char* pasw = ""; +const char* server = "http://dev.steelants.cz/vasek/home/api.php"; +const char* hwId = ""; +int lastState = 0; +int reconectAtemptsMax = 10; //time to wait before restart + +//Constant +#define SONOFF 12 +#define SONOFF_LED 13 +#define SONOFF_BUT 0 + + +void setup() { + Serial.begin(9600); + delay(10); + Serial.println('\n'); + Serial.println("HW: " + String(hwId)); + + pinMode(SONOFF, OUTPUT); + pinMode(SONOFF_LED, OUTPUT); + pinMode(SONOFF_BUT, INPUT); + + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, pasw); + Serial.print("Connecting to "); + Serial.print(ssid); Serial.println(" ..."); + + int i = 0; + while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect + delay(1000); + Serial.print(++i); Serial.print(' '); + } + + Serial.println('\n'); + Serial.println("Connection established!"); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); +} + +void loop() { + StaticJsonDocument<200> jsonContent; + jsonContent["token"] = hwId; + + if (!digitalRead(SONOFF_BUT)){ + jsonContent["values"]["on/off"]["value"] = (int) !lastState; + if (!lastState == 1) { + digitalWrite(SONOFF, HIGH) + } else if (!lastState == 0){ + digitalWrite(SONOFF, LOW) + } + while(!digitalRead(SONOFF_BUT)) { + delay(100); + } + } + + String requestJson = ""; + serializeJson(jsonContent, requestJson); + Serial.println("JSON: " + requestJson); + + HTTPClient http; + http.begin(server); + 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(); + + Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code + Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload + + deserializeJson(jsonContent, payload); + String hostname = jsonContent["device"]["hostname"]; + int state = jsonContent["value"]; + WiFi.hostname(hostname); + + if (state != lastState){ + if (state == 1 && lastState == 0) { + Serial.println("ON"); + digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW + } else { + Serial.println("OFF"); + digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW + } + } + lastState = state; +} diff --git a/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino new file mode 100644 index 0000000..a4602ba --- /dev/null +++ b/_FIRMWARE/firmwares/Sonoff_S20/Sonoff_S20_v2.ino @@ -0,0 +1,136 @@ +//Includes +#include +#include +#include + +//Variables +const char* ssid = " "; +const char* pasw = ""; +const char* hwId = ""; +const char* server = "http://dev.steelants.cz/vasek/home/api.php"; +int unsuccessfulRounds = 0; //time to wait before restart +int lastState = 0; + +//Pins +#define SONOFF 12 +#define SONOFF_LED 13 +#define SONOFF_BUT 0 + +void setup() { + Serial.begin(9600); + while (!Serial) continue; + delay(10); + Serial.println('\n'); + Serial.println("HW: " + String(hwId)); + + pinMode(SONOFF, OUTPUT); + pinMode(SONOFF_LED, OUTPUT); + pinMode(SONOFF_BUT, INPUT); + + WiFi.persistent(false); + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, pasw); + checkConnection(); + + Serial.println('\n'); + Serial.println("Connection established!"); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); +} + +void loop() { + StaticJsonDocument<200> jsonContent; + jsonContent["token"] = hwId; + + if (!digitalRead(SONOFF_BUT)){ + jsonContent["values"]["on/off"]["value"] = (int) !lastState; + if (!lastState == 1) { + digitalWrite(SONOFF, HIGH) + } else if (!lastState == 0){ + digitalWrite(SONOFF, LOW) + } + while(!digitalRead(SONOFF_BUT)) { + delay(100); + } + } + + String requestJson = ""; + serializeJson(jsonContent, requestJson); + Serial.println("JSON: " + requestJson); + + //HTTP CLIENT + HTTPClient http; + http.begin(server); + 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(); + + Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code + Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload + + deserializeJson(jsonContent, payload); + String hostname = jsonContent["device"]["hostname"]; + int state = jsonContent["value"]; + + + DeserializationError error = deserializeJson(doc, httpPayload); + + //configuration setup + String hostName = doc["device"]["hostname"]; + String ipAddress = doc["device"]["ipAddress"]; + String state = doc["state"]; + + + if (state != "succes") { + unsuccessfulRounds++; + Serial.println("UNSUCCESSFUL ROUND NUMBER " + unsuccessfulRounds + "FROM 5"); + } else if (state == "succes") { + unsuccessfulRounds = 0; + } + + //Set static ip + IPAddress addr; + if (addr.fromString(ipAddress)) { + IPAddress ip(addr); + Serial.print("IP address:\t"); + Serial.println(WiFi.localIP()); + } + + WiFi.hostname(hostName); + + if(unsuccessfulRounds == 5) { + Serial.println("RESTARTING ESP"); + ESP.restart() + } + + if (state != lastState){ + if (state == 1 && lastState == 0) { + Serial.println("ON"); + digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW + } else { + Serial.println("OFF"); + digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW + } + } + lastState = state; +} + +bool checkConnection() { + int count = 0; + Serial.print("Waiting for Wi-Fi connection"); + while ( count < 30 ) { + if (WiFi.status() == WL_CONNECTED) { + Serial.println(); + Serial.println("Connected!"); + return (true); + } + delay(500); + Serial.print("."); + count++; + } + Serial.println("Timed out."); + return false; +} diff --git a/api.php b/api.php index a65a8fd..41b0db6 100644 --- a/api.php +++ b/api.php @@ -189,6 +189,7 @@ if ($values != null || $values != "") { $jsonAnswer = [ 'device' => [ 'hostname' => $hostname, + 'ipAddress' => $device['ip_address'], ], 'state' => 'succes', ]; @@ -223,6 +224,7 @@ if ($values != null || $values != "") { 'device' => [ 'hostname' => $device['name'], 'sleepTime' => $device['sleep_time'], + 'ipAddress' => $device['ip_address'], ], 'state' => 'succes', 'value' => $subDeviceLastReordValue diff --git a/app/class/DeviceManager.php b/app/class/DeviceManager.php index 21f8e8e..6ae72ae 100644 --- a/app/class/DeviceManager.php +++ b/app/class/DeviceManager.php @@ -23,9 +23,11 @@ class DeviceManager{ } public function create ($name, $token) { + $defaultRoom = RoomManager::getDefaultRoomId(); $device = array ( 'name' => $name, 'token' => $token, + 'room_id' => $defaultRoom, ); try { Db::add ('devices', $device); diff --git a/app/class/NetworkManager.php b/app/class/NetworkManager.php new file mode 100644 index 0000000..247e0df --- /dev/null +++ b/app/class/NetworkManager.php @@ -0,0 +1,18 @@ +
echo('l_permission'); ?>
- +
- echo('l_owner'); ?>
- +
/>echo('l_read'); ?> />echo('l_use'); ?> />echo('l_edit'); ?> - +
@@ -62,15 +62,22 @@
Token:
- +

echo('t_networkSetting'); ?>

Type:
-
IP:
- + +
+
+
Subnet:
+ +
+
+
Gateway:
+
@@ -114,19 +121,19 @@
-
-
-
&#x
+
+
+
&#x
+
+
+
+
-
-
+
+
+ +
-
-
-
- -
-
@@ -143,10 +150,10 @@
- - - - + + + +
diff --git a/app/views/Home.php b/app/views/Home.php index b5156d6..6afa75c 100644 --- a/app/views/Home.php +++ b/app/views/Home.php @@ -157,6 +157,7 @@ class Home extends Template 'room' => $deviceData['room_id'], 'token' => $deviceData['token'], 'type' => $deviceData['type'], + 'ip' => $deviceData['ip_address'], 'sleepTime' => $deviceData['sleep_time'], 'approved' => $deviceData['approved'], 'permission' => $permissionArray,