The use of the ESP8266 in the world of IoT

User avatar
By MikeDB
#85847
kenn wrote:btw the most efficient way to operate is to have the ESP simply output data as a json, and web page (or an application) is on the tablet.


Could you explain that in more detail as it may be suitable. How does the ESP send data as a JSON to the webpage on the tablet ?
User avatar
By quackmore
#85854
Code: Select allSo in your ESPthermostat, am I correct in thinking that the ESP8266 code doesn't actually know it's using CORS ?
Or is there something inside your ESPBOT code as well which I can't locate ?


not completely,

summarizing what's happening in your setup:

the browser is using the tablet/PC file system as the origin http server but it also need to query the external esp device
without CORS enabled any modern browser will block the query to the esp for security reasons (cause it's directed to something different from origin)

with CORS enabled, the browser will inform the esp that the query is coming from origin using "Origin: ...." into the http header
the esp will have to verify if origin is allowed and provide information into the http headers "Access-Control-Allow-Origin: ...."
I kept it simple here and I just allow all origins using "Access-Control-Allow-Origin: *" into the http headers

these are the http messages exchanged in different scenarios for a single query

all the HTML and JS files are on the ESP
query (no reference to Origin)
Code: Select allGET /api/info HTTP/1.1
Host: 192.168.1.229
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36
Referer: http://192.168.1.229/
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6


answer (I don't care if "Origin" is missing, and add "Access-Control-Allow-Origin: *" anyway)
Code: Select allHTTP/1.1 200 OK
Server: espbot/2.0
Content-Type: application/json
Content-Length: 232
Access-Control-Allow-Origin: *

{"app_name":"App Example","app_version":"v1.0-96-ga6aa2de","espbot_name":"bad_flash","espbot_version":"v1.0-96-ga6aa2de","library_version":"v1.0-18-g50821c2","chip_id":"5169151","sdk_version":"3.1.0-dev(b1f42da)","boot_version":"7"}


all the HTML and JS files are on a tablet/PC
query ("Origin: null" meaning the browser is using the filesystem as a http server)
Code: Select allGET /api/info HTTP/1.1
Host: 192.168.1.229
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36
Origin: null
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6


answer (always the same)
Code: Select allHTTP/1.1 200 OK
Server: espbot/2.0
Content-Type: application/json
Content-Length: 232
Access-Control-Allow-Origin: *

{"app_name":"App Example","app_version":"v1.0-96-ga6aa2de","espbot_name":"bad_flash","espbot_version":"v1.0-96-ga6aa2de","library_version":"v1.0-18-g50821c2","chip_id":"5169151","sdk_version":"3.1.0-dev(b1f42da)","boot_version":"7"}