A Telegram BOT for this awesome all-in-one board.

Dependencies:   BSP_B-L475E-IOT01 mbed es_wifi jsmn

Telegram Bot for DISCO_L475VG_IOT01

This application embeds aTelegram chatbot into the DISCO_L475VG_IOT01 board.

The Bot answers to the users queries about:

  • Real time environmental data taken from the on board sensors.
  • Environmental data history of the latest 24 hours stored on board.
  • Camera images taken from the Arducam-mini-2mp (optional).

This software uses:

Compilation

Import in your compiler and modify the following defines:

  • WIFI_SSID
  • WIFI_PASSWORD
  • TELEGRAM_BOT_APIKEY

Please follow the Telegram bots documentation (https://core.telegram.org/bots) to better understand how the Telegram API works and how to create your bot.

In order to support the Arducam-Mini-2MP set WITH_ARDUCAM_2640 #define to 1.

Screenshots

/media/uploads/dvddnr/screenshot_20180130-073732.png /media/uploads/dvddnr/screenshot_20180130-073703.png /media/uploads/dvddnr/arducam.jpeg /media/uploads/dvddnr/screenshot_20180216-102601.png

Security

The Inventek wifi module creates the ssl connection but does not authenticate the server's certificate ( AT cmd P9=0 ).

For more details http://www.inventeksys.com/IWIN/programming-certificates-tcp-ssltls/

Revision:
8:89fe332bc412
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPHelper.h	Thu Feb 15 14:31:15 2018 +0000
@@ -0,0 +1,117 @@
+#pragma once
+
+#include <mbed.h>
+#include "es_wifi.h"
+
+#define HTTP_200OK "200 OK\r\n"
+#define HTTP_CL "Content-Length: "
+
+class HTTPHelper
+{
+private:
+    char m_io_buffer[ES_WIFI_PAYLOAD_SIZE];
+
+public:
+
+    HTTPHelper() {}
+    ~HTTPHelper(){}
+
+
+    bool HttpParseResponse(TCPConnector *net,uint8_t socket, char *content_buffer, uint32_t content_buffer_size, bool *status_ok, uint32_t *content_size)
+    {
+        uint16_t recv_bytes = 0;
+        uint16_t http_content_len = 0;
+        char *http_content_pivot = NULL;
+        uint16_t content_chunk_size;
+        bool res = false;
+
+        *status_ok = false;
+        *content_size = 0;
+
+        // receive RESPONSE
+        m_io_buffer[0] = 0;
+        if(net->tcp_read(socket,m_io_buffer,ES_WIFI_PAYLOAD_SIZE,&recv_bytes,false))
+        {
+            if (recv_bytes > 0)
+            {
+                m_io_buffer[recv_bytes]=0;
+                printf("%s\r\n",m_io_buffer);
+                http_parse_response(m_io_buffer, recv_bytes, status_ok, &http_content_len);
+                printf("> HTTP OK = %d Content len = %d\r\n", *status_ok, http_content_len);
+                *content_size = http_content_len;
+
+                /* fetch content */
+                http_content_pivot = strstr(m_io_buffer, "\r\n\r\n");
+                if (http_content_pivot != NULL)
+                {
+                    /* copy first content chunk */
+                    http_content_pivot += 4;
+                    content_chunk_size = strlen(http_content_pivot);
+                    if (content_chunk_size < content_buffer_size) strcpy(content_buffer, http_content_pivot);
+                    http_content_len -= content_chunk_size;
+                    printf("http_content_len %d content_chunk_size %d\r\n",http_content_len,content_chunk_size);
+
+                    /* continue to fetch content chunck */
+                    while (http_content_len > 0)
+                    {
+                        printf("http_content_len %d\r\n",http_content_len);
+                        if( net->tcp_read(socket,m_io_buffer,ES_WIFI_PAYLOAD_SIZE,&recv_bytes,true) == false ) break;
+                        m_io_buffer[recv_bytes] = 0;
+                        http_content_len -= recv_bytes;
+                        if (recv_bytes == 0)
+                        {
+                            printf("> ERROR : http content len overflow\r\n");
+                            break;
+                        }
+                        strcat(content_buffer,m_io_buffer);
+                    }
+                    res = (http_content_len == 0);
+                }
+
+            }
+        }
+
+        return res;
+
+    }
+
+private:
+
+    void http_parse_response(char *http_chunk, uint16_t http_chunk_len, bool *status_code_ok, uint16_t *content_len)
+    {
+        char *line_pivot, *key_pivot;
+
+        *status_code_ok = false;
+        *content_len = 0;
+
+        line_pivot = strstr(http_chunk, "\r\n");
+        if (line_pivot == NULL)
+            return ;
+
+        key_pivot = strstr(http_chunk, HTTP_200OK);
+        
+        if (key_pivot != NULL)
+            *status_code_ok = true;
+        else
+            *status_code_ok = false;
+
+        *content_len = 0;
+        while (1)
+        {
+            line_pivot = strstr(line_pivot + 2, "\r\n");
+            if (line_pivot == NULL)
+                break;
+            key_pivot = strstr(http_chunk, HTTP_CL);
+            if (key_pivot == NULL)
+                continue;
+            if (key_pivot < line_pivot)
+            {
+                key_pivot += strlen(HTTP_CL);
+                *content_len = atoi((char const *)key_pivot);
+                break;
+            }
+        }
+    }
+
+
+};
\ No newline at end of file