Sonoff basic, s20 firmware update

This commit is contained in:
JonatanRek 2019-08-23 23:03:55 +02:00
parent f089205dc0
commit 4a87204e16
3 changed files with 37 additions and 43 deletions

View File

@ -20,7 +20,7 @@ int reconectAtemptsMax = 10; //time to wait before restart
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
delay(10); delay(10);
Serial.println('\n'); Serial.println('\n');
Serial.println("HW: " + String(hwId)); Serial.println("HW: " + String(hwId));
pinMode(SONOFF, OUTPUT); pinMode(SONOFF, OUTPUT);
@ -32,31 +32,31 @@ void setup() {
WiFi.begin(ssid, pasw); WiFi.begin(ssid, pasw);
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ..."); Serial.print(ssid); Serial.println(" ...");
int i = 0; int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000); delay(1000);
Serial.print(++i); Serial.print(' '); Serial.print(++i); Serial.print(' ');
} }
Serial.println('\n'); Serial.println('\n');
Serial.println("Connection established!"); Serial.println("Connection established!");
Serial.print("IP address:\t"); Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
void loop() { void loop() {
Serial.println("CONECED");
bool buttonState = !digitalRead(SONOFF_BUT);
Serial.println("CONECED" + String(buttonState));
StaticJsonDocument<200> jsonContent; StaticJsonDocument<200> jsonContent;
jsonContent["token"] = hwId; jsonContent["token"] = hwId;
if (!digitalRead(SONOFF_BUT)){ if (!digitalRead(SONOFF_BUT)){
jsonContent["values"]["on/off"]["value"] = (int) !lastState; jsonContent["values"]["on/off"]["value"] = (int) !lastState;
if (!lastState == 1) {
digitalWrite(SONOFF, HIGH)
} else if (!lastState == 0){
digitalWrite(SONOFF, LOW)
}
while(!digitalRead(SONOFF_BUT)) { while(!digitalRead(SONOFF_BUT)) {
delay(100); delay(100);
} }
@ -77,24 +77,18 @@ void loop() {
Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload
deserializeJson(jsonContent, payload); deserializeJson(jsonContent, payload);
String hostname = jsonContent["device"]["hostname"]; String hostname = jsonContent["device"]["hostname"];
int state = jsonContent["value"];
WiFi.hostname(hostname); WiFi.hostname(hostname);
int state = jsonContent["value"];
if (state != lastState){ if (state != lastState){
if (state == 1 && lastState == 0) { if (state == 1 && lastState == 0) {
Serial.println("ON"); Serial.println("ON");
digitalWrite(SONOFF, HIGH); // Turn the LED on by making the voltage LOW 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 {
} else { Serial.println("OFF");
Serial.println("OFF"); digitalWrite(SONOFF, LOW); // Turn the LED on by making the voltage LOW
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; lastState = state;
delay(1000);
} }

20
api.php
View File

@ -15,16 +15,16 @@ foreach (["class", "views"] as $dir) {
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME); Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
//Filtrování IP adress //Filtrování IP adress
if (DEBUGMOD != 1) { /*if (DEBUGMOD != 1) {
if (!in_array($_SERVER['REMOTE_ADDR'], HOMEIP)) { if (!in_array($_SERVER['REMOTE_ADDR'], HOMEIP)) {
echo json_encode(array( echo json_encode(array(
'state' => 'unsuccess', 'state' => 'unsuccess',
'errorMSG' => "Using API from your IP insn´t alowed!", 'errorMSG' => "Using API from your IP insn´t alowed!",
)); ));
header("HTTP/1.1 401 Unauthorized"); header("HTTP/1.1 401 Unauthorized");
exit(); exit();
}
} }
}*/
@ -41,7 +41,7 @@ if (isset($obj['user']) && $obj['user'] != ''){
} }
//automationExecution //automationExecution
AutomationManager::executeAll(); //AutomationManager::executeAll();
//Record Cleaning //Record Cleaning
RecordManager::clean(RECORDTIMOUT); RecordManager::clean(RECORDTIMOUT);