使用 sim900 arduino 对 firebase 函数的 POST 请求



我试图使用连接到arduino uno的sim900发送POST请求。 我将其发送到Firebase函数,但我在函数中没有得到它。 我很乐意为我的问题找到解决方案, 或者我可以使用 SIM900 或任何其他 Celular 网络解决方案将传感器中的数据存储到 Firebase 的替代方法(。

Arduino 代码:

#include<SoftwareSerial.h>
SoftwareSerial client(7,8);

String reading="{  "testID" : 1, }";
void setup()
{
Serial.begin(9600);
client.begin(9600);
delay(500);
if(client.available())
{
Serial.println("Connected");
}
else
{
Serial.println("NotConnected");
}
//initSIM();
connectGPRS();
connectHTTP();
}
void loop()
{
}
void connectGPRS()
{ 
client.println("AT+SAPBR=3,1,"Contype","GPRS"");
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=3,1,"APN","www"");//APN
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=1,1");
delay(1000);
ShowSerialData();
client.println("AT+SAPBR=2,1");
delay(1000);
ShowSerialData();
}
void connectHTTP()
{
client.println("AT+HTTPINIT");
delay(1000);
ShowSerialData();
client.println("AT+HTTPPARA="CID",1");
delay(1000);
ShowSerialData();
client.println("AT+HTTPPARA="URL","www.us-central1-**************.cloudfunctions.net/helloWorld"");//Public server IP address
delay(1000);
ShowSerialData();
client.println("AT+HTTPPARA="CONTENT","application/json"");
delay(1000);
ShowSerialData();
client.println("AT+HTTPDATA=" + String(reading.length()) + ",100000");
delay(1000);
ShowSerialData();
client.println(reading);
delay(1000);
ShowSerialData;
client.println("AT+HTTPACTION=1");
delay(1000);
ShowSerialData();
client.println("AT+HTTPREAD");
delay(1000);
ShowSerialData();
client.println("AT+HTTPTERM");
delay(1000);
ShowSerialData;
}
void ShowSerialData()
{
while(client.available()!=0)
{
Serial.write(client.read());
delay(100);
}
}

火力基地功能:

const functions = require('firebase-functions');
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
// Check for POST request
if (request.method !== "POST") {
console.log('Please send a POST request');
}
if (request.method !== "GET") {
console.log('Please send a GET request');
}
let data = request.body;
console.log(`Hello from Firebase! ${JSON.stringify(data)}`);
// console.log('testing: ' + data[0]);
// console.log('testing: ' + data.longitude);
// console.log('testing: ' + data['longitude']);
});

谢谢大家!

这绝不是一个确定的解决方案,但应该适用于大多数SIM模块,但请确保您拥有用于SSL连接的最新固件。 这只是我项目中的代码片段,您只能使用这些内置HTTP函数的SIM模块进行POST,GET和DELETE,没有PUT和PATCH,因此Firebase有点限制。 这是非常基本的代码,因此您可能需要在这里和那里调整一些时间。如果您输入代码来检查响应,它将更快地工作,大多数延迟是不必要的。

首先尝试以下命令:

AT+HTTPSSL=1

如果您收到错误,则必须在使用 Firebase 之前更新模块的固件。

Firebase 仅适用于 HTTPS 连接。


// Firebase HTTPS connection, POST example
#define MODEM_TX             16
#define MODEM_RX             17
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM808 module)
#define client Serial1

void setup()
{
SerialMon.begin(115200);
// Set GSM module baud rate and UART pins
client.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(1000);

float tempIn = 21.5;
float humidity = 54.6;
float pressure = 1010.64;
char  fbtimebuff[32] = "1:30 20/2/21";
char  dataTEST[256];
char        FirebaseUrl[300];   // dimension to suit required character space
const char  FirebaseID[100]   = "https://my-project-details.firebaseio.com";    // project ID
const char  FirebaseAuth[100] = "YPXm66X-My-Big-Secrete-J9xsZsL";               // secret
strcpy(FirebaseUrl, FirebaseID);                      // Firebase account ID
strcat(FirebaseUrl, "/GPRS-Test/.json?auth=");        // Parenet/child path + .json and auth
strcat(FirebaseUrl, FirebaseAuth);
sprintf(dataTEST, "{"TempIn" : "%3.2f","Humidity" : "%2.2f","Pressure" : "%4.2f","Time" : "%s"}", tempIn, humidity, pressure, fbtimebuff);

// connect to GPRS network
SerialMon.printf("nnConnect to GPRSn");
client.println("AT+CIPSHUT");
ShowSerialData();
delay(500);
client.println("AT+CGATT=1");
ShowSerialData();
delay(1000);
client.println("AT+SAPBR=3,1,"Contype","GPRS"");
ShowSerialData();
delay(1000);
client.println("AT+SAPBR=1,1");
ShowSerialData();
delay(1000);
client.println("AT+SAPBR=2,1");
ShowSerialData();
delay(1000);
// post to Firebase
SerialMon.printf("post function to Firebasen");
client.println("AT+HTTPINIT");
ShowSerialData();
delay(1000);
client.println("AT+HTTPSSL=1");    // set SSL for HTTPS
ShowSerialData();
delay(1000);
client.println("AT+HTTPPARA="CID",1");
ShowSerialData();
delay(1000);
client.println("AT+HTTPPARA="URL"," + String(FirebaseUrl));
ShowSerialData();
delay(1000);  client.println("AT+HTTPPARA="CONTENT","application/json"");
ShowSerialData();
delay(1000);
int stlen = strlen(dataTEST);
SerialMon.printf("length: %dn", stlen);
client.println("AT+HTTPDATA=" + String(stlen) + ",100000");
ShowSerialData();
delay(1000);
SerialMon.printf("set datan");
client.println(dataTEST);
ShowSerialData();
delay(1000);
SerialMon.printf("POST data to Firebasen");
client.println("AT+HTTPACTION=1");
ShowSerialData();
delay(6000);
client.println("AT+HTTPREAD");
ShowSerialData();
delay(1000);
SerialMon.printf("Dissconectn");
client.println("AT+HTTPTERM");
ShowSerialData();
delay(1000);
// now disconnect from GPRS network
client.println("AT+CIPSHUT");
ShowSerialData();
delay(1000);
client.println("AT+SAPBR=0,1");
ShowSerialData();
delay(1000);
client.println("AT+CGATT=0");
ShowSerialData();
delay(1000);
}
void loop()
{
}

相关内容

  • 没有找到相关文章

最新更新