2012年2月6日月曜日

USB温度・湿度計モジュール(USBRH)をmbedで使う


ストロベリー・リナックスの USB温度・湿度計モジュール(USBRH)をmbedで使ってみました。
USBRHからのデータを温度・湿度に変換するにはSHT11のライブラリを参考にしました。
下記のプログラムでは温度・湿度をLCDに表示、Pachubeにデータをアップロードしています。
USBHostShellに組み込んでいます。

// usbrh.cpp 2012/2/6
#include "mbed.h"
#include "USBHost.h"
#include "Utils.h"
#include "TextLCD.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "PachubeClient.h"
#include 

extern TextLCD lcd;

string pachube_api_key = "";
string pachube_feedID = "";

EthernetNetIf eth;
HTTPClient client;

PachubeClient pachube(pachube_api_key);

u8 usbrh_buf[8];

void usbrh_request(int device)
{
    USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, 0x09, 0x0200, 0, 0, 7);
}

void usbrh_Callback(int device, int endpoint, int status, u8* data, int len, void* userData)
{
    static string old_csv_data;

    float t, rhl, rht;
    unsigned short sot, sorh;
    float d1,d2,c1,c2,c3,t1,t2;

    d1 = -40,1; d2 = 0.01;
    c1 = -2.0468; c2 = 0.0367; c3 = -1.5955/1000000.0;
    t1 = 0.01; t2 = 0.00008;

    //printfBytes("usbrh_Callback", data ,len);
    sorh = data[0] << 8 | data[1];
    sot = data[2] << 8 | data[3];

    t = d1 + d2 * (float)sot;
    rhl = c1 + c2 * (float)sorh + c3 * (float)(sorh * sorh);
    rht = (t - 25.0) * (t1 + t2 * (float)sorh) + rhl;
    if (rht > 99.0) rht = 100.0;

    printf("temperature: %.2f, humidity: %.2f\n", t, rht);
    lcd.locate(0, 0); lcd.printf("t: %.2f", t);
    lcd.locate(0, 1); lcd.printf("h: %.2f", rht);
    wait(10.0);

    char buf[64];
    sprintf(buf, "%.2f,%.2f", t, rht);
    string csv_data = buf; 
    if (old_csv_data != csv_data) {
        old_csv_data = csv_data;
        pachube.PutCsv(pachube_feedID, csv_data);
        printf("Pachube PutCsv[%s]", buf);
        printf("result / response: %d / %d\n", 
        pachube.Result(), pachube.Response());
    }
    USBInterruptTransfer(device, 0x81, usbrh_buf, sizeof(usbrh_buf), usbrh_Callback, userData);
    usbrh_request(device);
}

int OnUsbrhInsert(int device) 
{
    printf("USBRH inserted of %d\n", device);
    USBInterruptTransfer(device, 0x81, usbrh_buf, sizeof(usbrh_buf), usbrh_Callback, (void*)0);
    usbrh_request(device);
    return 0;
}

autoEvents.cppのOnLoadDeviceにOnUsbrhInsertを追加します。
void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
{
    printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
    char s[128];
    for (int i = 1; i < 3; i++)
    {
        if (GetString(device,i,s,sizeof(s)) < 0)
            break;
        printf("%d: %s\n",i,s);
    }

    if (deviceDesc->idVendor == 0x067b && deviceDesc->idProduct == 0x2303) {
        OnPL2303Insert(device);
        return;
    }

    if (deviceDesc->idVendor == 0x04bb && deviceDesc->idProduct == 0x0f04) {
        OnEcoboxInsert(device);
        return;
    }

    if (deviceDesc->idVendor == 0x1774 && deviceDesc->idProduct == 0x1001) {
        OnUsbrhInsert(device);
        return;
    }

---



0 件のコメント: