-2

I am trying to send a name variable from my ESP8266 to index.php.

This is my arduino code:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <HTTPClient.h>

const char* ssid = "NikitaiPhone"; //replace with your own wifi ssid
const char* password = "Maxius1"; //replace with your own wifi ssid password
const char* host = "104.220.131.254";

WiFiClient client;

void setup() {
    
    Serial.begin(115200);
    delay(10); // We start by connecting to a WiFi network Serial.println();
    Serial.println();
    WiFi.mode(WIFI_OFF);
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void loop(){
    HTTPClient http;

    http.begin(client, "http://104.220.131.254/index.php?name=Joe");

    int httpCode = http.GET();
    String payload = http.getString();

    Serial.println(httpCode);
    Serial.println(payload);

    http.end();

    delay(5000);
}

This is my php code:

<?php
$name = $_GET['name'];
if($name == "Joe"){
    echo "name " . $name;
} else {
    echo "name none";
}
?>

This is the response from index.php?name=Joe

index.php?name=Joe

This is the response from index.php

index.php

I am trying to have the name displayed on index.php. How do I do that? I would like to send variables from one ESP8266 to the index.php page and have the other ESP8266 read them. However, if I can only send it to index.php?name=Joe, there is no way for the other ESP8266 to read that page.

Thanks

Andrew
  • 1,544
  • 1
  • 18
  • 36
  • 4
    Please [edit] your post and add your code here as text. Images are extremely hard to read and troubleshoot. Especially yellow text on white background. How can you read that?! – aynber Nov 16 '21 at 15:35
  • If I can read it right (ugh, yellow on white, hurts), then you're **only** checking for name equals Joe on your GET, otherwise outputting `none`. So you need to change the logic to what you want – aynber Nov 16 '21 at 15:56
  • @aynber Nikita has a fundamental misunderstanding of how the GET super global variable works - it sounds like they think it can be accessed from other sessions without actually storing data. – Andrew Nov 16 '21 at 16:00
  • Sorry, can't see images from behind a firewall – DDS Nov 16 '21 at 16:26

1 Answers1

0

There is nothing going wrong here. $_GET is populated with the query string, key/value pairs passed as text after the ? in a get request.

This means that ?name=Joe is telling the PHP code to create a key in the get global variable called "name" and store the value "Joe" there.

When you don't have the "?" or anything after it, there are no key/value pairs to put into $_GET and this variable is unique for each request to the server - you cannot access the values passed in a different GET request's query string without actually storing them somewhere first.

Andrew
  • 1,544
  • 1
  • 18
  • 36
  • I would like one of my ESP8266 read a sensor value, send it to my server, and then have another ESP8266 read/take that value. So I should send a GET request to the server with the value, then store it somehow in the server, and only then the other ESP8266 would be able to read it? Just making sure I got that right, im very new to servers. – Nikita Varfolomeev Nov 20 '21 at 20:18
  • @NikitaVarfolomeev `$_GET` is not stored anywhere persistent, it's in the server's RAM. Storing like this is what databases are for, you could also potentially write it to a file and read back from that file if you control the server environment enough, but DBs are preferable for server side storage. You should also be using POST when you want to store something and GET when you want to read something. – Andrew Nov 22 '21 at 14:27