Forums

esp32 sending/writing data

Hello. I have a piece of hardware called an esp32 chip. It is taking sensor data and I am able to send that data to a local host site. I would like to send the data to my paid python anywhere account.

Below is hopefully enough information to show how I successfully send data to my local host site.

ESP32 Code is in C++, but has the info needed for a successful POST to my local site

const uint16_t port =   5000;           
const char * host =    "10.0.0.222";

after sensor data is received (client is a c++ class)

  if (!client.connect(host, port)) {           
      Serial.println("Connection failed.");           
      Serial.println("Waiting 5 seconds before retrying...");           
      delay(5000);           
      return;}   
  String PostData="my_data_string";           
  client.println("POST /echo HTTP/1.1");           
  client.println("Host:  ESP32");           
  client.println("User-Agent: Arduino/1.0");           
  client.println("Connection: close");           
  client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");           
  client.print("Content-Length: ");           
  client.println(PostData.length());           
  client.println();           
  client.println(PostData);             
  client.stop();

python code

@app.route('/echo', methods=['GET', 'POST'])
def echo():
    if request.method == 'POST':
        dataIncoming = json.loads(request.values['esp32'])

I'm hoping it would be as easy as replacing some of the info, like port number etc... , in the C++ code. Thanks!

Maybe it's as easy as changing the host variable in your C code. It's probably set to "localhost" or "127.0.0.1" and can be changed to send data to your pythonanywhere web app

I did change the host in my C code to SAtelliteVIsualization.pythonanywhere.com and a few variations with no luck. I also tried to change the port number as well.

Actually, I think I got it. Followed the bottom example here for ESP32 POST and it seems to be working. Thanks! https://randomnerdtutorials.com/esp32-http-get-post-arduino/#http-post

Great, thanks for sharing.