ESP8266 and nRF51822 simple WiFi connection and temperature publishing to a socket server.

Dependencies:   BLE_API DnsQuery ESP8266Interface NetworkSocketAPI mbed nRF51822

Fork of BLE_TemperatureBeacon by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
kixorz
Date:
Sat Aug 29 21:58:02 2015 +0000
Parent:
5:f4d74a8cad43
Commit message:
wifi

Changed in this revision

DnsQuery.lib Show annotated file Show diff for this revision Revisions of this file
ESP8266Interface.lib Show annotated file Show diff for this revision Revisions of this file
NetworkSocketAPI.lib Show annotated file Show diff for this revision Revisions of this file
TMP_nrf51/TMP_nrf51.cpp Show diff for this revision Revisions of this file
TMP_nrf51/TMP_nrf51.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
temp.cpp Show annotated file Show diff for this revision Revisions of this file
wifi.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DnsQuery.lib	Sat Aug 29 21:58:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/sarahmarshy/code/DnsQuery/#12d08f0f20cf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266Interface.lib	Sat Aug 29 21:58:02 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/NetworkSocketAPI/code/ESP8266Interface/#6426873b21bd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetworkSocketAPI.lib	Sat Aug 29 21:58:02 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#412be55c4ee8
--- a/TMP_nrf51/TMP_nrf51.cpp	Wed Aug 26 12:21:47 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "TMP_nrf51.h"
-#include "nrf_soc.h" // for internal thermometer sensor
-
-/**
- * @brief Get the temperature value.
- *
- * @return Die temperature in °C
- */
-TMP_nrf51::TempSensorValue_t TMP_nrf51::get(void)
-{
-    int32_t p_temp;
-    sd_temp_get(&p_temp);
-
-    return ((TempSensorValue_t)p_temp * 0.25); /* 0.25 is temperature sensor resolution */
-}
--- a/TMP_nrf51/TMP_nrf51.h	Wed Aug 26 12:21:47 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef TMP_NRF51_H
-#define TMP_NRF51_H
-
-#include "mbed.h"
-
-//!Library for the nrf51822 internal temperature sensor.
-
-class TMP_nrf51
-{
-public:
-    typedef float TempSensorValue_t;
-
-    /**
-    * @brief Get the temperature value.
-    *
-    * @return Die temperature in °C.
-    */
-    TempSensorValue_t get(void);
-};
-
-#endif
--- a/main.cpp	Wed Aug 26 12:21:47 2015 +0000
+++ b/main.cpp	Sat Aug 29 21:58:02 2015 +0000
@@ -17,69 +17,50 @@
 #include "mbed.h"
 #include "toolchain.h"
 #include "ble/BLE.h"
-#include "TMP_nrf51/TMP_nrf51.h"
+#include "HealthThermometerService.h"
 
 BLE ble;
 
 static Ticker ticker;
-static TMP_nrf51  tempSensor;
-static bool       triggerTempValueUpdate = false;
-static DigitalOut alivenessLED(LED1, 1);
-
-struct ApplicationData_t {
-    uint16_t                     applicationSpecificId; /* An ID used to identify temperature value in the manufacture specific AD data field */
-    TMP_nrf51::TempSensorValue_t tmpSensorValue;        /* User defined application data */
-} PACKED;
+static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
+const static char DEVICE_NAME[] = "Tower Thermometer";
 
-void periodicCallback(void)
-{
-    alivenessLED = !alivenessLED;  /* Do blinky on LED1 while we're waiting for BLE events. */
-
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread (where we should be able to block safely, if needed). */
-    triggerTempValueUpdate = true;
-}
+extern float getTemp();
 
-void setupApplicationData(ApplicationData_t &appData)
-{
-    static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
-    appData.applicationSpecificId = APP_SPECIFIC_ID_TEST;
-    appData.tmpSensorValue        = tempSensor.get();
-}
+extern void wifiInit();
+extern void wifiSend(float temp);
 
-void startAdvertisingTemperature(void)
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
-    /* Setup advertising payload */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
-    ApplicationData_t appData;
-    setupApplicationData(appData);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
-
-    /* Setup advertising parameters */
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(500);
-
     ble.gap().startAdvertising();
 }
 
 int main(void)
-{
-    ticker.attach(periodicCallback, 2); /* trigger sensor polling every 2 seconds */
-
+{    
+    //wifi init
+    wifiInit();
+    
+    //ble advertising
+    /*
     ble.init();
-    startAdvertisingTemperature();
+    ble.gap().onDisconnection(disconnectionCallback);
+    HealthThermometerService thermometerService(ble, getTemp(), HealthThermometerService::LOCATION_EAR);
+    
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().setAdvertisingInterval(500);
+    ble.gap().startAdvertising();
+    */
+    while (1) {
+        float temp = getTemp();
 
-    while (true) {
-        if (triggerTempValueUpdate) {
-            // Do blocking calls or whatever hardware-specific action is necessary to poll the sensor.
-            ApplicationData_t appData;
-            setupApplicationData(appData);
-            ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&appData, sizeof(ApplicationData_t));
+        wifiSend(temp);   
+        //thermometerService.updateTemperature( temp );
 
-            triggerTempValueUpdate = false;
-        }
-
-        ble.waitForEvent();
+        //ble.waitForEvent();
+        wait(3);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/temp.cpp	Sat Aug 29 21:58:02 2015 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+#include <math.h>
+AnalogIn thermistor(P0_3);
+
+int beta = 3975;
+float temperature, resistance;
+
+float getTemp() {
+    unsigned int a = thermistor.read_u16(); /* Read analog value 16 bit 65536*/
+    
+    /* Calculate the resistance of the thermistor from analog votage read. positive feedback 100k to ground 10 bit ADC*/
+    resistance= (float) 100000.0 * ((1023.0 / a) - 1.0);
+
+    /* Convert the resistance to temperature using Steinhart's Hart equation */
+    temperature=(1/((log(resistance/100000.0)/beta) + (1.0/298.15)))-273.15; 
+    return temperature;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wifi.cpp	Sat Aug 29 21:58:02 2015 +0000
@@ -0,0 +1,52 @@
+/* NetworkSocketAPI Example Program
+ * Copyright (c) 2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mbed.h"
+#include "ESP8266Interface.h"
+
+DigitalOut myled(LED1);
+DigitalOut status(LED2);
+void flash(){ myled = !myled; }
+
+void setStatus(int num){
+    for(int i = 0; i < num * 2; ++i){
+        status = !status;
+        wait(0.2f);
+    }
+}
+
+ESP8266Interface wifi(P0_23, P0_21);
+
+static SocketInterface* mySocket;
+void wifiInit() {
+    wifi.init();
+    int cs = wifi.connect("DemoRoom", "");
+    setStatus(cs == 0 ? 1 : 3);
+
+    mySocket = wifi.allocateSocket(SOCK_TCP);
+
+    mySocket->setAddressPort("192.168.2.235", 1234);
+    mySocket->open();
+    
+    //TODO
+    //mySocket->close();
+    //wifi.disconnect();
+}
+
+void wifiSend(float temp) {
+    setStatus(1);
+    mySocket->send(&temp, sizeof(temp), 100);
+}
\ No newline at end of file