aostvapp wrote:hi i am doing a project to read values from my php database the project consist of a main page with 6 on/off button i was able to update my database sucessfully but my problem is how to read each state from database with the nodemcu i used your http get example and i can see the states on the serial monitor how do i use these data to turn on and off led on the board thank you
Hi. sorry for the late reply. if you have a JSON file uploaded to your server like this:
ATTENTION: there are some parts marked as MODIFY THIS!!!!!! remember to modify them
{
"on_off1" = "1",
"on_off2" = "1",
"on_off3" = "1",
"on_off4" = "1",
"on_off5" = "1",
"on_off6" = "1"
}
then you can read it with these functions:
Code: Select allString SSL_send_get_req()
{
Serial.println("SSL_send_get_req ==>");
String out;
if (WiFi.status() == WL_CONNECTED) {
std::unique_ptr<BearSSL::WiFiClientSecure>http_client(new BearSSL::WiFiClientSecure);
const uint8_t fingerprint[20] = {0xdd, 0xa5, 0x2d, 0x31, 0x25,
0x31, 0xae, 0x7a, 0x10, 0x0b,
0x68, 0xba, 0x22, 0x84, 0x1a,
0x94, 0xec, 0x79, 0xb4, 0xbb
}; // MODIFY THIS
http_client->setFingerprint(fingerprint);
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if (https.begin(*http_client, "https://username:password@server/JSON_FILE_NAME.php")) { // MODIFY THIS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = https.GET();
Serial.printf("http code is: ");
Serial.println(httpCode);
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
out = payload;
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
}
return out;
}
void READ_JSON()
{
Serial.println("READ_JSON ==>");
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
mqtt_client.setInsecure();
Serial.println("CHECK_FINGERPRINT");
String respons = SSL_send_get_req();
if (respons != "" )
{
Serial.println("-------------------------------------------------");
Serial.println(respons);
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(X) + Y; // MODIFY THIS
DynamicJsonDocument doc(capacity);
// Parse JSON object
auto error = deserializeJson(doc, respons);
switch (error.code()) {
case DeserializationError::Ok:
Serial.print(F("Deserialization succeeded"));
Serial.println();
break;
case DeserializationError::InvalidInput:
Serial.print(F("Invalid input!"));
Serial.println();
return;
case DeserializationError::NoMemory:
Serial.print(F("Not enough memory"));
Serial.println();
return;
default:
Serial.print(F("Deserialization failed"));
Serial.println();
return;
}
// Decode JSON/Extract values
Serial.println(F("Response:"));
String _on_off1 = doc["on_off1"].as<char*>();
Serial.println(_on_off1);
String _on_off2 = doc["on_off2"];
Serial.println(_on_off);
String _on_off3 = doc["on_off3"].as<char*>();
Serial.println(_on_off3);
String _on_off4 = doc["on_off4"].as<char*>();
Serial.println(_on_off4);
String _on_off5 = doc["on_off5"].as<char*>();
Serial.println(_on_off5);
String _on_off6 = doc["on_off6"].as<char*>();
Serial.println(_on_off6);
on_off1 = _on_off1.toInt();
on_off2 = _on_off2.toInt();
on_off3 = _on_off3.toInt();
on_off4 = _on_off4.toInt();
on_off5 = _on_off5.toInt();
on_off6 = _on_off6.toInt();
}
else
{
Serial.println("Error in response");
}
}
}