for orginal PCB

Dependencies:   EthernetInterface SCP1000 WebSocketClient mbed-rtos mbed

Fork of ku-make_sensor201302 by Satoshi Motoyama

main.cpp

Committer:
kumajoi
Date:
2013-02-09
Revision:
5:8d05d8d98218
Parent:
4:99410a172710

File content as of revision 5:8d05d8d98218:

#include "mbed.h"
#include "SCP1000.h"
#include "EthernetInterface.h"
#include "Websocket.h"

DigitalOut myled(LED1);
AnalogIn light_in(p20);
AnalogIn temp_in(p19);
AnalogIn humid_in(p18);
SCP1000 scp1000(p11,p12,p13,p14);


int main()
{

    const char apikey[] = "xxxxxxxxxxxxxxxxxxxxxx"; //Change your apikey
    const char feed[]= "/feeds/00000";  //Change your feed URL

    char cms[512];
    char recv[512];
    int token = 0;
    EthernetInterface eth;
    eth.init();
    eth.connect();
    wait(5);
    printf("IP Address is %s\n\r", eth.getIPAddress());

    while (1) {
        Websocket ws("ws://api.cosm.com:8080/feeds/");
        ws.connect();
        if(ws.read(recv)) {
            printf("rcv: %s\r\n", recv);
        }

        float r_light, r_temp, r_humid, r_hpa;
        float light, temp, humid, hpa;

        light = light_in;
        temp = temp_in;
        humid = humid_in;
        hpa = scp1000.readPressure();

        // If you want, please change the value.
        r_light = light * 3.3 * 5 / 0.013;
        r_temp = temp * 3.3 * 100 - 60 - 1;
        r_humid = humid * 3.3 * 100 + 2;
        r_hpa = hpa / 100;


        myled = 1;
        // If you want, change the ID, Number of digits.
        sprintf (cms,"{\"method\" : \"put\",\"resource\" : \"%s\",\"params\" : {},\"headers\" : {\"X-ApiKey\":\"%s\"},\"body\" :{\"version\" : \"1.0.0\",\"datastreams\" : [{\"id\" : \"0\",\"current_value\" : \"%4.0f\"},{\"id\" : \"1\",\"current_value\" : \"%3.0f\"},{\"id\" : \"2\",\"current_value\" : \"%3.0f\"},{\"id\" : \"3\",\"current_value\" : \"%4.0f\"}]},\"token\" : \"0x%d\"}\r\n",feed,apikey,r_light,r_temp,r_humid,r_hpa,token);
 
        printf ("%s\r\n",cms);
        token++;
        int res = ws.send(cms);
        myled= 0 ;
        wait(5);

        if(ws.read(recv)) {
            printf("rcv: %s\r\n", recv);
            wait(1);
        }

        ws.close();
        // If you want, change the Interval.
        wait(300);

    }
}