使用esp32、+php+mysql的“未定义索引”

k3fezbri  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(269)

我想做一个程序在我的数据库里保存随机数。
我用的是 pushbutton 每次我按下它,它就会产生一个随机数。
我在esp32上运行了代码,效果很好。问题是php无法识别变量,似乎:
未定义索引:第5行的c:\wamp64\www\tentiva\random.php中的randnumber
arduino代码


# include <SPI.h>

# include <WiFi.h>

# include <WiFiClient.h>

# include <WiFiServer.h>

# include <WiFiUdp.h>

const char ssid[] = "Baja Preto";
const char psw[] = "seucosta123";
const int http_port = 80;
const char* http_site = "http://mycomputer";
IPAddress server(192,168,0,15);

WiFiClient client;
int switchState = 0;
int randNumber;
void setup() {
  delay(3000);
  Serial.begin(9600);
  Serial.println("Gravando dados no DB via PHP");
  Serial.println("Aguardando conexão");
  WiFi.begin(ssid, psw);
  while ( WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.print("\nWIFI conectado com sucesso: ");
  Serial.println(ssid);
  pinMode(14, INPUT);

}

void loop() {
  switchState = digitalRead(14);
  if (switchState == HIGH)
  {
    randNumber = random(300);
    delay(500);
    Serial.println(randNumber);
    Serial.println("Gravando dados no BD: ");

     // Envio dos dados do sensor para o servidor via GET
     if ( !getPage((int)randNumber)) {
      Serial.println("GET request failed");
     }
  }
}

bool getPage(int randNumber)  {
  if (!client.connect(server, http_port)) {
    Serial.println("Falha na conexao com o site: ");
    return false;
  }
  String param = "?randNumber=" + String(randNumber);
  Serial.println(param);
  client.println("GET localhost/tentativa/random.php" + param + " HTTP/1.1");
  client.println("Host: ");
  client.println(http_site);
  client.println("Connection: close");

  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
   }
  return true;
}

PHP代码

<?php

# $randNumber = filter_input(INPUT_GET, 'randNumber',

FILTER_SANITIZE_NUMBER_FLOAT);
$randNumber = filter_var ( $_GET['randNumber'], FILTER_SANITIZE_NUMBER_INT );

if (is_null($randNumber)) {
  //Gravar log de erros
  die("Dados inválidos");
} 

$conn = new mysqli("127.0.0.1", "root", "root", "espdata");
if ($conn->connect_error) {
    die("Não foi possivel estabelecer conexao com o BD:" . $conn- 
>connect_error);
} 
$sql="INSERT into esptable (id, sensor) VALUES (0, $randNumber)";

mysqli_query($conn, $sql);
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题