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/

Committer:
strysd
Date:
Fri Mar 01 12:24:19 2013 +0000
Revision:
22:07b3f2643582
Parent:
20:44214878a602
Child:
24:06545ba01cf4
use struct to set argument of post( )

Who changed what in which revision?

UserRevisionLine numberNew contents of line
strysd 6:cccef292257b 1 #include "main.h"
yueee_yt 0:27cf9683af17 2 #include "mbed.h"
yueee_yt 0:27cf9683af17 3 #include "EthernetInterface.h"
yueee_yt 0:27cf9683af17 4 #include "NTPClient.h"
yueee_yt 0:27cf9683af17 5 #include "fiap.h"
yueee_yt 0:27cf9683af17 6
yueee_yt 0:27cf9683af17 7 EthernetInterface eth;
yueee_yt 0:27cf9683af17 8 time_t ctTime;
strysd 18:f277fbbcea4b 9 Serial pc(USBTX, USBRX);
strysd 18:f277fbbcea4b 10 FIAP fiap;
yueee_yt 0:27cf9683af17 11
strysd 17:868a61434584 12 //AnalogIn ain_p15(p15);
strysd 17:868a61434584 13 //AnalogIn ain_p16(p16);
strysd 17:868a61434584 14 //AnalogIn ain_p17(p17);
strysd 17:868a61434584 15 //AnalogIn ain_p18(p18);
strysd 13:4e51ac520b33 16 AnalogIn ain_p19(p19);
strysd 13:4e51ac520b33 17 AnalogIn ain_p20(p20);
strysd 4:f50c307c5eda 18
strysd 13:4e51ac520b33 19 DigitalOut led1(LED1);//error while connecting to NTP server or FIAPStorage
strysd 8:e482433c9593 20 DigitalOut led3(LED3);//LAN speed (1 = error)
strysd 8:e482433c9593 21 DigitalOut led4(LED4);//LAN link (1 = error)
strysd 4:f50c307c5eda 22
strysd 8:e482433c9593 23 DigitalIn lnk(P1_25);//LAN link
strysd 8:e482433c9593 24 DigitalIn spd(P1_26);//LAN speed
strysd 9:0d34968a5de1 25 DigitalOut speed(p29);//LAN speed for RJ45 (1 = OK)
strysd 9:0d34968a5de1 26 DigitalOut link(p30); //LAN link for RJ45 (1 = OK)
strysd 8:e482433c9593 27
strysd 8:e482433c9593 28 int exitTimer;
strysd 17:868a61434584 29 int i;
strysd 22:07b3f2643582 30 int this_year, this_mon;
strysd 13:4e51ac520b33 31
strysd 20:44214878a602 32 //I define longer than actual length.
strysd 20:44214878a602 33 //because last characters are chopped when I define just length, 64.
strysd 20:44214878a602 34 #define STORAGE_URL_SIZE 100
strysd 20:44214878a602 35 char storage_url[STORAGE_URL_SIZE];
strysd 18:f277fbbcea4b 36 struct fiap_element element[]= {
strysd 18:f277fbbcea4b 37 {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
strysd 18:f277fbbcea4b 38 };
strysd 18:f277fbbcea4b 39 struct tm t;
strysd 18:f277fbbcea4b 40
strysd 17:868a61434584 41 char val_p15[6];
strysd 17:868a61434584 42 char val_p16[6];
strysd 17:868a61434584 43 char val_p17[6];
strysd 17:868a61434584 44 char val_p18[6];
strysd 13:4e51ac520b33 45 char val_p19[6];
strysd 13:4e51ac520b33 46 char val_p20[6];
strysd 13:4e51ac520b33 47
strysd 17:868a61434584 48 float float_p15_wrk;
strysd 17:868a61434584 49 float float_p16_wrk;
strysd 17:868a61434584 50 float float_p17_wrk;
strysd 17:868a61434584 51 float float_p18_wrk;
strysd 17:868a61434584 52 float float_p19_wrk;
strysd 17:868a61434584 53 float float_p20_wrk;
strysd 17:868a61434584 54
strysd 17:868a61434584 55 float float_p15;
strysd 17:868a61434584 56 float float_p16;
strysd 17:868a61434584 57 float float_p17;
strysd 17:868a61434584 58 float float_p18;
strysd 13:4e51ac520b33 59 float float_p19;
strysd 13:4e51ac520b33 60 float float_p20;
strysd 13:4e51ac520b33 61
strysd 9:0d34968a5de1 62 //if you set LEDs for LAN indicator in real time.
strysd 9:0d34968a5de1 63 /*
strysd 9:0d34968a5de1 64 Ticker flipper;
strysd 9:0d34968a5de1 65
strysd 9:0d34968a5de1 66 void myflip() {
strysd 9:0d34968a5de1 67 led3 = spd;
strysd 9:0d34968a5de1 68 led4 = lnk;
strysd 17:868a61434584 69 if(USE_RJ45_LAMP == true){
strysd 17:868a61434584 70 speed = !spd;
strysd 17:868a61434584 71 link = !lnk;
strysd 17:868a61434584 72 }
strysd 9:0d34968a5de1 73 }
strysd 9:0d34968a5de1 74 */
strysd 9:0d34968a5de1 75
strysd 18:f277fbbcea4b 76 int syncNTP(void)
strysd 18:f277fbbcea4b 77 {
strysd 18:f277fbbcea4b 78 NTPClient ntp;
strysd 18:f277fbbcea4b 79 if (ntp.setTime(NTP_SERVER) != 0) {
strysd 18:f277fbbcea4b 80 return -1;
strysd 18:f277fbbcea4b 81 }
strysd 18:f277fbbcea4b 82 ctTime = time(NULL);
strysd 18:f277fbbcea4b 83 ctTime+= TIME_ZONE_SECONDS;
strysd 18:f277fbbcea4b 84 set_time(ctTime);
strysd 18:f277fbbcea4b 85 return 0;
strysd 18:f277fbbcea4b 86 }
strysd 18:f277fbbcea4b 87
strysd 18:f277fbbcea4b 88 int setFIAPStorage(void )
strysd 18:f277fbbcea4b 89 {
strysd 18:f277fbbcea4b 90 LocalFileSystem local("local");
strysd 18:f277fbbcea4b 91 FILE *fp;
strysd 18:f277fbbcea4b 92 fp = fopen("/local/storage.txt", "r");
strysd 18:f277fbbcea4b 93 if(fp == NULL){
strysd 18:f277fbbcea4b 94 return -1;
strysd 18:f277fbbcea4b 95 }
strysd 20:44214878a602 96 fgets(storage_url, STORAGE_URL_SIZE, fp);
strysd 18:f277fbbcea4b 97 fiap.setStorage(storage_url);
strysd 18:f277fbbcea4b 98 fclose(fp);
strysd 18:f277fbbcea4b 99
strysd 18:f277fbbcea4b 100 fiap.debug_mode=FIAP_DEBUG_MODE;
strysd 18:f277fbbcea4b 101 return 0;
strysd 18:f277fbbcea4b 102 }
strysd 18:f277fbbcea4b 103
strysd 13:4e51ac520b33 104 int sendFIAP(void )
yueee_yt 0:27cf9683af17 105 {
yueee_yt 0:27cf9683af17 106 ctTime = time(NULL);
strysd 4:f50c307c5eda 107 t = *localtime(&ctTime);
strysd 10:51ef659414a9 108
strysd 17:868a61434584 109 float_p15_wrk =
strysd 17:868a61434584 110 float_p16_wrk =
strysd 17:868a61434584 111 float_p17_wrk =
strysd 17:868a61434584 112 float_p18_wrk =
strysd 17:868a61434584 113 float_p19_wrk =
strysd 17:868a61434584 114 float_p20_wrk =
strysd 17:868a61434584 115 0.0;
strysd 17:868a61434584 116
strysd 22:07b3f2643582 117 for(i = 0; i < 4; i++){
strysd 22:07b3f2643582 118 // float_p15_wrk += ain_p15;
strysd 22:07b3f2643582 119 // float_p16_wrk += ain_p16;
strysd 22:07b3f2643582 120 // float_p17_wrk += ain_p17;
strysd 22:07b3f2643582 121 // float_p18_wrk += ain_p18;
strysd 22:07b3f2643582 122 float_p19_wrk += ain_p19;
strysd 22:07b3f2643582 123 float_p20_wrk += ain_p20;
strysd 22:07b3f2643582 124 }
strysd 17:868a61434584 125
strysd 22:07b3f2643582 126 // float_p15 = float_p15_wrk/4 * P15_COEFFICIENT + P15_CONSTANT;
strysd 22:07b3f2643582 127 // float_p16 = float_p16_wrk/4 * P16_COEFFICIENT + P16_CONSTANT;
strysd 22:07b3f2643582 128 // float_p17 = float_p17_wrk/4 * P17_COEFFICIENT + P17_CONSTANT;
strysd 22:07b3f2643582 129 // float_p18 = float_p18_wrk/4 * P18_COEFFICIENT + P18_CONSTANT;
strysd 22:07b3f2643582 130 float_p19 = float_p19_wrk/4 * P19_COEFFICIENT + P19_CONSTANT;
strysd 22:07b3f2643582 131 float_p20 = float_p20_wrk/4 * P20_COEFFICIENT + P20_CONSTANT;
strysd 17:868a61434584 132
strysd 17:868a61434584 133 // sprintf(val_p15,"%4.1f",float_p15);
strysd 17:868a61434584 134 // sprintf(val_p16,"%4.1f",float_p16);
strysd 17:868a61434584 135 // sprintf(val_p17,"%4.1f",float_p17);
strysd 17:868a61434584 136 // sprintf(val_p18,"%4.1f",float_p18);
strysd 13:4e51ac520b33 137 sprintf(val_p19,"%4.1f",float_p19);
strysd 13:4e51ac520b33 138 sprintf(val_p20,"%4.1f",float_p20);
strysd 13:4e51ac520b33 139
strysd 22:07b3f2643582 140 this_year = t.tm_year + 1900;
strysd 22:07b3f2643582 141 this_mon = t.tm_mon + 1;
strysd 22:07b3f2643582 142
strysd 22:07b3f2643582 143 struct fiap_element P19_element = {
strysd 22:07b3f2643582 144 P19_POINT_ID, val_p19, this_year, this_mon, t.tm_mday,
strysd 22:07b3f2643582 145 t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
strysd 17:868a61434584 146
strysd 22:07b3f2643582 147 struct fiap_element P20_element = {
strysd 22:07b3f2643582 148 P20_POINT_ID, val_p20, this_year, this_mon, t.tm_mday,
strysd 22:07b3f2643582 149 t.tm_hour, t.tm_min, t.tm_sec, TIME_ZONE_CHAR};
strysd 17:868a61434584 150
strysd 22:07b3f2643582 151 element[0] = P19_element;
strysd 22:07b3f2643582 152 element[1] = P20_element;
strysd 13:4e51ac520b33 153
strysd 13:4e51ac520b33 154 return fiap.post(element,COUNT_OF_POINTS);
yueee_yt 0:27cf9683af17 155 }
yueee_yt 0:27cf9683af17 156
strysd 17:868a61434584 157 /* stop USB Interface */
strysd 17:868a61434584 158 #define USR_POWERDOWN (0x104)
strysd 17:868a61434584 159 int semihost_powerdown() {
strysd 17:868a61434584 160 uint32_t arg;
strysd 17:868a61434584 161 return __semihost(USR_POWERDOWN, &arg);
strysd 17:868a61434584 162 }
strysd 17:868a61434584 163
strysd 20:44214878a602 164 /** Frequency Control
strysd 20:44214878a602 165 * @see ClockControl/ClockControl.cpp by Michael Wei
strysd 20:44214878a602 166 */
strysd 18:f277fbbcea4b 167 void setPLL0Frequency(unsigned char clkSrc, unsigned short M, unsigned char N)
strysd 18:f277fbbcea4b 168 {
strysd 18:f277fbbcea4b 169 LPC_SC->CLKSRCSEL = clkSrc;
strysd 18:f277fbbcea4b 170 LPC_SC->PLL0CFG = (((unsigned int)N-1) << 16) | M-1;
strysd 18:f277fbbcea4b 171 LPC_SC->PLL0CON = 0x01;
strysd 18:f277fbbcea4b 172 LPC_SC->PLL0FEED = 0xAA;
strysd 18:f277fbbcea4b 173 LPC_SC->PLL0FEED = 0x55;
strysd 18:f277fbbcea4b 174 while (!(LPC_SC->PLL0STAT & (1<<26)));
strysd 18:f277fbbcea4b 175
strysd 18:f277fbbcea4b 176 LPC_SC->PLL0CON = 0x03;
strysd 18:f277fbbcea4b 177 LPC_SC->PLL0FEED = 0xAA;
strysd 18:f277fbbcea4b 178 LPC_SC->PLL0FEED = 0x55;
strysd 18:f277fbbcea4b 179 }
strysd 18:f277fbbcea4b 180
strysd 18:f277fbbcea4b 181 unsigned int setSystemFrequency(unsigned char clkDivider, unsigned char clkSrc, unsigned short M, unsigned char N)
strysd 18:f277fbbcea4b 182 {
strysd 18:f277fbbcea4b 183 setPLL0Frequency(clkSrc, M, N);
strysd 18:f277fbbcea4b 184 LPC_SC->CCLKCFG = clkDivider - 1;
strysd 18:f277fbbcea4b 185 SystemCoreClockUpdate();
strysd 18:f277fbbcea4b 186 return SystemCoreClock;
strysd 18:f277fbbcea4b 187 }
strysd 18:f277fbbcea4b 188
yueee_yt 0:27cf9683af17 189 int main()
yueee_yt 0:27cf9683af17 190 {
strysd 20:44214878a602 191 setSystemFrequency(0x3, 0x1, 6, 1);//clock down from 96MHz to 48MHz
strysd 20:44214878a602 192 led1 = led3 = led4 = speed = link = 0;
strysd 20:44214878a602 193 pc.baud(USB_PAUD_RATE);
strysd 9:0d34968a5de1 194
strysd 9:0d34968a5de1 195 //if you set LEDs for LAN indicator in real time.
strysd 9:0d34968a5de1 196 //interval : 0.1 = 100ms
strysd 9:0d34968a5de1 197 //flipper.attach(&myflip, 0.1);
strysd 4:f50c307c5eda 198
yueee_yt 0:27cf9683af17 199 eth.init(); //Use DHCP
yueee_yt 0:27cf9683af17 200 eth.connect();
strysd 10:51ef659414a9 201 printf("IP: %s\r\n", eth.getIPAddress());
strysd 18:f277fbbcea4b 202
strysd 10:51ef659414a9 203 led1 = 1;
strysd 18:f277fbbcea4b 204 if(syncNTP() != 0) {
strysd 18:f277fbbcea4b 205 printf("NTP Sync Error\r\n");
strysd 18:f277fbbcea4b 206 return -1;
strysd 18:f277fbbcea4b 207 }
strysd 18:f277fbbcea4b 208 ctTime = time(NULL);
strysd 18:f277fbbcea4b 209 printf("Time: %s\r\n", ctime(&ctTime));
strysd 18:f277fbbcea4b 210
strysd 18:f277fbbcea4b 211 if(setFIAPStorage() != 0) {
strysd 18:f277fbbcea4b 212 printf("Check if storage.txt exists in mbed memory.\r\n");
yueee_yt 0:27cf9683af17 213 return -1;
yueee_yt 0:27cf9683af17 214 }
strysd 10:51ef659414a9 215 led1 = 0;
strysd 18:f277fbbcea4b 216
strysd 17:868a61434584 217 if(FIAP_DEBUG_MODE == false){
strysd 18:f277fbbcea4b 218 semihost_powerdown();//Stop also USB Interface
strysd 17:868a61434584 219 }
strysd 13:4e51ac520b33 220
strysd 13:4e51ac520b33 221 exitTimer = RETRY_TIMES_ON_ERROR;
strysd 8:e482433c9593 222 while(exitTimer > 0) {
strysd 8:e482433c9593 223 led3 = spd;
strysd 8:e482433c9593 224 led4 = lnk;
strysd 17:868a61434584 225 if(USE_RJ45_LAMP == true){
strysd 17:868a61434584 226 speed = !spd;
strysd 17:868a61434584 227 link = !lnk;
strysd 17:868a61434584 228 }
strysd 8:e482433c9593 229 if (led4 == 1 || led3 == 1) {
strysd 8:e482433c9593 230 exitTimer--;
strysd 13:4e51ac520b33 231 wait(WAIT_NEXT_SEND);
strysd 13:4e51ac520b33 232 continue;
strysd 13:4e51ac520b33 233 }
strysd 13:4e51ac520b33 234
strysd 18:f277fbbcea4b 235 if(sendFIAP() != 0){
strysd 13:4e51ac520b33 236 led1 = 1;
strysd 13:4e51ac520b33 237 exitTimer--;
strysd 8:e482433c9593 238 } else {
strysd 13:4e51ac520b33 239 led1 = 0;
strysd 13:4e51ac520b33 240 exitTimer = RETRY_TIMES_ON_ERROR;//initialize
strysd 8:e482433c9593 241 }
strysd 13:4e51ac520b33 242 wait(WAIT_NEXT_SEND);
yueee_yt 0:27cf9683af17 243 }
strysd 8:e482433c9593 244 eth.disconnect();
strysd 17:868a61434584 245 if(FIAP_DEBUG_MODE == true){
strysd 17:868a61434584 246 printf("Disconnected because of error\r\n");
strysd 17:868a61434584 247 }
strysd 13:4e51ac520b33 248
strysd 8:e482433c9593 249 return -1;
strysd 5:720a29128f41 250 }