Jaafar Benabdallah / Mbed OS DISCO-IOT01_HomeEnv

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X picojson

Fork of HelloWorld_ST_Sensors by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.h Source File

main.h

00001 /* Home Environemnt Monitoring and Logging with STM32 IOT DISCOVERY Board   */
00002 /*                                                                          */
00003 /* Jaafar Benabdallah - 2017 */
00004 
00005 
00006 /* Libraries */
00007 #include "mbed.h"
00008 #include "HTS221Sensor.h"
00009 #include "LPS22HBSensor.h"
00010 #include "LSM6DSLSensor.h"
00011 #include "lis3mdl_class.h"
00012 #include "VL53L0X.h"
00013 #include "wifi.h"
00014 #include "picojson.h"
00015 
00016 /* Syncing Params */
00017 #define SENSE_PERIOD_S  5.0
00018 #define UPLOAD_PERIOD_S 30.0
00019 #define CHECK_PROXIMITY_PERIOD_S  1.0
00020 
00021 // Influx TSDB http api
00022 #define INFLUX_SERVER_URL   "rpi1.nrjy.com"
00023 #define INFLUX_SERVER_PORT  8086
00024 #define INFLUX_WRITE_EP     "/write?db=HENV2&precision=s&rp=HENV2_1y"
00025 
00026 // time server http api to get current unix time
00027 #define TIME_SERVER_URL     "www.convert-unix-time.com"
00028 #define TIME_SERVER_PORT    80
00029 #define TIME_SERVER_EP      "/api?timestamp=now&timezone=New_York"
00030 
00031 #define WIFI_WRITE_TIMEOUT 3000
00032 #define WIFI_READ_TIMEOUT  3000
00033 #define CONNECTION_TRIAL_MAX    3
00034 
00035 /* Nextion display macros */
00036 
00037 #define RESET_NEXTION           nextion.printf("rest\xff\xff\xff")
00038 #define WIFI_ICON_ON            nextion.printf("vis wifi,1\xff\xff\xff")
00039 #define WIFI_ICON_OFF           nextion.printf("vis wifi,0\xff\xff\xff")
00040 #define UPLOAD_ICON_ON          nextion.printf("vis upload,1\xff\xff\xff")
00041 #define UPLOAD_ICON_OFF         nextion.printf("vis upload,0\xff\xff\xff")
00042 #define NO_WAKEUP_ON_SERIALIN   nextion.printf("usup=0\xff\xff\xff")
00043 #define WAKEUP_ON_TOUCH         nextion.printf("thup=1\xff\xff\xff")
00044 #define SLEEP_ON_NOTOUCH_30S    nextion.printf("thsp=30\xff\xff\xff")
00045 #define WAKE_UP_DISPLAY         nextion.printf("sleep=0\xff\xff\xff")
00046 
00047 
00048 /* Interface definition */
00049 static DevI2C devI2c(PB_11,PB_10);
00050 Serial pc(USBTX, USBRX, 115200);
00051 Serial nextion(PA_0, PA_1, 115200); //i.e. D1, D0 : Serial 4
00052 DigitalOut led3(LED3);// WiFi module status
00053 DigitalOut ledhttp(LED2);// Http activity
00054 
00055 
00056 /* Operation flags */
00057 volatile bool   sense_enabled = false;
00058 volatile bool   take_measurements = false;
00059 volatile bool   send_measurements = false;
00060 volatile bool   check_proximity = false;
00061 bool rtc_synced = false;
00062 bool WiFi_on = false;
00063 
00064 
00065 /* Timers   */
00066 Ticker  getMeasurementsTicker;
00067 Ticker  sendMeasurementsTicker;
00068 Ticker  checkProximityTicker;
00069 // Ticker  WiFi_led_ticker;
00070 
00071 
00072 /* Environmental sensors */
00073 static LPS22HBSensor press_temp(&devI2c);
00074 static HTS221Sensor hum_temp(&devI2c);
00075 /* Motion sensors */
00076 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
00077 static LIS3MDL magnetometer(&devI2c);
00078 /* Range sensor  */
00079 static DigitalOut shutdown_pin(PC_6);
00080 static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
00081 
00082 
00083 /* Environment variables */
00084 uint32_t distance_val = 0;
00085 float tempC_val = 0;
00086 float tempF_val = 0;
00087 float RH_val = 0;
00088 float Patm_val = 0;
00089 float tempC_val2 = 0;
00090 
00091 
00092 /* Comm vars */
00093 static   uint8_t http_request[1024];
00094 static   uint8_t http_resp[512];
00095 uint16_t reqLen;
00096 uint16_t respLen;
00097 uint32_t request_sent = 0;
00098 uint32_t request_acked = 0;
00099 
00100 
00101 /* Functions Prototypes */
00102 bool connectToServer(char *serverUrl, uint16_t serverPort, uint8_t socket_id);
00103 bool connectWiFi(void);
00104 bool checkWiFi(void);
00105 void time_to_sense_cb(void);
00106 void time_to_send_cb(void);
00107 void time_to_check_distance_cb(void);
00108 bool sync_rtc(void);
00109 bool getCurrentTime(uint8_t socket_id, uint32_t *timeStamp);
00110 // void toggle_led3_cb();