I'm trying to send a GET request to an nodejs server (using express) from arduino mega using the wifi module esp8266 with the AT-Commands.
It seems the server doesn't receive nothing even if in the arduino serial monitor I can read "SEND OK" when the server is running.
The same command sent from arduino to an flask server works. With the Chrome browser the I have no problems, both nodejs server and flask receive the request.
I have spent a lot of days finding a solution for this problem, I don't understand where I am wrong.
Arduino code for sending request:
void sendGetRequest(String ip, int port, String buttonId) {
String command = "AT+CIPSTART=0,\"TCP\",";
command += "\"";
command += ip;
command += "\",";
command += port;
command += "\r\n";
sendData(command, 1000, DEBUG);
sendData("AT+CIPSEND=0,15\r\n", 1000, DEBUG);
String request = "GET /b1";
sendData(request, 1000, DEBUG);
sendData(" \r\n", 1000, DEBUG);
sendData(" \r\n", 1000, DEBUG);
sendData(" \r\n", 1000, DEBUG);
//sendData("AT+CIPCLOSE=4\r\n", 1000, DEBUG);
}
String sendData(String command, const int timeout, boolean debug) {
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while ((time+timeout) > millis()) {
while (esp8266.available()) {
// the esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if (debug) {
Serial.print(response);
}
return response;
}
NodeJs express endpoint:
var http = require('http');
//Server port
var port = 6882;
//Web server
var app = express();
var server = http.createServer(app);
server.listen(port, "0.0.0.0");
app.get('/b1', function (req, res) {
console.log("Button pressed!" );
res.send("Hello");
});
Flask simple server that works:
from flask import Flask
app = Flask(__name__)
@app.route("/b1", methods=["GET"])
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Thanks for your help, and sorry for my bad english.
via Cosimo Lovascio
No comments:
Post a Comment