inconshreveable / ngrok

Unified ingress for developers
https://ngrok.com
Other
24.12k stars 4.26k forks source link

Issue in sending sensor data from ESP8266 to Ngrok tunnel #945

Closed ha-rish closed 4 months ago

ha-rish commented 5 months ago

With a free ngrok account, I was able to generate a static HTTP domain. In my localhost a code is running to receive and decode the data from port 80. The localhost is mapped to the ngrok tunnel. Now, I have a few ESP8266 nodes which send data to this HTTP domain frequently. The data is sent to the domain with a POST REQUEST method. The response code arrived is 307 instead of 200. Can you please guide me where the error might be?

The program that runs on ESP8266 is given below:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char* ssid = "";
const char* password = "";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    WiFiClient client;

    http.begin(client, "https://domain.ngrok-free.app");
    http.addHeader("Content-Type", "text/plain");

    String postMessage = "$A123";

    int httpResponseCode = http.POST(postMessage);

    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println(httpResponseCode);
      Serial.println(response);
    } else {
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }

    http.end();
  } else {
    Serial.println("Error in WiFi connection");
  }

  delay(10000);  // Send a request every 10 seconds
}

In the localhost:

from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def home():
    if request.method == 'POST':
        data = request.data  # Get the data from the POST request
        print("Received data: ", data)
        return 'OK', 200  # Send a response back to the ESP8266
    else:
        return 'Hello, World!', 200  # Send a response for GET requests

if __name__ == '__main__':
    app.run(port=443) 
russorat commented 4 months ago

Thank you for opening this issue! As of April 2016, the ngrok service has permanently moved to https://ngrok.com/. To join the community and file bug reports or feature enhancements for the ngrok agent, please go to https://github.com/ngrok/ngrok.

This repository is no longer actively maintained and will be archived soon to reduce confusion with the latest ngrok versions. We thank you for the continued support of ngrok and look forward to seeing you over at ngrok.com and the ngrok Community Repo.