使用 arduino GSM 模块使用 GPRS 将数据发送到网络
Sending data to web using arduino GSM module using GPRS
#include <SoftwareSerial.h>
SoftwareSerial GSM(9, 10); // RX, TX
int sensor=5;
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_HTTPACTION_TYPE,
PS_HTTPACTION_RESULT,
PS_HTTPACTION_LENGTH,
PS_HTTPREAD_LENGTH,
PS_HTTPREAD_CONTENT
};
byte parseState = PS_DETECT_MSG_TYPE;
char buffer[80];
byte pos = 0;
int contentLength = 0;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void sendGSM(const char* msg, int waitMs = 500) {
GSM.println(msg);
delay(waitMs);
while(GSM.available()) {
parseATText(GSM.read());
}
}
void setup()
{
GSM.begin(9600);
Serial.begin(9600);
sendGSM("AT+SAPBR=3,1,\"APN\",\"zongwap\"");
sendGSM("AT+SAPBR=1,1",300);
sendGSM("AT+HTTPINIT");
sendGSM("AT+HTTPPARA=\"CID\",1");
sendGSM("AT+HTTPPARA=\"URL\",\"http://afridiofkh.000webhostapp.com?
sensor1="" + sensor + ""\"""");
sendGSM("AT+HTTPACTION=0");
}
void loop()
{
while(GSM.available()) {
parseATText(GSM.read());
}
}
void parseATText(byte b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
/*
// Detailed debugging
Serial.println();
Serial.print("state = ");
Serial.println(state);
Serial.print("b = ");
Serial.println(b);
Serial.print("pos = ");
Serial.println(pos);
Serial.print("buffer = ");
Serial.println(buffer);*/
switch (parseState) {
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
parseState = PS_IGNORING_COMMAND_ECHO;
}
else if ( b == ':' ) {
//Serial.print("Checking message type: ");
//Serial.println(buffer);
if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
Serial.println("Received HTTPACTION");
parseState = PS_HTTPACTION_TYPE;
}
else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
Serial.println("Received HTTPREAD");
parseState = PS_HTTPREAD_LENGTH;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
Serial.print("Ignoring echo: ");
Serial.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPACTION_TYPE:
{
if ( b == ',' ) {
Serial.print("HTTPACTION type is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_RESULT;
resetBuffer();
}
}
break;
case PS_HTTPACTION_RESULT:
{
if ( b == ',' ) {
Serial.print("HTTPACTION result is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_LENGTH;
resetBuffer();
}
}
break;
case PS_HTTPACTION_LENGTH:
{
if ( b == '\n' ) {
Serial.print("HTTPACTION length is ");
Serial.println(buffer);
// now request content
GSM.print("AT+HTTPREAD=0,");
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPREAD_LENGTH:
{
if ( b == '\n' ) {
contentLength = atoi(buffer);
Serial.print("HTTPREAD length is ");
Serial.println(contentLength);
Serial.print("HTTPREAD content: ");
parseState = PS_HTTPREAD_CONTENT;
resetBuffer();
}
}
break;
case PS_HTTPREAD_CONTENT:
{
// for this demo I'm just showing the content bytes in the serial monitor
Serial.write(b);
contentLength--;
if ( contentLength <= 0 ) {
// all content bytes have now been read
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
我什至不理解代码,但我明白了,当我尝试 post 将数据上网时,它不起作用,我不知道它有什么问题
我正在取回网络内容,但无法 post 将数据传输到网络
我尝试了 get 和 post 方法,但没有一个给我带来好运
您在代码中发出的 http 请求有一些 mistakes.You 无法像您那样从 arduino 请求。
当数据库使用 000webhost 时,最好的方法是利用 PHP.You 可以通过 selecting 文件管理器 然后 [=43] 上传 PHP 文件=] "Upload files now" 。其中有一个名为 "public_html" 的文件夹,这是要上传 PHP 代码的地方。
现在这就是你想要do.You创建一个PHP代码,当这个php的URL时,它会执行对数据库的插入操作代码是 requested.Then 你可以从 arduino 请求这个 link 来执行插入。
PHP 代码是(请在代码提供的位置提供您的用户名、密码、数据库名称、table 名称、列名称,然后按我上面所说的上传):
<?php
$servername = "localhost";
$username = "provide your username here";
$password = "password here";
$dbname = "database name here";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO table_name (column_name) VALUES ('".mysqli_real_escape_string($conn,$_GET['sensor1'])."')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
获取上传的PHP代码的URL(右击上传的文件点击查看,然后在浏览器中复制URL)。您可以在您的 Arduino 草图中使用它。
你的 arduino 代码应该看起来像(URL 在这个草图中是不完整的用我上面说的那样替换它 [http://afridiofkh.000webhostapp.com/phpfilename.php
这是来自 http 的 replaced.starting till ? 标记将被替换] ) :
#include <SoftwareSerial.h>
SoftwareSerial GSM(9, 10); // RX, TX
int sensor=5;
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_HTTPACTION_TYPE,
PS_HTTPACTION_RESULT,
PS_HTTPACTION_LENGTH,
PS_HTTPREAD_LENGTH,
PS_HTTPREAD_CONTENT
};
byte parseState = PS_DETECT_MSG_TYPE;
char buffer[130],url[130]; //adjust as your url length
byte pos = 0;
int contentLength = 0;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void sendGSM(const char* msg, int waitMs = 500) {
GSM.println(msg);
delay(waitMs);
while(GSM.available()) {
parseATText(GSM.read());
}
}
void setup()
{
GSM.begin(9600);
Serial.begin(9600);
sendGSM("AT+SAPBR=3,1,\"APN\",\"zongwap\"");
sendGSM("AT+SAPBR=1,1",300);
sendGSM("AT+HTTPINIT");
sendGSM("AT+HTTPPARA=\"CID\",1");
sprintf(url, "AT+HTTPPARA=\"URL\",\"http://afridiofkh.000webhostapp.com/phpfilename.php?sensor1=%d\"",sensor);
sendGSM(url);
sendGSM("AT+HTTPACTION=0");
}
void loop()
{
while(GSM.available()) {
parseATText(GSM.read());
}
}
void parseATText(byte b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
/*
// Detailed debugging
Serial.println();
Serial.print("state = ");
Serial.println(state);
Serial.print("b = ");
Serial.println(b);
Serial.print("pos = ");
Serial.println(pos);
Serial.print("buffer = ");
Serial.println(buffer);*/
switch (parseState) {
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
parseState = PS_IGNORING_COMMAND_ECHO;
}
else if ( b == ':' ) {
//Serial.print("Checking message type: ");
//Serial.println(buffer);
if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
Serial.println("Received HTTPACTION");
parseState = PS_HTTPACTION_TYPE;
}
else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
Serial.println("Received HTTPREAD");
parseState = PS_HTTPREAD_LENGTH;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
Serial.print("Ignoring echo: ");
Serial.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPACTION_TYPE:
{
if ( b == ',' ) {
Serial.print("HTTPACTION type is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_RESULT;
resetBuffer();
}
}
break;
case PS_HTTPACTION_RESULT:
{
if ( b == ',' ) {
Serial.print("HTTPACTION result is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_LENGTH;
resetBuffer();
}
}
break;
case PS_HTTPACTION_LENGTH:
{
if ( b == '\n' ) {
Serial.print("HTTPACTION length is ");
Serial.println(buffer);
// now request content
GSM.print("AT+HTTPREAD=0,");
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPREAD_LENGTH:
{
if ( b == '\n' ) {
contentLength = atoi(buffer);
Serial.print("HTTPREAD length is ");
Serial.println(contentLength);
Serial.print("HTTPREAD content: ");
parseState = PS_HTTPREAD_CONTENT;
resetBuffer();
}
}
break;
case PS_HTTPREAD_CONTENT:
{
// for this demo I'm just showing the content bytes in the serial monitor
Serial.write(b);
contentLength--;
if ( contentLength <= 0 ) {
// all content bytes have now been read
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
这将根据需要执行插入操作 to.PHP 很容易,因为它可用于扩展操作。
#include <SoftwareSerial.h>
SoftwareSerial GSM(9, 10); // RX, TX
int sensor=5;
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_HTTPACTION_TYPE,
PS_HTTPACTION_RESULT,
PS_HTTPACTION_LENGTH,
PS_HTTPREAD_LENGTH,
PS_HTTPREAD_CONTENT
};
byte parseState = PS_DETECT_MSG_TYPE;
char buffer[80];
byte pos = 0;
int contentLength = 0;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void sendGSM(const char* msg, int waitMs = 500) {
GSM.println(msg);
delay(waitMs);
while(GSM.available()) {
parseATText(GSM.read());
}
}
void setup()
{
GSM.begin(9600);
Serial.begin(9600);
sendGSM("AT+SAPBR=3,1,\"APN\",\"zongwap\"");
sendGSM("AT+SAPBR=1,1",300);
sendGSM("AT+HTTPINIT");
sendGSM("AT+HTTPPARA=\"CID\",1");
sendGSM("AT+HTTPPARA=\"URL\",\"http://afridiofkh.000webhostapp.com?
sensor1="" + sensor + ""\"""");
sendGSM("AT+HTTPACTION=0");
}
void loop()
{
while(GSM.available()) {
parseATText(GSM.read());
}
}
void parseATText(byte b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
/*
// Detailed debugging
Serial.println();
Serial.print("state = ");
Serial.println(state);
Serial.print("b = ");
Serial.println(b);
Serial.print("pos = ");
Serial.println(pos);
Serial.print("buffer = ");
Serial.println(buffer);*/
switch (parseState) {
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
parseState = PS_IGNORING_COMMAND_ECHO;
}
else if ( b == ':' ) {
//Serial.print("Checking message type: ");
//Serial.println(buffer);
if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
Serial.println("Received HTTPACTION");
parseState = PS_HTTPACTION_TYPE;
}
else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
Serial.println("Received HTTPREAD");
parseState = PS_HTTPREAD_LENGTH;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
Serial.print("Ignoring echo: ");
Serial.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPACTION_TYPE:
{
if ( b == ',' ) {
Serial.print("HTTPACTION type is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_RESULT;
resetBuffer();
}
}
break;
case PS_HTTPACTION_RESULT:
{
if ( b == ',' ) {
Serial.print("HTTPACTION result is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_LENGTH;
resetBuffer();
}
}
break;
case PS_HTTPACTION_LENGTH:
{
if ( b == '\n' ) {
Serial.print("HTTPACTION length is ");
Serial.println(buffer);
// now request content
GSM.print("AT+HTTPREAD=0,");
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPREAD_LENGTH:
{
if ( b == '\n' ) {
contentLength = atoi(buffer);
Serial.print("HTTPREAD length is ");
Serial.println(contentLength);
Serial.print("HTTPREAD content: ");
parseState = PS_HTTPREAD_CONTENT;
resetBuffer();
}
}
break;
case PS_HTTPREAD_CONTENT:
{
// for this demo I'm just showing the content bytes in the serial monitor
Serial.write(b);
contentLength--;
if ( contentLength <= 0 ) {
// all content bytes have now been read
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
我什至不理解代码,但我明白了,当我尝试 post 将数据上网时,它不起作用,我不知道它有什么问题 我正在取回网络内容,但无法 post 将数据传输到网络 我尝试了 get 和 post 方法,但没有一个给我带来好运
您在代码中发出的 http 请求有一些 mistakes.You 无法像您那样从 arduino 请求。
当数据库使用 000webhost 时,最好的方法是利用 PHP.You 可以通过 selecting 文件管理器
现在这就是你想要do.You创建一个PHP代码,当这个php的URL时,它会执行对数据库的插入操作代码是 requested.Then 你可以从 arduino 请求这个 link 来执行插入。
PHP 代码是(请在代码提供的位置提供您的用户名、密码、数据库名称、table 名称、列名称,然后按我上面所说的上传):
<?php
$servername = "localhost";
$username = "provide your username here";
$password = "password here";
$dbname = "database name here";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO table_name (column_name) VALUES ('".mysqli_real_escape_string($conn,$_GET['sensor1'])."')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
获取上传的PHP代码的URL(右击上传的文件点击查看,然后在浏览器中复制URL)。您可以在您的 Arduino 草图中使用它。
你的 arduino 代码应该看起来像(URL 在这个草图中是不完整的用我上面说的那样替换它 [http://afridiofkh.000webhostapp.com/phpfilename.php
这是来自 http 的 replaced.starting till ? 标记将被替换] ) :
#include <SoftwareSerial.h>
SoftwareSerial GSM(9, 10); // RX, TX
int sensor=5;
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_HTTPACTION_TYPE,
PS_HTTPACTION_RESULT,
PS_HTTPACTION_LENGTH,
PS_HTTPREAD_LENGTH,
PS_HTTPREAD_CONTENT
};
byte parseState = PS_DETECT_MSG_TYPE;
char buffer[130],url[130]; //adjust as your url length
byte pos = 0;
int contentLength = 0;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void sendGSM(const char* msg, int waitMs = 500) {
GSM.println(msg);
delay(waitMs);
while(GSM.available()) {
parseATText(GSM.read());
}
}
void setup()
{
GSM.begin(9600);
Serial.begin(9600);
sendGSM("AT+SAPBR=3,1,\"APN\",\"zongwap\"");
sendGSM("AT+SAPBR=1,1",300);
sendGSM("AT+HTTPINIT");
sendGSM("AT+HTTPPARA=\"CID\",1");
sprintf(url, "AT+HTTPPARA=\"URL\",\"http://afridiofkh.000webhostapp.com/phpfilename.php?sensor1=%d\"",sensor);
sendGSM(url);
sendGSM("AT+HTTPACTION=0");
}
void loop()
{
while(GSM.available()) {
parseATText(GSM.read());
}
}
void parseATText(byte b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer(); // just to be safe
/*
// Detailed debugging
Serial.println();
Serial.print("state = ");
Serial.println(state);
Serial.print("b = ");
Serial.println(b);
Serial.print("pos = ");
Serial.println(pos);
Serial.print("buffer = ");
Serial.println(buffer);*/
switch (parseState) {
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
parseState = PS_IGNORING_COMMAND_ECHO;
}
else if ( b == ':' ) {
//Serial.print("Checking message type: ");
//Serial.println(buffer);
if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
Serial.println("Received HTTPACTION");
parseState = PS_HTTPACTION_TYPE;
}
else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
Serial.println("Received HTTPREAD");
parseState = PS_HTTPREAD_LENGTH;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
Serial.print("Ignoring echo: ");
Serial.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPACTION_TYPE:
{
if ( b == ',' ) {
Serial.print("HTTPACTION type is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_RESULT;
resetBuffer();
}
}
break;
case PS_HTTPACTION_RESULT:
{
if ( b == ',' ) {
Serial.print("HTTPACTION result is ");
Serial.println(buffer);
parseState = PS_HTTPACTION_LENGTH;
resetBuffer();
}
}
break;
case PS_HTTPACTION_LENGTH:
{
if ( b == '\n' ) {
Serial.print("HTTPACTION length is ");
Serial.println(buffer);
// now request content
GSM.print("AT+HTTPREAD=0,");
GSM.println(buffer);
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_HTTPREAD_LENGTH:
{
if ( b == '\n' ) {
contentLength = atoi(buffer);
Serial.print("HTTPREAD length is ");
Serial.println(contentLength);
Serial.print("HTTPREAD content: ");
parseState = PS_HTTPREAD_CONTENT;
resetBuffer();
}
}
break;
case PS_HTTPREAD_CONTENT:
{
// for this demo I'm just showing the content bytes in the serial monitor
Serial.write(b);
contentLength--;
if ( contentLength <= 0 ) {
// all content bytes have now been read
parseState = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
这将根据需要执行插入操作 to.PHP 很容易,因为它可用于扩展操作。