#include #include #include #include #include #include #include #include #include static float flow_data = 0.00; //variable to save flow meter value static float pressure_data = 0.00;//variable to save preasure sensor value struct sensor_value voltage, current; //************Device Pointers******************************* const struct device *ads = DEVICE_DT_GET_ONE(ti_ads1100); const struct device *ina = DEVICE_DT_GET_ONE(ti_ina219b); //************Bluetooth******************************* static struct bt_uuid_128 flow_char_uuid = BT_UUID_INIT_128( BT_UUID_128_ENCODE(0x9c85a726, 0xb7f1, 0x11ec, 0xb909, 0x0242ac120002)); //Characteristics UUIDs static struct bt_uuid_128 pressure_char_uuid = BT_UUID_INIT_128( BT_UUID_128_ENCODE(0x2f516d79, 0x77ca, 0x45e2, 0x8fa0, 0x93ab5e5691e0)); //Characteristics UUIDs #define FM_SERVICE_UUID_VAL \ BT_UUID_128_ENCODE(0xf7547938, 0x68ba, 0x11ec, 0x90d6, 0x0242ac120003) static struct bt_uuid_128 fm_svc_uuid = BT_UUID_INIT_128(FM_SERVICE_UUID_VAL); //Flow meter Service UUID // Advertisement Data that nrf52833DK will advertise static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID128_ALL, FM_SERVICE_UUID_VAL), }; // GAP callbacks static void connected(struct bt_conn *conn, uint8_t err) { if (err) { printk("Connection failed (err 0x%02x)\n", err); } else { printk("Connected\n"); } } static void disconnected(struct bt_conn *conn, uint8_t reason) { printk("Disconnected (reason 0x%02x)\n", reason); } BT_CONN_CB_DEFINE(conn_callbacks) = { .connected = connected, .disconnected = disconnected, }; //Mapping from voltage/currentr to pressure/flow static float maping(double in, float in_min, float in_max, float out_min, float out_max) { return (in - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } // GATT Access Callbacks :: Allow the server (Mobile Phone) to read flow data from client(nrf52833DK) static ssize_t read_flow_data(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { int ret = sensor_sample_fetch(ina); if (ret) { printf("Could not fetch data from INA219.\n"); } ret = sensor_channel_get(ina, SENSOR_CHAN_CURRENT, ¤t); if (ret) { printf("Could not fetch data from INA219B.\n"); } flow_data = sensor_value_to_double(¤t); flow_data = flow_data*1000; if (flow_data>=4.00) { flow_data = maping(flow_data, 4.00, 20.00, 0.00, 300.00); } const float *value = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, value, sizeof(*value)); } // GATT Access Callbacks :: Allow the server (Mobile Phone) to read flow data from client(nrf52833DK) static ssize_t read_pressure_data(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset){ double volt; //double volt_a [3]; sensor_sample_fetch(ads); sensor_channel_get(ads, SENSOR_CHAN_VOLTAGE, &voltage); volt = sensor_value_to_double(&voltage); //printk("%f\n", volt); pressure_data = maping(volt, 5.00, 0.00, 0.00, 160.00); pressure_data = pressure_data - 0.00489; const float *value = attr->user_data; return bt_gatt_attr_read(conn, attr, buf, len, offset, value, sizeof(*value)); } // Attribute Table: Declare services and charecteristics with attritubutes and permissions (Read/Write) BT_GATT_SERVICE_DEFINE( flow_svc, BT_GATT_PRIMARY_SERVICE(&fm_svc_uuid), BT_GATT_CHARACTERISTIC(&flow_char_uuid.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_flow_data,NULL, &flow_data), BT_GATT_CHARACTERISTIC(&pressure_char_uuid.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_pressure_data,NULL, &pressure_data)); void main(void) { int err; if (ads == NULL) { printf("\n Error: ADS1100 Node not found.\n"); } if (ina == NULL) { printf("\n Error: INA219 Node not found.\n"); } if (!device_is_ready(ads)) { printf("\n Error: Device \"%s\" is not ready; ", ads->name); } if (!device_is_ready(ina)) { printf("\n Error: Device \"%s\" is not ready; ", ina->name); } err = bt_enable(NULL); if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } printk("Bluetooth initialized\n"); // start avertising err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } if (err==0) { printk("Advertising successfully started\n"); } }