ESP32 无法连接在线托管,而在本地服务器中它工作正常[已关闭]

问题描述 投票:0回答:1

我尝试使用免费托管000webhostapp使我的传感器监控数据在线,我只需上传我的htdocs文件,使其匹配数据库名称,表名称并设置连接数据库。但它仍然不起作用。 但如果我尝试在本地服务器 XAMPP 上运行代码,它就可以正常工作。

我匹配数据库名称,数据库表,作为我的本地数据库。 我将数据库用户名、密码与在线托管相匹配。

这是我的代码:

#include "WiFi.h"
#include "HTTPClient.h"

//variabel untuk set konektifitas hotspot
const char* ssid = "KAPRODI-TEI";  //"mikrofologi";
const char* pass = "tanyapakadib"; //"Nom3rhapeku1";

//variabel host server lokal/ online
const char* host = "monsen32.000webhostapp.com";   //"192.168.1.8";

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));

  //koneksi ke WiFi
  WiFi.begin(ssid, pass);
  Serial.println("Mengubungkan...");
  //Tamilan sermon ketika wifi belum terkoneksi
  while(WiFi.status() != WL_CONNECTED){
    Serial.print("."); delay(500);
  }
  //ketika sudah terkoneksi
  Serial.print("WiFi Terhubung");
}

void loop() {
  float suhu = random(32, 41);
  int kelembaban = random(16, 31);
  int ldr = random(200, 301);
  
  Serial.print("Nilai Sensor suhu: "); Serial.println(suhu);
  Serial.print("Nilai Sensor kelembaban: "); Serial.println(kelembaban);
  Serial.print("Nilai Sensor LDR: "); Serial.println(ldr);
  Serial.println("");

  //kirim data ke server lokal/ online
  WiFiClient client ;  //"client" disini merupakan variabel bebas
  //inisialisasi port web server apache 80
  const int httpPort = 80;
  if( !client.connect(host, httpPort) ){ //jk koneksi ke variabel host melalui port gagal
    Serial.println("Koneksi Gagal");     //maka tampilkan notif "koneksi gagal"
    return;                              //kembali ke loop pertama
  }
  else if(client.connect(host, httpPort)){
    Serial.println("Koneksi HTTP Berhasil");
  }

  //kondisi sudah bisa terhubung dengan server
  //maka kirim data sensor ke database

  String Link;      //variabel link lokal
  HTTPClient http;  //

  //Link = "http://" + String(host) + "/multisensor/kirimdata.php?suhu=" + String(suhu) + "&kelembaban=" + String (kelembaban) + "&ldr=" + String (ldr);
  Link = "https://" + String(host) + "/kirimdata.php?suhu=" + String(suhu) + "&kelembaban=" + String (kelembaban) + "&ldr=" + String (ldr);


  //eksekusi alamat link yang ada
  http.begin(Link);
  http.GET();

  //baca respon setelah berhasil kirim nilai sensor
  String respon = http.getString();
  Serial.println(respon);
  http.end();

  delay(1000);
}
php monitoring esp32
1个回答
0
投票

#include "WiFi.h"
#include "HTTPClient.h"

//variabel untuk set konektifitas hotspot
const char* ssid = "KAPRODI-TEI";  //"mikrofologi";
const char* pass = "tanyapakadib"; //"Nom3rhapeku1";

//variabel host server lokal/ online
const char* host = "monsen32.000webhostapp.com";   //"192.168.1.8";

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));

  //koneksi ke WiFi
  WiFi.begin(ssid, pass);
  Serial.println("Mengubungkan...");
  //Tamilan sermon ketika wifi belum terkoneksi
  while(WiFi.status() != WL_CONNECTED){
    Serial.print("."); delay(500);
  }
  //ketika sudah terkoneksi
  Serial.print("WiFi Terhubung");
}

void loop() {
  float suhu = random(32, 41);
  int kelembaban = random(16, 31);
  int ldr = random(200, 301);
  
  Serial.print("Nilai Sensor suhu: "); Serial.println(suhu);
  Serial.print("Nilai Sensor kelembaban: "); Serial.println(kelembaban);
  Serial.print("Nilai Sensor LDR: "); Serial.println(ldr);
  Serial.println("");

  //kirim data ke server lokal/ online
  WiFiClient client ;  //"client" disini merupakan variabel bebas
  //inisialisasi port web server apache 80
  const int httpPort = 80;
  if( !client.connect(host, httpPort) ){ //jk koneksi ke variabel host melalui port gagal
    Serial.println("Koneksi Gagal");     //maka tampilkan notif "koneksi gagal"
    return;                              //kembali ke loop pertama
  }
  else if(client.connect(host, httpPort)){
    Serial.println("Koneksi HTTP Berhasil");
  }

  //kondisi sudah bisa terhubung dengan server
  //maka kirim data sensor ke database

  String Link;      //variabel link lokal
  HTTPClient http;  //

  //Link = "http://" + String(host) + "/multisensor/kirimdata.php?suhu=" + String(suhu) + "&kelembaban=" + String (kelembaban) + "&ldr=" + String (ldr);
  Link = "https://" + String(host) + "/kirimdata.php?suhu=" + String(suhu) + "&kelembaban=" + String (kelembaban) + "&ldr=" + String (ldr);


  //eksekusi alamat link yang ada
  http.begin(Link);
  http.GET();

  //baca respon setelah berhasil kirim nilai sensor
  String respon = http.getString();
  Serial.println(respon);
  http.end();

  delay(1000);
}

© www.soinside.com 2019 - 2024. All rights reserved.