Reputation: 1
Im trying to send POST request with sim900 connected to arduino uno. I send it to firebase functions but I dont get it in the function. I will be happy to get solution for my problem, or alternative way that i can store data from sensors to firebase with sim900 or any other celular network solution).
arduino code:
#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);
}
}
firebase function:
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']);
});
Thank you all!
Upvotes: 0
Views: 1114
Reputation: 11
This is by no means a definitive solution but should work on most SIM modules but make sure you have up to date firmware for SSL connection. This is only a snip of code from my project, you can only POST, GET and DELETE using these SIM modules built in HTTP functions, there's no PUT and PATCH so a bit limited for Firebase. This is very basic code so you may have to tweak some timing here and there. It will work a lot faster if you put code to check the responses, most delays are not necessary.
Try this command first:
AT+HTTPSSL=1
If you get an error you MUST update the module's firmware before using Firebase.
Firebase will only work with HTTPS connection.
// 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("\n\nConnect to GPRS\n");
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 Firebase\n");
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: %d\n", stlen);
client.println("AT+HTTPDATA=" + String(stlen) + ",100000");
ShowSerialData();
delay(1000);
SerialMon.printf("set data\n");
client.println(dataTEST);
ShowSerialData();
delay(1000);
SerialMon.printf("POST data to Firebase\n");
client.println("AT+HTTPACTION=1");
ShowSerialData();
delay(6000);
client.println("AT+HTTPREAD");
ShowSerialData();
delay(1000);
SerialMon.printf("Dissconect\n");
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()
{
}
Upvotes: 0