感谢您花时间阅读我的文章。我试着只涵盖我认为对这个问题很重要的内容,但我不是一个专业的程序员,所以请耐心等待。
我有一个带有Arduino Mega的SIM900模块,正在尝试使用http-get函数将数据发送到mySQL数据库。我已经成功地使用SIM900在我的.ftp服务器中创建了文本文件,所以我知道它在某种程度上是有效的,只是不是http函数。我有一个.php脚本,当我通过浏览器使用它时,它运行良好(例如,访问"mysite.epizy.com/senddata.php?sensor69=69"将在浏览器中返回:
阵列([sensor69]=>69[__test]=<dfc33491ff31abef9348da81e436528[_ga]=&>;GA1.2.428043981.1572933465(
当我尝试连接SIM900时,我在串行上得到以下信息:
setting connection type
AT+SAPBR=3,1,"CONTYPE","GPRS"
OK
AT+SAPBR=3,1,"APN","WWW.vodafone.net.nz"
OK
AT+SAPBR=1,1
ERROR
AT+SAPBR=2,1
+SAPBR: 1,1,"121.90.164.49"
OK
AT+HTTPINIT
OK
AT+HTTPPARA="CID",1
OK
AT+HTTPPARA="URL","mysite.epizy.com/send_data.php?sensor69=69"
OK
AT+HTTPACTION=0
OK
⸮⸮
+HTTPACTION:0,200,859
⸮
AT+HTTPREAD
+HTTPREAD:859
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&ar
AT+HTTPTERM
OK
正如我们在HTTPREAD函数中看到的,它返回859个字符,但这似乎被截断了,并且在我的数据库或日志文件中没有注册(在.php中创建/更新(这是我的.php脚本";senddata.php";
<?php
print_r($_REQUEST);
//log php activation
date_default_timezone_set("Pacific/Auckland");
$my_file = 'log.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
$new_data = "n"."Module=".$_GET['moduleID'].", ". date("d:m:Y")." ". date("h/i/sa");
fwrite($handle, $new_data);
fclose($handle);
// Prepare variables for database connection
$dbusername = "epiz_69696969"; // enter database username, I used "arduino" in step 2.2
$dbpassword = "69696969696969"; // enter database password, I used "arduinotest" in step 2.2
$server = "sql169.epizy.com"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
$moduleID=$_GET['moduleID'];
// Connect to your database
$dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
$dbselect = mysql_select_db("epiz_24743320_testData",$dbconnect);
// Prepare the SQL statement
$sql = "INSERT INTO simData2 (moduleID) VALUES ('$moduleID')";
// Execute SQL statement
mysql_query($sql);
mysql_close();
?>
以下是我的http连接arduino代码部分:
void httpConnect() {
myGsm.println("AT+SAPBR=3,1,"CONTYPE","GPRS""); // Set type of internet connection to GPRS context
delay(1000);
Serial.println(" setting connection type");
printSerialData();
myGsm.println("AT+SAPBR=3,1,"APN","WWW.vodafone.net.nz""); // Set access point name
delay(1000);
printSerialData();
myGsm.println("AT+SAPBR=1,1"); // Enable GPRS connection
delay(1000);
printSerialData();
myGsm.println("AT+SAPBR=2,1"); // To check connection status
delay(5000);
printSerialData();
myGsm.println("AT+HTTPINIT"); // Initialize HTTP
delay(500);
printSerialData();
myGsm.println("AT+HTTPPARA="CID",1"); // End the PARA ???
delay(500);
printSerialData();
myGsm.print("AT+HTTPPARA="URL","mysite.epizy.com/send_data.php?sen69=69""); // Send PARA command, set url to send data
delay(2000);
printSerialData();
delay(2000);
printSerialData();
myGsm.println(""); // close url
Serial.println("");
delay(500);
printSerialData();
delay(500);
myGsm.println("AT+HTTPACTION=0"); //HTTP method 0-get 1-post 2-head
delay(1000);
printSerialData();
delay(1000);
printSerialData();
delay(2000);
printSerialData();
delay(2000);
printSerialData();
delay(2000);
printSerialData();
myGsm.println("AT+HTTPREAD");
delay(2000);
printSerialData();
delay(2000);
printSerialData();
delay(2000);
printSerialData();
myGsm.println("AT+HTTPTERM");
delay(100);
printSerialData();
}
如果有人能帮助解释为什么SIM900模块根本无法激活我的.php,我们将不胜感激。
干杯
我已经解决了这个问题。事实证明,我使用的服务器(epizy.com(infinity-free((需要SIM900所没有的javascript功能(至少在我目前的设置中(。我用heroku创建了一个新的服务器,使用了几乎相同的.php文件(数据库是postgreSQL,而不是mySQL,所以做了一些小调整(,现在可以根据需要成功地将数据发送到数据库中。我仍然没有解决截断串行响应的问题,但这对于我所追求的功能来说是不必要的。