SORACOM Harvest and Grove - Temperature&Humidity Sensor(DHT11) example for Wio cellular targets - Seeed Wio 3G and Wio LTE-M1/NB1(BG96)

Dependencies:   DHT-2

Files at this revision

API Documentation at this revision

Comitter:
asagin
Date:
Thu Jun 06 01:14:27 2019 +0000
Parent:
5:1d355dfee639
Commit message:
It's working!

Changed in this revision

DHT-2.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DHT-2.lib	Thu Jun 06 01:14:27 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/lamell/code/DHT-2/#49350e4b126d
--- a/main.cpp	Wed Jun 05 06:52:22 2019 +0000
+++ b/main.cpp	Thu Jun 06 01:14:27 2019 +0000
@@ -20,11 +20,17 @@
 #include "CellularDevice.h"
 #include "UDPSocket.h"
 #include "CellularLog.h"
+#include "DHT.h"
 
 #define UDP 0
 #define TCP 1
 #define NONIP 2
 
+// define the sensor /
+DHT sensor(D38, DHT11);
+int sensorError = 0;
+float Temperature = 0.0f;
+
 // Number of retries /
 #define RETRY_COUNT 3
 
@@ -165,15 +171,15 @@
 #endif
         return -1;
     }
-/*
-int val = 123;    // 普通の変数
-int* ptr = &val;  // ポインタ(pointer)
-int& ref = val;   // 参照(reference)
-const int cval = 45;  // 定数(constant)
-*/
+    /*
+    int val = 123;    // 普通の変数
+    int* ptr = &val;  // ポインタ(pointer)
+    int& ref = val;   // 参照(reference)
+    const int cval = 45;  // 定数(constant)
+    */
     int n = 0;
     int val = 1;
- //   int *echo_string = &val;
+//   int *echo_string = &val;
     char recv_buf[4];
 
     sock.set_timeout(15000);
@@ -220,15 +226,9 @@
 #else
 //UDP
     char echo_string[100];
-    //char echo_string[] = "{\"count\":15}";
-    //retcode = sock.sendto(sock_addr, (void*)echo_string, sizeof(echo_string)-1);
-    sprintf(echo_string, "{\"count\" : %d}", val);
+    sprintf(echo_string, "{\"Temprature\" : %4.2f}", Temperature);
     retcode = sock.sendto(sock_addr, (void*)echo_string, strlen(echo_string));
-    //retcode = sock.sendto(sock_addr, ("{\"count\":%d}",  *echo_string) , sizeof(echo_string));
     print_function("%s\n", echo_string);
-    /*
-    retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
-    */
     if (retcode < 0) {
         print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
         return -1;
@@ -239,13 +239,6 @@
     n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
 #endif
 
-    /*
-    virtual nsapi_size_or_error_t sendto    (   const char *    host,
-    uint16_t    port,
-    const void *    data,
-    nsapi_size_t    size
-    )
-    */
 #endif
 
     sock.close();
@@ -262,62 +255,64 @@
 
 int main()
 {
-    print_function("\n\nmbed-os-example-cellular_temp\n");
+    print_function("\n\nmbed-os-example-cellular-temp\n");
     print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__);
+
+    for(int count = 1; count < 10; count = count + 1) {
+        sensorError = sensor.readData();
+        if (0 == sensorError) {
+            Temperature   = sensor.ReadTemperature(CELCIUS);
+            
+            } else {
+            print_function("Error: %d\n", sensorError);
+        }
+            
+
 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN
-    print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL"));
+        print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL"));
 #endif
 
-    print_function("Establishing connection\n");
+//    print_function("Establishing connection\n");
 #if MBED_CONF_MBED_TRACE_ENABLE
-    trace_open();
+        trace_open();
 #else
-    dot_thread.start(dot_event);
+        dot_thread.start(dot_event);
 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
 
 #if MBED_CONF_APP_SOCK_TYPE == NONIP
-    iface = CellularContext::get_default_nonip_instance();
+        iface = CellularContext::get_default_nonip_instance();
 #else
-    iface = CellularContext::get_default_instance();
+        iface = CellularContext::get_default_instance();
 #endif
 
-    MBED_ASSERT(iface);
-
-    // sim pin, apn, credentials and possible plmn are taken automatically from json when using NetworkInterface::set_default_parameters()
-    iface->set_default_parameters();
-
-    nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
+        MBED_ASSERT(iface);
 
-    /* Attempt to connect to a cellular network */
-     if (do_connect() == NSAPI_ERROR_OK) {
-        retcode = test_send_recv();
-    }/*
-    if (do_connect() == NSAPI_ERROR_OK) {
-        for(int count = 1; count < 100; count = count + 1) {
-            if(pushButton == 1) {
+        // sim pin, apn, credentials and possible plmn are taken automatically from json when using NetworkInterface::set_default_parameters()
+        iface->set_default_parameters();
+
+        nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
 
-                retcode = test_send_recv();
-
-            }
+        /* Attempt to connect to a cellular network */
+        if (do_connect() == NSAPI_ERROR_OK) {
+            retcode = test_send_recv();
         }
-    }*/
+        wait(1);
+        if (iface->disconnect() != NSAPI_ERROR_OK) {
+            print_function("\n\n disconnect failed.\n\n");
+        }
 
-    if (iface->disconnect() != NSAPI_ERROR_OK) {
-        print_function("\n\n disconnect failed.\n\n");
-    }
-
-    if (retcode == NSAPI_ERROR_OK) {
-        print_function("\n\nSuccess. Exiting \n\n");
-    } else {
-        print_function("\n\nFailure. Exiting \n\n");
-    }
+        if (retcode == NSAPI_ERROR_OK) {
+            print_function("\n\nSuccess. Exiting \n\n");
+        } else {
+            print_function("\n\nFailure. Exiting \n\n");
+        }
 
 #if MBED_CONF_MBED_TRACE_ENABLE
-    trace_close();
+        trace_close();
 #else
-    dot_thread.terminate();
+        dot_thread.terminate();
 #endif
-
+    }
 
     return 0;
 }