mavishak / cnss-embedded

1 stars 0 forks source link

Sending HTTP request from STM32 to Firebase via ESP8266 #15

Open mavishak opened 3 years ago

mavishak commented 3 years ago

Some pointers:

mavishak commented 3 years ago

We managed to send a post msg to the Real-time database in Firebase

04.03.2021:

We sent a POST HTTPS request using port 443 and SSL

  1. We needed to write: AT+CIPSSLSIZE=4096 ESPRESSIF: Board index English Forum Explore General Discussion
  2. We needed to write: _sprintf((char)command, "AT+CIPSTART=\"SSL\",\"%s\",%ld\r\n",(char)firebase_host, httpsport) StackExchange: Making HTTPS requests using ESP8266 AT commands
  3. The POST message was:

    POST /rest/test/posts.json? HTTP/1.1 Host: **** Content-Type: application/json Content-Length: 49

    {"author": "Nemo Resh", "title": "Fish are blue"}

Note: we got the format of the HTTP message using Postman and Firebase documentation Firebase: Saving Data

mavishak commented 3 years ago

Moving Forwards

The HTTP request to firebase should be as follows:

PUT /devices/[device_id]/history/[dd-mm-yyyy]/[hh:mm].json?auth=[DATABASE_SECRET] HTTP/1.1

Host: [DATABASE_HOST] Content-Type: application/json Content-Length: [BODY LENGTH]

{"image_path": "image", "notes": "alarm went off"}

see: How do I authenticate my Firebase with just a string key

A Few Issues

mavishak commented 3 years ago

We need to find a way to get the actual date and time when an alert is created. We don't expect to work because this is an embedded system, but we checked it anyways: see: c function localtime

void timestamp(void){

       time_t rawtime;
       struct tm *info;
       time( &rawtime );
       info = localtime( &rawtime );
       memset((char*)command, '\0', COMMAND_SIZE*sizeof(uint8_t));
       sprintf((char*)command, "Current local time and date: %s", asctime(info));
       write_usart2((uint8_t*)command);

}

The output time and date was not correct:

Current local time and date: Wed Dec 31 23:59:59 1969

We tried playing with firebase server value - timestamp:

_{"imagepath": "image", "notes": "alarm went off!", "time": {".sv": "timestamp"}}

We did not find a way to insert the timestamp in the path and it seems the timestamp does not indicate the actual time and date as it is the time since UNIX epoch, in milliseconds. see: Firebase Timestamp and: Firebase server values Rest API

mavishak commented 3 years ago

06.03.2021

After consulting our instructor we have decided to use the Firebase timestamp and convert it to the date and time using: UNIX timestamp converters.

var s = new Date(1504095567183).toLocaleDateString("en-US") // expected output "8/30/2017"
console.log(s);

see: convert UNIX timestamp to date and time convert timestamp to date and time in angular

NaomiCreate commented 3 years ago

07.03.2021

We organized the code. Now the alert is recorded under the device-ID in the Real-Time Database. !TBD: