使用 GPRS 使用 arduino GSM 模块将数据发送到网络 [英] Sending data to web using arduino GSM module using GPRS

查看:40
本文介绍了使用 GPRS 使用 arduino GSM 模块将数据发送到网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
 }
}

我什至不明白代码但我明白了当我尝试将数据发布到网络时它不起作用我不知道它有什么问题我正在获取网络内容,但我无法将数据发布到网络我尝试了 get 和 post 方法都没有给我带来好运

i even don't understand the code but i get the idea and when i am trying to post the data to web its not working i don't know what's wrong with it i am getting web content back but i can't post data to the web i tried both get and post method non of them brought luck to me

推荐答案

您在代码中提出的 http 请求有一些错误.您不能像以前那样从 arduino 请求.

The http request you made in your code has some mistakes.You can't request from arduino as you did.

当使用 000webhost 作为数据库时,最好的方法是使用 PHP.您可以通过选择文件管理器来上传 PHP 文件 然后选择立即上传文件".里面有一个文件夹名为public_html",是上传PHP代码的地方.

When using 000webhost for database the best way is to make use of PHP.You can upload PHP files by selecting File Manager and then select "Upload files now" .In that there is a folder named "public_html" ,it's where the PHP codes are to be uploaded.

现在这就是你想要做的.你想创建一个PHP代码,当请求这个php代码的URL时,它会执行对数据库的插入操作.然后你可以从arduino请求这个链接来执行插入.

Now here is what you want to do.You want to create a PHP code which would perform the insertion operation to the database when the URL of this php code is requested.Then you can request this link from arduino for performing insertion.

PHP 代码是(请在提供的代码位置提供您的用户名、密码、数据库名、表名、列名,然后按照我上面所说的方式上传):

The PHP code is ( Please provide your username,password,dbname,table name,column name in the provided locations of the code and upload it as i said above) :

<?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 草图中使用它.

Obtain the URL of the uploaded PHP code (right click uploaded file and click view , then copy URL in the browser). You can use it in your Arduino sketch.

您的 arduino 代码应该如下所示(此草图中的 URL 不完整,将其替换为我上面所说的 [http://afridiofkh.000webhostapp.com/phpfilename.php 这是一个被替换.从 http 开始直到 ? 标记被替换] ) :

Your arduino code should look like ( URL in this sketch is incomplete replace it with one obtained as i said above [ http://afridiofkh.000webhostapp.com/phpfilename.php this is the one to be replaced.starting from http till ? mark is to be replaced] ) :

#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;
 }
}

这将根据您的需要执行插入操作.PHP 很容易,因为它可以用于扩展操作.

This would perform the insertion operation as you need to.PHP is easy as it can be used to extend the operations.

这篇关于使用 GPRS 使用 arduino GSM 模块将数据发送到网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆