LM61のセンサ値をAmbientにアップロードするプログラムです。

Dependencies:   mbed-http

Files at this revision

API Documentation at this revision

Comitter:
JKsoft_main
Date:
Mon Jan 14 11:44:47 2019 +0000
Parent:
0:b3812b1c103d
Commit message:
f

Changed in this revision

Ambient_os/Ambient.cpp Show annotated file Show diff for this revision Revisions of this file
Ambient_os/Ambient.h Show annotated file Show diff for this revision Revisions of this file
C12832.lib Show diff for this revision Revisions of this file
LM75B.lib Show diff for this revision Revisions of this file
MMA7660.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
ssl_ca_pem.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ambient_os/Ambient.cpp	Mon Jan 14 11:44:47 2019 +0000
@@ -0,0 +1,125 @@
+#include "Ambient.h"
+
+
+const char* AMBIENT_HOST = "http://ambidata.io";
+const int AMBIENT_PORT = 80;
+const char* AMBIENT_HOST_DEV = "192.168.0.6";
+const int AMBIENT_PORT_DEV = 4567;
+
+const char * ambient_keys[] = {"\"d1\":\"", "\"d2\":\"", "\"d3\":\"", "\"d4\":\"", "\"d5\":\"", "\"d6\":\"", "\"d7\":\"", "\"d8\":\"", "\"lat\":\"", "\"lng\":\"", "\"created\":\""};
+
+Ambient::Ambient() {
+}
+
+bool
+Ambient::init(NetworkInterface* nif, unsigned int channelId, const char * writeKey, int dev) {
+    this->channelId = channelId;
+
+    if (sizeof(writeKey) > AMBIENT_WRITEKEY_SIZE) {
+        return false;
+    }
+    strcpy(this->writeKey, writeKey);
+
+    if(NULL == nif) {
+        return false;
+    }
+    this->_nif = nif;
+    this->dev = dev;
+    if (dev) {
+        strcpy(this->host, AMBIENT_HOST_DEV);
+        this->port = AMBIENT_PORT_DEV;
+    } else {
+        strcpy(this->host, AMBIENT_HOST);
+        this->port = AMBIENT_PORT;
+    }
+    for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
+        this->data[i].set = false;
+    }
+    return true;
+}
+
+bool
+Ambient::set(int field, char * data) {
+    --field;
+    if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
+        return false;
+    }
+    if (strlen(data) > AMBIENT_DATA_SIZE) {
+        return false;
+    }
+    this->data[field].set = true;
+    strcpy(this->data[field].item, data);
+
+    return true;
+}
+
+bool
+Ambient::set(int field, int data) {
+    char buf[12];
+    snprintf(buf, sizeof(buf), "%d", data);
+    return (set(field, buf));
+}
+
+bool
+Ambient::set(int field, double data) {
+    char buf[12];
+    snprintf(buf, sizeof(buf), "%f", data);
+    return (set(field, buf));
+}
+
+bool
+Ambient::clear(int field) {
+    --field;
+    if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
+        return false;
+    }
+    this->data[field].set = false;
+
+    return true;
+}
+
+bool
+Ambient::send() {
+
+    char url[256];
+
+    memset(url, 0, sizeof(url));
+    strcat(url, this->host);
+    sprintf(&url[strlen(url)], "/api/v2/channels/%d/data", this->channelId);
+
+    HttpRequest* post_req = new HttpRequest(_nif, HTTP_POST, url);
+
+    char body[192];
+    char str[180];
+
+    memset(body, 0, sizeof(body));
+    strcat(body, "{\"writeKey\":\"");
+    strcat(body, this->writeKey);
+    strcat(body, "\",");
+
+    for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
+        if (this->data[i].set) {
+            strcat(body, ambient_keys[i]);
+            strcat(body, this->data[i].item);
+            strcat(body, "\",");
+        }
+    }
+    body[strlen(body) - 1] = '\0';
+    strcat(body, "}\r\n");
+    
+    memset(str, 0, sizeof(str));
+    sprintf(str, "%d", strlen(body));
+    post_req->set_header("Content-Length", str);
+    post_req->set_header("Content-Type", "application/json");
+
+    HttpResponse* post_res = post_req->send(body, strlen(body));
+    
+    if(post_res->get_status_code() == 200){
+        delete post_req;
+        return true;
+    }
+    
+    delete post_req;    
+    
+    return false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ambient_os/Ambient.h	Mon Jan 14 11:44:47 2019 +0000
@@ -0,0 +1,79 @@
+#ifndef Ambient_h
+#define Ambient_h
+
+#include "mbed.h"
+#include "http_request.h"
+
+#define AMBIENT_WRITEKEY_SIZE 18
+#define AMBIENT_MAX_RETRY 5
+#define AMBIENT_DATA_SIZE 24
+#define AMBIENT_NUM_PARAMS 11
+#define AMBIENT_TIMEOUT 3000 // milliseconds
+
+class Ambient
+{
+public:
+    /** Create Ambient instance
+     */
+    Ambient(void);
+
+    bool init(NetworkInterface* nif, unsigned int channelId, const char * writeKey, int dev = 0);
+
+    /** Set data on field-th field of payload.
+     * @param field index of payload (1 to 8)
+     * @param data data
+     * @returns
+     *    true on success,
+     *    false on failure
+     */
+    bool set(int field, char * data);
+
+    /** Set data on field-th field of payload.
+     * @param field index of payload (1 to 8)
+     * @param data data
+     * @returns
+     *    true on success,
+     *    false on failure
+     */
+    bool set(int field, int data);
+
+    /** Set data on field-th field of payload.
+     * @param field index of payload (1 to 8)
+     * @param data data
+     * @returns
+     *    true on success,
+     *    false on failure
+     */
+    bool set(int field, double data);
+
+    /** Clear data on field-th field of payload.
+     * @param field index of payload (1 to 8)
+     * @returns
+     *    true on success,
+     *    false on failure
+     */
+    bool clear(int field);
+
+    /** Send data to Ambient
+     * @returns
+     *    true on success,
+     *    false on failure
+     */
+    bool send(void);
+
+private:
+
+    NetworkInterface * _nif;
+    unsigned int channelId;
+    char writeKey[AMBIENT_WRITEKEY_SIZE];
+    int dev;
+    char host[18];
+    int port;
+
+    struct {
+        int set;
+        char item[AMBIENT_DATA_SIZE];
+    } data[AMBIENT_NUM_PARAMS];
+};
+
+#endif // Ambient_h
--- a/C12832.lib	Wed Aug 01 00:14:57 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://os.mbed.com/users/chris/code/C12832/#7de323fa46fe
--- a/LM75B.lib	Wed Aug 01 00:14:57 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://os.mbed.com/users/chris/code/LM75B/#6a70c9303bbe
--- a/MMA7660.lib	Wed Aug 01 00:14:57 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://os.mbed.com/users/Sissors/code/MMA7660/#36a163511e34
--- a/main.cpp	Wed Aug 01 00:14:57 2018 +0000
+++ b/main.cpp	Mon Jan 14 11:44:47 2019 +0000
@@ -1,61 +1,23 @@
 #include "mbed.h"
-#include <sstream>
 #include "easy-connect.h"
 #include "https_request.h"
-#include "ssl_ca_pem.h"
-#include "C12832.h"
-#include "MMA7660.h"
-#include "LM75B.h"
+#include "Ambient.h"
 
-C12832 lcd(D11, D13, D12, D7, D10);
-MMA7660 MMA(D14,D15);
-LM75B sensor(D14,D15);
+unsigned int channelId = 0;
+const char* writeKey = "";
+Ambient ambient;
 
+AnalogIn ain(A0);
 Serial pc(USBTX,USBRX);
 
-const char API_TOKEN[] = "api-token";
-const char URL[] = "https://{domain}.cybozu.com/k/v1/record.json";
-
-int app_id = 2;
-
-bool post_kintone(NetworkInterface* nif, const char *url, int app_id_, char* field_code, float value)
-{    
-    HttpsRequest* post_req = new HttpsRequest(nif, SSL_CA_PEM, HTTP_POST, url);
-    post_req->set_header("X-Cybozu-API-Token", API_TOKEN);
-    post_req->set_header("Content-Type", "application/json");
-    
-    std::stringstream ss_body;
-        
-    ss_body << "{\"app\": " << app_id_ << ", \"record\": {\"" << field_code << "\": {\"value\": \"" << value << "\"}}}\n";
-    
-    string body = ss_body.str();
-
-    printf("body:%s\r\n",body.c_str());
-    
-    HttpResponse* post_res = post_req->send(body.c_str(), body.length());
-    
-    printf("res:%s\r\n",post_res->get_body_as_string().c_str());
-    
-    
-    if(post_res->get_status_code() == 200){
-        delete post_req;
-        return true;
-    }
-    
-    delete post_req;    
-    
-    return false;
-}
 
 // main() runs in its own thread in the OS
 int main() {
     NetworkInterface* network = NULL;
 
-    pc.baud(115200);
+    pc.baud(9600);
 
-    lcd.cls();
-    lcd.locate(0,3);
-    lcd.printf("Network Connect..");
+    pc.printf("Network Connect..");
 
     pc.printf("\r\n----- Start -----\r\n");
     
@@ -65,21 +27,17 @@
         return -1;
     }
 
-    lcd.printf("OK");
     pc.printf("\r\n----- Network Connected -----\r\n");
     
     wait(2.0);
+
+    ambient.init(network,channelId, writeKey);
     
     while(1) {
-        lcd.cls();
-        lcd.locate(0,3);
-        lcd.printf("x=%.2f y=%.2f z=%.2f",MMA.x(), MMA.y(), MMA.z());
-        
-        float temp = sensor.read();
-        lcd.locate(0,14);
-        lcd.printf("Temp = %.1f\n", temp);
-        
-        bool ret = post_kintone(network, URL, app_id, "temp", temp);
+        float tmp = (ain * 3.3 -0.6)/0.01;
+        ambient.set(1, tmp);
+
+        bool ret = ambient.send();        
         pc.printf("\n----- HTTPS POST response [%s]----- \n\r",ret== true ? "OK" : "NG");
         
         wait(10.0);
--- a/mbed_app.json	Wed Aug 01 00:14:57 2018 +0000
+++ b/mbed_app.json	Mon Jan 14 11:44:47 2019 +0000
@@ -7,7 +7,7 @@
         },
         "network-interface":{
             "help": "options are ETHERNET, WIFI_ESP8266, WIFI_ODIN, WIFI_RTW, MESH_LOWPAN_ND, MESH_THREAD, CELLULAR_ONBOARD",
-            "value": "WIFI_ESP8266"
+            "value": "ETHERNET"
         },
         "mesh_radio_type": {
             "help": "options are ATMEL, MCR20",
@@ -22,10 +22,10 @@
             "value": "D0"
         },
         "wifi-ssid": {
-            "value": "\"ssid\""
+            "value": "\"devCamp2018\""
         },
         "wifi-password": {
-            "value": "\"password\""
+            "value": "\"0802devCamp\""
         },
         "esp8266-debug": {
             "value": null
--- a/ssl_ca_pem.h	Wed Aug 01 00:14:57 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-const char SSL_CA_PEM[] = "-----BEGIN CERTIFICATE-----\n"
-"MIIFADCCA+igAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBjzELMAkGA1UEBhMCVVMx\n"
-"EDAOB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