使用 gsim 800l 和 arduino 发送数据

问题描述 投票:0回答:1

我只是尝试使用 gsim 和 aurdino 向网络服务器发出 http 请求,我已经使用 python 完成了,并且工作正常

requests.post(url="https://9336-154-239-164-63.ngrok-free.app/id=tiva1&lat=30.282092&lon=31.748090&timestamp=2&hdop=3&altitude=44&speed=5"); to an Arduino code using GSIM 800l with same functionality

现在我想用 GSIM 执行相同的 http 请求,我尝试了这段代码,但它根本不起作用。所以希望有人能帮助我

`#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&timestamp=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 arduino-uno
1个回答
0
投票

当然!需要进行一些修改和添加,以确保 SIM800L 可以发出正确的 HTTP POST 请求。

URL 格式:您已将服务器 URL 和参数组合成一个字符串。建议将服务器 URL 从参数中分离出来,以确保正确构造 HTTP POST 请求。

HTTP 初始化:初始化 HTTP 和 GPRS 连接的顺序至关重要。我重新排列了一些命令以获得更好的顺序。

命令响应:我添加了一种在发送 AT 命令后从 SIM800L 模块读取响应的方法。这有助于调试和理解命令是否被接受。

这是修改后的Arduino代码:

#include <SoftwareSerial.h>

SoftwareSerial sim800l(10, 11); // RX, TX

void setup() {
  Serial.begin(9600);
  sim800l.begin(9600);
  
  delay(3000);  // Wait for the module to power on and find the network
  
  sendATCommand("AT");
  delay(1000);

  sendATCommand("AT+CPIN?"); // Check SIM card status
  delay(1000);
  
  sendATCommand("AT+CGATT=1"); // Attach to GPRS
  delay(1000);

  sendATCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); // Set the connection type to GPRS
  delay(1000);
  
  sendATCommand("AT+SAPBR=1,1"); // Open a GPRS context
  delay(1000);
  
  sendATCommand("AT+HTTPINIT"); // Initialize HTTP service
  delay(1000);
}

void loop() {
  String server = "https://9336-154-239-164-63.ngrok-free.app";
  String params = "id=tiva1&lat=30.282092&lon=31.748090&timestamp=2&hdop=3&altitude=44&speed=5";
  
  sendATCommand("AT+HTTPPARA=\"CID\",1");
  delay(1000);
  
  sendATCommand("AT+HTTPPARA=\"URL\",\"" + server + "\"");
  delay(1000);
  
  sendATCommand("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\"");
  delay(1000);
  
  sendATCommand("AT+HTTPDATA=" + String(params.length()) + ",10000");
  delay(2000); // Wait for > prompt
  
  sim800l.println(params);
  delay(2000);  // Allow data to be processed

  sendATCommand("AT+HTTPACTION=1"); // Start the POST request
  delay(5000); // Wait for the response
  
  sendATCommand("AT+HTTPREAD"); // Read the HTTP response
  delay(1000);
  
  delay(60000); // 1-minute delay before the next loop
}

void sendATCommand(String command) {
  sim800l.println(command);
  delay(500);
  while (sim800l.available()) {
    String response = sim800l.readString();
    Serial.print(response);  // Print the module's response to the Serial monitor
  }
}

上面的代码初始化了GSM模块,建立了GPRS连接,然后循环发送HTTP POST请求。确保您的 SIM 卡上有良好的 GSM 信号和足够的余额或适当的数据计划以建立 GPRS 连接。

监视 Arduino 串行监视器以了解模块的响应以调试您可能遇到的任何问题也很有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.