Re: Wifi zásuvka
Napsal: 14 kvě 2020, 21:50
No tohle chapu, jen nevim presne jak zakomponovat do toho form...
České fórum pro všechny nadšence do Arduina a dalších technologií.
https://forum.hwkitchen.cz/
Tedy:u toho formu si dej do action nějaký skript, který tu hodnotu updatne v db a udělá redirect zpět na výpis s tlačítkem
Kód: Vybrat vše
<form action="update_led.php" method="POST">
... hidden ... submit
</form>
Kód: Vybrat vše
$sql = 'UPDATE arduino_data SET hodnota='.(int)$_POST['hodnota'].' WHERE modul="led"';
$doSql = $mysqli->query($sql);
header('Location: jmeno_skriptu_s_formem.php');
Kód: Vybrat vše
$mysqli = mysqli_connect($server, $user, $pass, $db); //připojení k MySQL
$sql = 'SELECT * FROM arduino_data WHERE modul="led"';
$doSql = $mysqli->query($sql);
$hodnota = $doSql->fetch_assoc()['hodnota']; //načte hodnotu led z DB
$sqlzapis = 'UPDATE arduino_data SET hodnota='.(int)$_POST['stav'].' WHERE modul="led"';
$doSqlzapis = $mysqli->query($sqlzapis);
include 'menu.html';?>
<form action="" method="POST">
<?php if ($hodnota) { ?>
<h1>Světlo za sedačkou</h1>
<input type="hidden" name="stav" value="0">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko" value="1">Zhasnout</button>
<?php } else { ?>
<h1>Světlo za sedačkou</h1>
<input type="hidden" name="stav" value="1">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko" value="0">Rozsvítit</button>
<?php } ?>
</form>
Kód: Vybrat vše
$mysqli = mysqli_connect($server, $user, $pass, $db); //připojení k MySQL
$sql = 'SELECT * FROM arduino_data WHERE modul="led"';
$doSql = $mysqli->query($sql);
$hodnota = $doSql->fetch_assoc()['hodnota']; //načte hodnotu led z DB
$sqlzapis = 'UPDATE arduino_data SET hodnota='.(int)$_POST['stav'].' WHERE modul="led"';
$doSqlzapis = $mysqli->query($sqlzapis);
$sql2 = 'SELECT * FROM arduino_data WHERE modul="led2"';
$doSql2 = $mysqli->query($sql2);
$hodnota2 = $doSql2->fetch_assoc()['hodnota']; //načte hodnotu led z DB
$sqlzapis2 = 'UPDATE arduino_data SET hodnota='.(int)$_POST['stav2'].' WHERE modul="led2"';
$doSqlzapis2 = $mysqli->query($sqlzapis2);
include 'menu.html';?>
<form action="" method="POST">
<?php if ($hodnota) { ?>
<h1>Světlo za sedačkou</h1>
<input type="hidden" name="stav" value="0">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko" value="1" >Zhasnout</button>
<?php } else { ?>
<h1>Světlo za sedačkou</h1>
<input type="hidden" name="stav" value="1">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko" value="0" >Rozsvítit</button>
<?php } ?>
</form>
<form action="" method="POST">
<?php if ($hodnota2) { ?>
<h1>Světlo u TV</h1>
<input type="hidden" name="stav2" value="0">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko2" value="1" >Zhasnout</button>
<?php } else { ?>
<h1>Světlo u TV</h1>
<input type="hidden" name="stav2" value="1">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko2" value="0" >Rozsvítit</button>
<?php } ?>
</form>
Kód: Vybrat vše
$mysqli = mysqli_connect($server, $user, $pass, $db); //připojení k MySQL
$sql = 'SELECT * FROM arduino_data WHERE modul="led"';
$doSql = $mysqli->query($sql);
$hodnota = $doSql->fetch_assoc()['hodnota']; //načte hodnotu led z DB
$sql2 = 'SELECT * FROM arduino_data WHERE modul="led2"';
$doSql2 = $mysqli->query($sql2);
$hodnota2 = $doSql2->fetch_assoc()['hodnota']; //načte hodnotu led z DB
include 'menu.html';
?>
<form action="update_led1.php" method="POST">
<?php if ($hodnota) { ?>
<h1>Světlo za sedačkou</h1>
<input type="hidden" name="stav" value="0">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko" value="1" >Zhasnout</button>
<?php } else { ?>
<h1>Světlo za sedačkou</h1>
<input type="hidden" name="stav" value="1">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko" value="0" >Rozsvítit</butto$
<?php } ?>
</form>
<form action="update_led2.php" method="POST">
<?php if ($hodnota2) { ?>
<h1>Světlo u TV</h1>
<input type="hidden" name="stav2" value="0">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko2" value="1" >Zhasnout</butto$
<?php } else { ?>
<h1>Světlo u TV</h1>
<input type="hidden" name="stav2" value="1">
<button style="font-size : 40px; height:100px; width:300px" type="submit" name="tlacitko2" value="0" >Rozsvítit</butt$
<?php } ?>
</form>
Kód: Vybrat vše
$mysqli = mysqli_connect($server, $user, $pass, $db); //připojení k MySQL
$sqlzapis = 'UPDATE arduino_data SET hodnota='.(int)$_POST['stav'].' WHERE modul="led"';
$doSqlzapis = $mysqli->query($sqlzapis);
header('Location: led.php');
Kód: Vybrat vše
#include <SPI.h>
#include <UIPEthernet.h> // Used for Ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE };
#define BUFF_DELKA 255
char buffer[BUFF_DELKA]; //zásobník na příchozí řetězec
// ********************************************
// ***************EDITACE *********************
// ********NUTNO DODRŽET ADRESU SOUBORU *******
// ********/jirka/reset1.php reset2.php *******
// ********************************************
// **** NASTAVENI SITE****
IPAddress ip(10, 10, 111, 80); //IP ADRESA
IPAddress gateway(10, 10, 111, 1); // VYCHOZI BRANA
IPAddress subnet(255, 255, 255, 0); // MASKA
IPAddress dnServer(8, 8, 4, 4); // DNS
int ID_MODULU = 1;
char serverName[] = "yxyxyxyx"; // webserver
// ********************************************
// ********************************************
// ********************************************
EthernetClient client;
String readString;
int x=0; //pocitadlo riadkov
char lf=10; //line feed character
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
Serial.print("ip-");
Serial.println( Ethernet.localIP());
Serial.print("Subnet mask-");
Serial.println( Ethernet.subnetMask());
Serial.print("Gateway-");
Serial.println( Ethernet.gatewayIP());
Serial.print("DNS-");
Serial.println( Ethernet.dnsServerIP());
}
void loop() {
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
client.println("GET http://yxyxyxyx/yxyxyxyx/reset1.php");
Serial.println("GET http://yxyxyxyx/yxyxyxyx/reset1.php");
client.println("Host: konycz.eu");
client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
client.println("Connection: close");
client.println();
delay(1000);
int inBuffer = 0;
for(int i = 0; i < BUFF_DELKA; i++){ //vyprázdní buffer
buffer[i] = '\\0'; //nastaví všechny prvky buffer na znak konce řetězce
}
while(client.available()){
buffer[inBuffer] = client.read();
inBuffer++;
}
if(inBuffer == 9){
if(buffer[0] == 'r' && buffer[1] == 'e' && buffer[2] == 's'){
if(buffer[8] == '1'){
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, LOW);
}
Serial.println(buffer[8]);
}
}
client.stop();
}
Serial.println(buffer[8]);
delay(6000);
}
Kód: Vybrat vše
#include <SPI.h>
#include <UIPEthernet.h> // Used for Ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE };
unsigned long cas = 0;
// ********************************************
// ***************EDITACE *********************
// ********NUTNO DODRŽET ADRESU SOUBORU *******
// ********/jirka/reset1.php reset2.php *******
// ********************************************
// **** NASTAVENI SITE****
IPAddress ip(10, 10, 111, 80); //IP ADRESA
IPAddress gateway(10, 10, 111, 1); // VYCHOZI BRANA
IPAddress subnet(255, 255, 255, 0); // MASKA
IPAddress dnServer(8, 8, 4, 4); // DNS
int ID_MODULU = 1;
char serverName[] = "yxyxyxyx"; // webserver
// ********************************************
// ********************************************
// ********************************************
EthernetClient client;
void setup() {
Serial.begin(9600);
pinMode(4, OUTPUT);
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
Serial.print("ip-");
Serial.println( Ethernet.localIP());
Serial.print("Subnet mask-");
Serial.println( Ethernet.subnetMask());
Serial.print("Gateway-");
Serial.println( Ethernet.gatewayIP());
Serial.print("DNS-");
Serial.println( Ethernet.dnsServerIP());
}
void loop() {
if (millis() - cas >= 6000 || cas == 0) {
cas = millis();
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
client.println("GET http://yxyxyxyx/yxyxyxyx/reset1.php HTTP/1.0");
Serial.println("GET http://yxyxyxyx/yxyxyxyx/reset1.php");
client.println("Host: konycz.eu");
client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
client.println("Connection: close");
client.println();
delay(1000);
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line); //ak chceme vypísať HTTP header
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("Nacitany payload response:");
Serial.println(line); //odpoveď webservera - naše dáta
} else {
Serial.println("Pripojenie na webserver sa nepodarilo");
}
client.stop();
}
}