IEEE1888 GateWay sample that handles multiple sensors.

Dependencies:   EthernetInterface Fiap HTTPClientForFIAP NTPClient mbed-rtos mbed spxml

Fork of IEEE1888_TEMP_NODE by Satoru Yoshida

This program sends data to FIAP storage with IEEE1888 format.

Note: You should locate storage.txt into mbed memory at least before using this program. The storage.txt contains storage url, see example at main.h

You can launch an AMI ( Amazon Machine Image ) on Amazon Web Service ( http://aws.amazon.com/ ) as the FIAP storage. The AMI is named as "IEEE1888 FiapStorage2 Sample", registered at Tokyo region (ap-northeast-1) .

If You need more information about the AMI, see http://d.hatena.ne.jp/satoruyoshida/20130611/1370961807 .

/media/uploads/strysd/fiap_ami.jpg

This image shows sample data of IEEE1888 format.

Note: this image is shown if FIAP_DEBUG_MODE is set to true.

/media/uploads/strysd/fiap_protocol.jpg

You can define COUNT_OF_POINTS constant as number of analog sensors and digital inputs. In this program, I use P19 and P20 as analog inputs from 2 sensors. So I define the COUNT_OF_POINTS as 2.

/media/uploads/strysd/fiap_gw.jpg

P19 is connected to illuminance sensor, S9648 (HAMAMATSU PHOTONICS K.K.). And P20 is connected to temperature sensor, LM35 (National Semiconductor Corporation) .

You can also use another analog sensors and digital inputs, for example LM61BIZ instead of LM35.

/media/uploads/strysd/server_room.png

/media/uploads/strysd/mymbed_multi_censor_initial.png

from here, in Japanese. このプログラムは IEEE1888 形式で FIAP Storage にデータ送信する例です。 このプログラムを使用する前に、少なくとも FIAP Storage の url を記載した sotrage.txt をメモリに格納しておく必要があります。 url の例は、 main.h ファイルにあります。

Amazon Web Service に用意した AMI を利用して FIAP Storage を起動することができます。IEEE1888 FiapStorage2 Sample という名前の公開 AMI です。

この AMI について詳しくは、http://d.hatena.ne.jp/satoruyoshida/20130611/1370961807 をご覧ください。

なお、main.h にはほかにも定数があります。例えば、IEEE1888 (FIAP) の通信内容をコンソールで確認したい場合は、FIAP_DEBUG_MODE を true にします。 また、アナログ・センサーやデジタル入力の個数に沿って、COUNT_OF_POINTS を定義します。

このプログラム例では、P19 および P20 をセンサーからのアナログ入力として使用しています。そのため、COUNT_OF_POINTS を 2 と定義しています。 P19 には浜松ホトニクス社製の照度センサー S9648、P20 にはナショナル・セミコンダクターの温度センサー LM35 を接続しています。 参考までに LM35 をより安価な LM61BIZ に置き換えた場合の設定値もコメントアウト状態で入れてあります

(ここから日本語文のみの内容) トランジスタ技術 2012年 2月号 の P189 - P195 に、Arduino、Ethernet シールド、温度センサー(LM35)、照度センサー(S9648-100)、DIP スイッチおよびトグルスイッチを用いたネットワーク温度&照度計の記事が掲載されています(落合 秀也 様/井上 博之 様)。 このプログラムでは、照度センサーは S9648-100 ではなく、より安価な S9648 で代用し、スイッチ 2 種類は省略していますのでご注意ください。 S9648 の負荷抵抗 1KΩ に並列に入れたコンデンサーは、手元に余っていた 0.1μF です。この容量が妥当かどうかは未検証です。

LM35 の出力をアンプで6倍に増幅してみたときのメモがここにあります。

https://mbed.org/users/strysd/notebook/memo_analog_input_from_lm35dz_via_nju7032d/

照度センサーを S9648-100 に変えて、抵抗値も検討したときのメモはこちら。

https://mbed.org/users/strysd/notebook/difference_s9648_and_s9648_100/

main.cpp

Committer:
strysd
Date:
2013-05-06
Revision:
26:54b10f53bf41
Parent:
25:929a72bcd8d2

File content as of revision 26:54b10f53bf41:

#include "main.h"
#include "mbed.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#include "fiap.h"

EthernetInterface eth;
time_t ctTime;
Serial pc(USBTX, USBRX);
FIAP fiap;

#ifdef P15_POINT_ID
AnalogIn ain_p15(p15);
#endif
#ifdef P16_POINT_ID
AnalogIn ain_p16(p16);
#endif
#ifdef P17_POINT_ID
AnalogIn ain_p17(p17);
#endif
#ifdef P18_POINT_ID
AnalogIn ain_p18(p18);
#endif
#ifdef P19_POINT_ID
AnalogIn ain_p19(p19);
#endif
#ifdef P20_POINT_ID
AnalogIn ain_p20(p20);
#endif

DigitalOut led1(LED1);//error while connecting to NTP server or FIAPStorage
DigitalOut led3(LED3);//LAN speed (1 = error)
DigitalOut led4(LED4);//LAN link (1 = error)

DigitalIn lnk(P1_25);//LAN link
DigitalIn spd(P1_26);//LAN speed

#if USE_RJ45_LAMP
DigitalOut speed(p29);//LAN speed for RJ45 (1 = OK)
DigitalOut link(p30); //LAN link for RJ45 (1 = OK)
#endif

int exitTimer;
int i;
int this_year, this_mon;

//I define longer than actual length.
//because last characters are chopped when I define just length, 64.
#define STORAGE_URL_SIZE 100
char storage_url[STORAGE_URL_SIZE];
struct fiap_element element[]= {
    {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
};
struct tm t;

#ifdef P15_POINT_ID
char val_p15[8];
float float_p15_wrk;
float float_p15;
#endif
#ifdef P16_POINT_ID
char val_p16[8];
float float_p16_wrk;
float float_p16;
#endif
#ifdef P17_POINT_ID
char val_p17[8];
float float_p17_wrk;
float float_p17;
#endif
#ifdef P18_POINT_ID
char val_p18[8];
float float_p18_wrk;
float float_p18;
#endif
#ifdef P19_POINT_ID
char val_p19[8];
float float_p19_wrk;
float float_p19;
#endif
#ifdef P20_POINT_ID
char val_p20[8];
float float_p20_wrk;
float float_p20;
#endif

int syncNTP(void)
{
    NTPClient ntp;
    if (ntp.setTime(NTP_SERVER) != 0) {
        return -1;
    }
    ctTime = time(NULL);
    ctTime+= TIME_ZONE_SECONDS;
    set_time(ctTime);
    return 0;
}

int setFIAPStorage(void )
{
    LocalFileSystem local("local");
    FILE *fp;
    fp = fopen("/local/storage.txt", "r");
    if(fp == NULL){
        return -1;
    }
    fgets(storage_url, STORAGE_URL_SIZE, fp);
    fiap.setStorage(storage_url);
    fclose(fp);
    
    fiap.debug_mode=FIAP_DEBUG_MODE;
    return 0;
}

int sendFIAP(void )
{
    ctTime = time(NULL);
    t = *localtime(&ctTime);

    #ifdef P15_POINT_ID
    float_p15_wrk  = ain_p15;//dummy read
    float_p15_wrk  = ain_p15;
    float_p15_wrk += ain_p15;
    float_p15 = float_p15_wrk/2 * P15_COEFFICIENT + P15_CONSTANT;
    #endif
    #ifdef P16_POINT_ID
    float_p16_wrk  = ain_p16;//dummy read
    float_p16_wrk  = ain_p16;
    float_p16_wrk += ain_p16;
    float_p16 = float_p16_wrk/2 * P16_COEFFICIENT + P16_CONSTANT;
    #endif
    #ifdef P17_POINT_ID
    float_p17_wrk  = ain_p17;//dummy read
    float_p17_wrk  = ain_p17;
    float_p17_wrk += ain_p17;
    float_p17 = float_p17_wrk/2 * P17_COEFFICIENT + P17_CONSTANT;
    #endif
    #ifdef P18_POINT_ID
    float_p18_wrk  = ain_p18;//dummy read
    float_p18_wrk  = ain_p18;
    float_p18_wrk += ain_p18;
    float_p18 = float_p18_wrk/2 * P18_COEFFICIENT + P18_CONSTANT;
    #endif
    #ifdef P19_POINT_ID
    float_p19_wrk  = ain_p19;//dummy read
    float_p19_wrk  = ain_p19;
    float_p19_wrk += ain_p19;
    float_p19 = float_p19_wrk/2 * P19_COEFFICIENT + P19_CONSTANT;
    #endif
    #ifdef P20_POINT_ID
    float_p20_wrk  = ain_p20;//dummy read
    float_p20_wrk  = ain_p20;
    float_p20_wrk += ain_p20;
    float_p20 = float_p20_wrk/2 * P20_COEFFICIENT + P20_CONSTANT;
    #endif

    #ifdef P15_POINT_ID
    sprintf(val_p15, P15_PREC, float_p15);
    #endif
    #ifdef P16_POINT_ID
    sprintf(val_p16, P16_PREC, float_p16);
    #endif
    #ifdef P17_POINT_ID
    sprintf(val_p17, P17_PREC, float_p17);
    #endif
    #ifdef P18_POINT_ID
    sprintf(val_p18, P18_PREC, float_p18);
    #endif
    #ifdef P19_POINT_ID
    sprintf(val_p19, P19_PREC, float_p19);
    #endif
    #ifdef P20_POINT_ID
    sprintf(val_p20, P20_PREC, float_p20);
    #endif

    this_year = t.tm_year + 1900;
    this_mon  = t.tm_mon  + 1;

    #ifdef P15_POINT_ID
    struct fiap_element P15_element = {
        P15_POINT_ID, val_p15, this_year, this_mon, t.tm_mday,
           t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
    #endif
    #ifdef P16_POINT_ID
    struct fiap_element P16_element = {
        P16_POINT_ID, val_p16, this_year, this_mon, t.tm_mday,
           t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
    #endif
    #ifdef P17_POINT_ID
    struct fiap_element P17_element = {
        P17_POINT_ID, val_p17, this_year, this_mon, t.tm_mday,
           t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
    #endif
    #ifdef P18_POINT_ID
    struct fiap_element P18_element = {
        P18_POINT_ID, val_p18, this_year, this_mon, t.tm_mday,
           t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
    #endif
    #ifdef P19_POINT_ID
    struct fiap_element P19_element = {
        P19_POINT_ID, val_p19, this_year, this_mon, t.tm_mday,
           t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
    #endif
    #ifdef P20_POINT_ID
    struct fiap_element P20_element = {
        P20_POINT_ID, val_p20, this_year, this_mon, t.tm_mday,
           t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
    #endif

    i = 0;
    #ifdef P15_POINT_ID
    element[i++] = P15_element;
    #endif
    #ifdef P16_POINT_ID
    element[i++] = P16_element;
    #endif
    #ifdef P17_POINT_ID
    element[i++] = P17_element;
    #endif
    #ifdef P18_POINT_ID
    element[i++] = P18_element;
    #endif
    #ifdef P19_POINT_ID
    element[i++] = P19_element;
    #endif
    #ifdef P20_POINT_ID
    element[i++] = P20_element;
    #endif
    return fiap.post(element, i);
}

#if FIAP_DEBUG_MODE
#else
/* stop USB Interface */
#define USR_POWERDOWN (0x104)
int semihost_powerdown() {
    uint32_t arg;
    return __semihost(USR_POWERDOWN, &arg);
}
#endif

/** Frequency Control
 * @see ClockControl/ClockControl.cpp by Michael Wei
 */
void setPLL0Frequency(unsigned char clkSrc, unsigned short M, unsigned char N)
{
      LPC_SC->CLKSRCSEL = clkSrc;
      LPC_SC->PLL0CFG   = (((unsigned int)N-1) << 16) | M-1;
      LPC_SC->PLL0CON   = 0x01;             
      LPC_SC->PLL0FEED  = 0xAA;
      LPC_SC->PLL0FEED  = 0x55;
      while (!(LPC_SC->PLL0STAT & (1<<26)));
    
      LPC_SC->PLL0CON   = 0x03;
      LPC_SC->PLL0FEED  = 0xAA;
      LPC_SC->PLL0FEED  = 0x55;
}

unsigned int setSystemFrequency(unsigned char clkDivider, unsigned char clkSrc, unsigned short M, unsigned char N)
{
    setPLL0Frequency(clkSrc, M, N);
    LPC_SC->CCLKCFG = clkDivider - 1;
    SystemCoreClockUpdate();
    return SystemCoreClock;
}

int main()
{
    setSystemFrequency(0x3, 0x1, 6, 1);//clock down from 96MHz to 48MHz
    led1 = led3 = led4 = 0;

    #if USE_RJ45_LAMP
    speed = link = 0;
    #endif

    pc.baud(USB_PAUD_RATE);

    eth.init(); //Use DHCP
    eth.connect();
    printf("IP: %s\r\n", eth.getIPAddress());

    led1 = 1;
    if(syncNTP() != 0) {
        printf("NTP Sync Error\r\n");
        return -1;
    }
    ctTime = time(NULL);
    printf("Time: %s\r\n", ctime(&ctTime));
    
    if(setFIAPStorage() != 0) {
        printf("Check if storage.txt exists in mbed memory.\r\n");
        return -1;
    }
    led1 = 0;

    #if FIAP_DEBUG_MODE
    #else
        semihost_powerdown();//Stop also USB Interface
    #endif

    exitTimer = RETRY_TIMES_ON_ERROR;
    while(exitTimer > 0) {
        led3 = spd;
        led4 = lnk;

        #if USE_RJ45_LAMP
        speed = !spd;
        link = !lnk;
        #endif
        
        if (led4 == 1 || led3 == 1) {
            exitTimer--;
            wait(WAIT_NEXT_SEND);
            continue;
        }
        
        if(sendFIAP() != 0){
            led1 = 1;
            exitTimer--;
        } else {
            led1 = 0;
            exitTimer = RETRY_TIMES_ON_ERROR;//initialize
        }
        wait(WAIT_NEXT_SEND);
    }
    eth.disconnect();
    #if FIAP_DEBUG_MODE
    printf("Disconnected because of error\r\n");
    #endif

    return -1;
}