Last active
March 6, 2024 10:03
-
-
Save pharzan/5bb643b383bfea1049eefe44116dcbb1 to your computer and use it in GitHub Desktop.
esp8266 post json data using usocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#listens to the above run to | |
#FLASK_APP=server.py FLASK_DEBUG=1 python3.5 -m flask run -h 192.168.1.124 -p 8999: | |
#run this on the local server to listen to socket communication | |
from flask import Flask, render_template, jsonify | |
from flask import request as query | |
app = Flask(__name__) | |
@app.route('/', methods=['POST']) | |
def update_entry(): | |
request_json = query.get_json() | |
print(request_json) | |
return 'got it!' | |
if __name__ == '__main__': | |
# socketio.run(app | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#run this on the esp8266 to send json | |
import usocket | |
# Create a TCP socket that can communicate over the internet. | |
socketObject = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM) | |
# Create a "request" string, which is how we "ask" the web server for data. | |
data='{"name":"pharzan"}' | |
request = "POST / HTTP/1.1\r\nHost: www.pharzan.com\r\nContent-Length:"+str(len(data))+" \r\nContent-Type: application/json\r\n\r\n"+data+"\r\n\r\n" | |
print(request) | |
# Create a variable named "address" that stores the address and port | |
# of the web server we wish to communicate with. | |
address = ("192.168.1.124", 8999) | |
# Connect the socket object to the web server specified in "address". | |
socketObject.connect(address) | |
# Send the "GET" request to the MicroPython web server. | |
# A "GET" request asks the server for the web page data. | |
bytessent = socketObject.send(request) | |
socketObject.close() | |
print("Socket closed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment