0

I want to send data to Cloud Firestore database using Sim808 GPRS module and Arduino UNO.

The code I uploaded to Arduino is as follows:

  #include <DFRobot_sim808.h>
  #include <SoftwareSerial.h>

  #define PIN_TX    10
  #define PIN_RX    11
  SoftwareSerial mySerial(PIN_TX,PIN_RX);
  DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

  char http_cmd[] = "GET /add?lat=45.23164&long=42.45737&date=29.01.2020 HTTP/1.0\r\n\r\n";
  char buffer2[512];

  void setup(){
    mySerial.begin(9600);
    Serial.begin(9600);

    //******** Initialize sim808 module *************
    while(!sim808.init()) {
        delay(1000);
        Serial.print("Sim808 init error\r\n");
    }
    delay(3000);

    //*********** Attempt DHCP *******************
    while(!sim808.join(F("cmnet"))) {
        Serial.println("Sim808 join network error");
        delay(2000);
    }

    //************ Successful DHCP ****************
    Serial.print("IP Address is ");
    Serial.println(sim808.getIPAddress());

    //*********** Establish a TCP connection ************
    if(!sim808.connect(TCP,"us-central1-myprojectname.cloudfunctions.net", 80)) {
        Serial.println("Connect error");
    }else{
        Serial.println("Connect mbed.org success");
    }

    //*********** Send a GET request *****************
    Serial.println("waiting to fetch...");
    sim808.send(http_cmd, sizeof(http_cmd)-1);
    while (true) {
        int ret = sim808.recv(buffer2, sizeof(buffer2)-1);
        if (ret <= 0){
            Serial.println("fetch over...");
            break;
        }
        buffer2[ret] = '\0';
        Serial.print("Recv: ");
        Serial.print(ret);
        Serial.println(" bytes: ");
        Serial.println(buffer2);
        break;
    }

    //************* Close TCP or UDP connections **********
    sim808.close();

    //*** Disconnect wireless connection, Close Moving Scene *******
    sim808.disconnect();
  }

  void loop(){

  }

Then I created a Firebase Functions project using Firebase CLI and the codes I wrote in the index.js file are as follows:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const app = express();

admin.initializeApp();
var fs = admin.firestore();
app.get("/",(req,res)=>{
    var lat = req.param("lat");
    var long = req.param("long");
    var date = req.param("date");
    fs.collection("locations").add({
        "date":date,
        "lat":lat,
        "long":long
    }).then(()=>{
            res.send("Location :"+lat+","+long);
            return null;
    }).catch((err)=>{
        console.log(err);
        res.send("Error");

    });
});

exports.add = functions.https.onRequest(app);

Then I get a link using the "firebase-deploy" command. The project console link looks like this: "https://us-central1-myprojectname.cloudfunctions.net/add?lat=42.23164&long=43.45737&date=29.01.2020"

Finally, I watch Arduino UNO on the serial port screen. But I get 404 (Not Found) error.

Note : When I go to the link with the browser, I can add new records to the Firestore database.

Why did I get a 404 (Not Found) error? Can you help me? Thanks in advance.

1 Answers1

0

Your client request is a bit too simplistic. You are basically using this one line as your HTTP protocol implementation:

GET /add?lat=45.23164&long=42.45737&date=29.01.2020 HTTP/1.0

This won't work for most hosted services that use DNS aliases to host multiple sites behind a single IP address. The server doesn't actually know which Cloud Functions project you're referring to.

Ideally, you should find and use a proper HTTP client library that knows how make requests to modern HTTP services. But if you really can't do that, you should at least send a Host header with your request, so that Cloud Functions can identify your project using the DNS host assigned to your project:

GET /add?lat=45.23164&long=42.45737&date=29.01.2020 HTTP/1.0
Host: us-central1-myprojectname.cloudfunctions.net
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441