Help with ESP8266 and GET REQUEST

Hi,

I’m needing help to build a project and I’m looking to use a library for my ESP8266 module in my ARDUINO UNO.

If you know a Library to use the ESP8266 I appriciate it.

I need the arduino reading the DB every time to receive a simple value 0 or 1, I just need to know how to receive this answer.

At the moment I am not able to make a code on the arduino with this library: GitHub - itead/ITEADLIB_Arduino_WeeESP8266: An easy-to-use Arduino ESP8266 library besed on AT firmware.

That makes the GET request work, could someone show me a code that would work? Thank you.

My PHP:

<?php
 $servername = "mysql.hostinger.com.br";
 $username = "root";
 $password = "*****";
 $dbname = "esp1";
 $conn = new mysqli($servername, $username, $password);
 
 header("content-type: application/json");

 $sql = "SELECT  * FROM `u582151594_esp82`.`esp1`";
 $result = $conn->query($sql);
 while($data = $result->fetch_object()) {
  $value = $data->value;      
 }
 
 echo '{"value":';
 echo $value;
 echo '}';
 
 $conn->close();
?>

The code returns me:
{“value”:0}

I need the arduino to read this JSON, parse, and remove this VALUE for me to use in my code.

Some help?

Thankss

Start with the HTTP GET example from your library. Adapt that to receive the line containing the value and print it to the serial monitor. Post that code and we can suggest how to extract the value. Since the are only two possible values, that should not be too difficult!

PaulRB:
Start with the HTTP GET example from your library. Adapt that to receive the line containing the value and print it to the serial monitor. Post that code and we can suggest how to extract the value. Since the are only two possible values, that should not be too difficult!

The problem is that this example does not seem to be working, I did test the example and it falls into the else Serial.print ("create tcp err \ r \ n"); Do you have any other library for ESP8266 or do you know a way that would make TCP work?

Thanks.

Here is the example code from github:

/**
 * @example HTTPGET.ino
 * @brief The HTTPGET demo of library WeeESP8266. 
 * @author Wu Pengfei<pengfei.wu@itead.cc> 
 * @date 2015.03
 * 
 * @par Copyright:
 * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version. \n\n
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "ESP8266.h"

#define SSID        "ITEAD"
#define PASSWORD    "12345678"
#define HOST_NAME   "www.baidu.com"
#define HOST_PORT   (80)

ESP8266 wifi(Serial1);

void setup(void)
{
    Serial.begin(9600);
    Serial.print("setup begin\r\n");

    Serial.print("FW Version:");
    Serial.println(wifi.getVersion().c_str());

    if (wifi.setOprToStationSoftAP()) {
        Serial.print("to station + softap ok\r\n");
    } else {
        Serial.print("to station + softap err\r\n");
    }

    if (wifi.joinAP(SSID, PASSWORD)) {
        Serial.print("Join AP success\r\n");

        Serial.print("IP:");
        Serial.println( wifi.getLocalIP().c_str());       
    } else {
        Serial.print("Join AP failure\r\n");
    }
    
    if (wifi.disableMUX()) {
        Serial.print("single ok\r\n");
    } else {
        Serial.print("single err\r\n");
    }
    
    Serial.print("setup end\r\n");
}
 
void loop(void)
{
    uint8_t buffer[1024] = {0};

    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
        Serial.print("create tcp ok\r\n");
    } else {
        Serial.print("create tcp err\r\n");
    }

    char *hello = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\n\r\n";
    wifi.send((const uint8_t*)hello, strlen(hello));

    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    if (len > 0) {
        Serial.print("Received:[");
        for(uint32_t i = 0; i < len; i++) {
            Serial.print((char)buffer[i]);
        }
        Serial.print("]\r\n");
    }

    if (wifi.releaseTCP()) {
        Serial.print("release tcp ok\r\n");
    } else {
        Serial.print("release tcp err\r\n");
    }
    
    while(1);
    
}

Can you post the exact code you are running, in case you made an error?

I don’t use that library, because I don’t use esp8266 wifi adaptors with Uno. I uploaded sketches directly to the esp chip, so I don’t need the Uno at all. I have done this with esp-01 modules and Wemos D1 Mini boards. This means I use different libraries, but the libraries I use would not work for your circuit.

PaulRB:
Here is the example code from github:

/**
  • @example HTTPGET.ino
  • @brief The HTTPGET demo of library WeeESP8266.
  • @author Wu Pengfeipengfei.wu@itead.cc
  • @date 2015.03
  • @par Copyright:
  • Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
  • This program is free software; you can redistribute it and/or
  • modify it under the terms of the GNU General Public License as
  • published by the Free Software Foundation; either version 2 of
  • the License, or (at your option) any later version. \n\n
  • THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  • IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  • FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  • AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  • LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  • OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  • THE SOFTWARE.
    */

#include “ESP8266.h”

#define SSID        “ITEAD”
#define PASSWORD    “12345678”
#define HOST_NAME  “www.baidu.com
#define HOST_PORT  (80)

ESP8266 wifi(Serial1);

void setup(void)
{
    Serial.begin(9600);
    Serial.print(“setup begin\r\n”);

Serial.print(“FW Version:”);
    Serial.println(wifi.getVersion().c_str());

if (wifi.setOprToStationSoftAP()) {
        Serial.print(“to station + softap ok\r\n”);
    } else {
        Serial.print(“to station + softap err\r\n”);
    }

if (wifi.joinAP(SSID, PASSWORD)) {
        Serial.print(“Join AP success\r\n”);

Serial.print(“IP:”);
        Serial.println( wifi.getLocalIP().c_str());     
    } else {
        Serial.print(“Join AP failure\r\n”);
    }
   
    if (wifi.disableMUX()) {
        Serial.print(“single ok\r\n”);
    } else {
        Serial.print(“single err\r\n”);
    }
   
    Serial.print(“setup end\r\n”);
}

void loop(void)
{
    uint8_t buffer[1024] = {0};

if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
        Serial.print(“create tcp ok\r\n”);
    } else {
        Serial.print(“create tcp err\r\n”);
    }

char hello = “GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\n\r\n”;
    wifi.send((const uint8_t
)hello, strlen(hello));

uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    if (len > 0) {
        Serial.print(“Received:[”);
        for(uint32_t i = 0; i < len; i++) {
            Serial.print((char)buffer[i]);
        }
        Serial.print("]\r\n");
    }

if (wifi.releaseTCP()) {
        Serial.print(“release tcp ok\r\n”);
    } else {
        Serial.print(“release tcp err\r\n”);
    }
   
    while(1);
   
}




Can you post the exact code you are running, in case you made an error?

I don't use that library, because I don't use esp8266 wifi adaptors with Uno. I uploaded sketches directly to the esp chip, so I don't need the Uno at all. I have done this with esp-01 modules and Wemos D1 Mini boards. This means I use different libraries, but the libraries I use would not work for your circuit.

Im trying the same code that you display here, but it fail when it try to create the tcp

UNO

The connection should be like these:

ESP8266_TX->RX(D3)
ESP8266_RX->TX(D2)
ESP8266_CH_PD->3.3V
ESP8266_VCC->3.3V
ESP8266_GND->GND

This is a concern to me. The 3.3V output of Uno cannot provide enough current for an esp8266. This could damage the Uno and cannot provide enough current for the esp to connect to WiFi. Also the Uno's pins output 5V and this could damage the esp.

Can you post all of the serial monitor output when you reset the Arduino? Use code tags.

Are you using the "www.baidu.com" hostname or did you change that part?

The code issues aside....

I just wanted to chime in before you get too deep into the project, to say that I think you should re-think your approach here.

While in general terms... your project seems feasible.. in reality it will NOT be very efficient.

You state this: "I need the arduino reading the DB every time to receive a simple value 0 or 1, I just need to know how to receive this answer."

To me.. if you need to keep 'polling' the DB to check on the status of a variable/state... this timed/looped (continuous) polling over HTTP is a waste.

What you need and should use is a MQTT protocol. It is a simple subscribe and publish type of protocol/server... where the ESP8266 device just 'subscribe' to a topic/event.. and when ANY changes happend.. the MQTT server pushed out this notification/change to all subscribed device.

No wasting time/bandwidth polling over and over just to see if a change has happened.

Now.. this may be new to you and seem very daunting.. but it was new to me a few months back and it was very easy to pick up.

I used a Raspberry Pi 3.

I configured it as an MQTT server.
I also configured it to run as a web server (Apache, MySQL & PHP)

So not only could I control my home network via one, central MQTT server in my house...I could also log data/values for logging/history purposes... but execute other things via PHP(other database writes/updates, website/page updates..etc)

For about $35.00 USD and roughly 40+minutes of your time from start to finish and testing... I think its a good investment.

PaulRB:
This is a concern to me. The 3.3V output of Uno cannot provide enough current for an esp8266. This could damage the Uno and cannot provide enough current for the esp to connect to WiFi. Also the Uno's pins output 5V and this could damage the esp.

Can you post all of the serial monitor output when you reset the Arduino? Use code tags.

Are you using the "www.baidu.com" hostname or did you change that part?

Yes I saw about the voltage of the ESP8266, and I'm using the same code, it connects to WIFI quietly, just the TCP start up forward that does not happen.

xl97:
The code issues aside....

I just wanted to chime in before you get too deep into the project, to say that I think you should re-think your approach here.

While in general terms... your project seems feasible.. in reality it will NOT be very efficient.

You state this: "I need the arduino reading the DB every time to receive a simple value 0 or 1, I just need to know how to receive this answer."

To me.. if you need to keep 'polling' the DB to check on the status of a variable/state... this timed/looped (continuous) polling over HTTP is a waste.

What you need and should use is a MQTT protocol. It is a simple subscribe and publish type of protocol/server... where the ESP8266 device just 'subscribe' to a topic/event.. and when ANY changes happend.. the MQTT server pushed out this notification/change to all subscribed device.

No wasting time/bandwidth polling over and over just to see if a change has happened.

Now.. this may be new to you and seem very daunting.. but it was new to me a few months back and it was very easy to pick up.

I used a Raspberry Pi 3.

I configured it as an MQTT server.
I also configured it to run as a web server (Apache, MySQL & PHP)

So not only could I control my home network via one, central MQTT server in my house...I could also log data/values for logging/history purposes... but execute other things via PHP(other database writes/updates, website/page updates..etc)

For about $35.00 USD and roughly 40+minutes of your time from start to finish and testing... I think its a good investment.

Thanks for your tip, my project is very simples, my project is something very simple, I do not want to invest much on it, I'm just using what I have in hand, the part of it getting polling the server is something else an alternative for it to read a value that I'll send from a mobile application , I want something to that effect, just that it reads an external value.

But thank you for your days.

As pointed out above there may be a power supply issue. The ESP8266 chip can easily draw 300 mA peaks when sending data - that’s a lot for your Arduino to supply, and may simply be too much even, causing the voltage on the ESP side to drop too much and the transmit to fail. Try connecting to a separate power supply.

Also there’s the issue of voltages; your ESP module runs at 3.3V, not 5V. Now that’s easy to handle using a level shifter and a regulator, it doesn’t make it any more power efficient.