求助:基于SIM800L与Arduino实现指定HTTP POST请求
问题描述
我已通过Python成功实现向指定URL发送HTTP POST请求,代码如下:
requests.post(url="https://9336-154-239-164-63.ngrok-free.app/id=tiva1&lat=30.282092&lon=31.748090×tamp=2&hdop=3&altitude=44&speed=5");
现希望使用Arduino搭配SIM800L模块实现相同功能,但自行编写的代码无法正常运行,代码如下:
#include <SoftwareSerial.h> // SoftwareSerial object to communicate with SIM800L SoftwareSerial sim800l(10, 11); // RX, TX void setup() { Serial.begin(9600); sim800l.begin(9600); delay(2000); sendATCommand("AT"); // Check if the module is responding sendATCommand("AT+CPIN?"); // Check SIM card status sendATCommand("AT+CGATT?"); // Check if attached to GPRS sendATCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); // Set the connection type sendATCommand("AT+SAPBR=1,1"); // Enable GPRS sendATCommand("AT+HTTPINIT"); // Initialize HTTP service } void loop() { // Make the HTTP POST request String url = "https://9336-154-239-164-63.ngrok-free.app/id=tiva1&lat=30.282092&lon=31.748090×tamp=2&hdop=3&altitude=44&speed=5"; sendATCommand("AT+HTTPPARA=\"URL\",\"" + url + "\""); sendATCommand("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\""); sendATCommand("AT+HTTPDATA=0,10000"); // Set the data length sendATCommand("AT+HTTPACTION=1"); // Start the POST request delay(5000); // Wait for the response // Read the response sendATCommand("AT+HTTPREAD=0,1000"); // Close the HTTP service sendATCommand("AT+HTTPTERM"); delay(60000); // Wait for 1 minute before making the next request } void sendATCommand(String command) { sim800l.println(command); delay(500); while (sim800l.available()) { char c = sim800l.read(); Serial.write(c); delay(30); // Delay between characters to allow time for printing } }
恳请协助排查代码问题,实现与Python版本相同的HTTP POST请求功能。
内容的提问来源于stack exchange,提问作者khaled mohamed
相关产品推荐
相关产品推荐

