Go Down

Topic: How to send data from website to Arduino ESP32 (Read 159 times) previous topic - next topic

MusicianTeen

Is it possible to send JSON payload to ESP32 from website ?

Thee_Captain

It is possible to make an HTTP request from the ESP32 and read the response from your HTTP website on your Arduino ESP32.

This is a basic HTTP request to get you started. https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino

The Arduino JSON library works well to parse your JSON. https://arduinojson.org/
Throw a little karma my way. What goes around comes around.

MusicianTeen

@Thee_Captain, thank you. Information you have shared is to make request to external website, in this case to http://example.com/index.html.

I am looking for sending payload from external application (http://example.com/index.html) to Ardunio.

arduino_new

@Thee_Captain, thank you. Information you have shared is to make request to external website, in this case to http://example.com/index.html.

I am looking for sending payload from external application (http://example.com/index.html) to Ardunio.
How do you plan to receive the payload from your arduino?

Thee_Captain

@Thee_Captain, thank you. Information you have shared is to make request to external website, in this case to http://example.com/index.html.

I am looking for sending payload from external application (http://example.com/index.html) to Ardunio.
String payload = http.getString();

This contains the HTTP resppnse from your website. Your endpoint should contain your JSON string.

It is not really advised to initiate"sending" things from "websites." HTTP doesn't really work that way. There are things like push notifications that are initiated by the server, but these are not native HTTP and usually requires a service from iOS or Android. Here is W3C's push API: https://www.w3.org/TR/push-api/

I think using push notifications is not the solution to most problems. Most problems require a client to request the resource.

Maybe if you explain your project, it would become more clear what solution you need.
Throw a little karma my way. What goes around comes around.

MusicianTeen

thank you @Thee_Captain

I am beginner with Arduino. I am working with ESP32 board and have connected that to my local wifi and have local ip address. How do I access this ESP32 over internet - externally from servers not connected to local wifi ?   

lesept

You have to set a port forwarding from you modem to your ESP32 IP
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

pourduino

You have to set a port forwarding from you modem to your ESP32 IP
And you must have a public IP address(you shouldn't be behind a NAT) and this IP should be static. You can ask these from your ISP.

If you can't do the above it's really much easier to just periodically poll the server via the ESP and not the other way around.

If server->ESP connection is an absolute necessity you can try maintaining a TCP connection between the two then the server can send data to ESP at any time.
However for this you have to write a program for the server and on both your server and ESP you should have good code that handles disconnecting/reconnecting to make sure connection is never lost. Also you'll probably need a Virtual Private Server for this.

Thee_Captain

thank you @Thee_Captain

I am beginner with Arduino. I am working with ESP32 board and have connected that to my local wifi and have local ip address. How do I access this ESP32 over internet - externally from servers not connected to local wifi ?  
So I wouldn't try to "access the ESP32" from outside the local network. I would access the server on the outside external network from my ESP32 using an HTTP GET request. Then I would read the response from the server that is on Thee Internet.

Local ESP32 sends GET Request to www.example.com/json/id/21.html
www.example.com responds with a JSON text string to your ESP32
Local ESP32 reads, parses and handles the JSON response body

In most cases this client server paradigm will work.

So in the basic example, lines 88-92, String payload will contain the response body from the server anywhere on Thee Internet.

Code: [Select]

// file found at server
if(httpCode == HTTP_CODE_OK) {
 String payload = http.getString();
 USE_SERIAL.println(payload);
}



You need to set the server up at your endpoint, www.example.com/json/id/21.html, to respond with a JSON string. This is as easy as writing a file named 21.html with a JSON string in the file. Then store that file at your_http_root/json/id/ on your HTTP Server on the domain www.example.com.

You can also dynamically create that page using a scripting language like PHP, Ruby, Python ect.
Throw a little karma my way. What goes around comes around.

Deva_Rishi

Check out this for accessing an ESP from anywhere, do be careful ! if you can get in maybe others can too.
To 'Correct' you have to be Correct. (and not be condescending..)

Go Up