I would like to control a relay using Wemos D! R1 via a website from anywhere. I managed to control it using local web server and now would like to step it up further.

I'm using the following code which connects to this website and retrieve data as JSON perfectly.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "SSID";
const char* password = "PASS";
void setup () {
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
void loop() {
    if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
        HTTPClient http;  //Declare an object of class HTTPClient
        http.begin("http://jsonplaceholder.typicode.com/users/1");  //Specify request destination
        int httpCode = http.GET();                                                                  //Send the request
        if (httpCode > 0) { //Check the returning code
            String payload = http.getString();   //Get the request response payload
            Serial.println("");                  // Empty Line
            Serial.println(payload);             //Print the response payload
            //////////////// Json code
              StaticJsonDocument<2000> doc;
              DeserializationError error = deserializeJson(doc, payload);
              if (error) {
                Serial.print(F("deserializeJson() failed: "));
              const char* sensor = doc["name"];
              const char* username = doc["username"];
              const char* email = doc["email"];
              const char* address = doc["address"];
            //////////////// End of Json Code
              http.end();   //Close connection
              while ( 0 < 1 ){ delay(10000); }  // to break the loop
    delay(10);    //Send a request every 30 seconds

I made a web page that makes AJAX call to a PHP file to retrieve data from MYSQL DB, but when I run it, the monitor keeps printing empty lines forever although connected successfully to my WIFI.

        url: "test.php",
        type: 'GET',
        dataType: 'json', // added data type
    data: ({mobile: 'hi'}),
        success: function(res) {

The result I'm getting from AJAX is (which you can check visiting this link:


I'm using the following code on php file:

header('Content-type: application/json');   
require 'functions.php'; 
header("Access-Control-Allow-Origin: *");
    $catrecord = mysqli_query($link,"SELECT * FROM `ard`");
        while($catinfo = mysqli_fetch_array($catrecord, MYSQLI_ASSOC)){ $Cat = $catinfo['status']; }
    $responseArray = array('status' => $Cat );          

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);     
    header('Content-Type: application/json');   
    echo $encoded;
} else {    
    echo $responseArray['status'];      
  • 1
    check the returning code. 200 is OK. all others are an error. negative return values are a local error. positive are a HTTP error
    – Juraj
    Aug 28, 2020 at 19:59
  • What is the exact question? You don't know how to modified the Arduino code to receive json payload of {"status":"on"}?
    – hcheung
    Aug 29, 2020 at 1:21
  • you can use port forwarding or websockets to control from the WAN. or hammer the php site from the wemos, but that's not as good as talking to the esp server directly or having always-on streaming.
    – dandavis
    Aug 29, 2020 at 7:31
  • @dandavis "talking to the esp server directly or having always-on streaming." would you please elaborate more or share a link about this methodology. Thx Aug 29, 2020 at 11:07
  • You should do what @Juraj suggested. The way it's written now, your code won't correctly report errors which would hide the cause of the problem you're seeing.
    – romkey
    Aug 29, 2020 at 15:20


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.