ThingPlug GMMP library Ethernet Example on Nucleo and W5500

Dependencies:   GMMP_mbed NTPClient WIZnet_Library mbed

Fork of ThingPlug_Ethernet_Example by Sangmin Lee

Files at this revision

API Documentation at this revision

Comitter:
lesmin
Date:
Sun Aug 09 14:24:15 2015 +0000
Child:
1:468ac9167975
Commit message:
ThingPlug_WiFi_Example_2

Changed in this revision

GMMP_2.lib Show annotated file Show diff for this revision Revisions of this file
NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
SNICInterface.lib Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GMMP_2.lib	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/lesmin/code/GMMP_2/#7e575e5f88ec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/donatien/code/NTPClient/#881559865a93
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SNICInterface.lib	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/murata/code/SNICInterface/#b53ccb9989c4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,51 @@
+#ifndef CONFIG_H_
+#define CONFIG_H_
+
+/*
+ *  Communication Module configuration starts here
+ */
+#define USE_SNIC_WIFI //Murata Type YD Wi-Fi
+//#define USE_WIZNET_W5500
+
+
+/*
+ *  SNIC configuration starts here
+ */
+#ifdef USE_SNIC_WIFI
+
+    #define MBED_AP_SSID                  "FON"//"TIDE867"//"tide855" 
+    /** Securiry Options
+    e_SEC_OPEN       = 0x00, //Open
+    e_SEC_WEP        = 0x01, // WEP
+    e_SEC_WPA_TKIP   = 0x02, // WPA-PSK(TKIP)
+    e_SEC_WPA2_AES   = 0x04, // WPA2-PSK(AES)
+    e_SEC_WPA2_MIXED = 0x06, // WPA2-PSK(TKIP/AES)
+    e_SEC_WPA_AES    = 0x07  // WPA-PSK(AES) **/
+    #define MBED_AP_SECURITY_TYPE         e_SEC_OPEN//e_SEC_WPA2_AES//e_SEC_OPEN
+    #define MBED_AP_SECUTIRY_KEY          ""//"tidetide"
+    //unsigned char g_mac[] = {0x98, 0xF1, 0x70, 0x67, 0x4D, 0xC3}; //MURATA WIFI
+
+#endif//USE_SNIC_WIFI
+
+// Some Ethernet Shields (ex.W5500) do NOT have MAC address. use your cell phone number instead.
+// 010-1234-5678 -> {0x00, 0x10, 0x12, 0x34, 0x56, 0x78};
+#define DEFAULT_MAC_ADDR {0x00, 0x10, 0x12, 0x34, 0x56, 0x78}   //MAC Address
+
+/*
+ *  GMMP configuration starts here
+ */
+#define THINGPLUG_GMMP_IP_ADDR      {61, 250, 21, 211}  //ThingPlug GMMP 서버 IP주소
+#define THINGPLUG_GMMP_PORT_NUM     31002               //마이페이지 > 서비스 정보 수정 > 서비스 기본 정보 > TCP Listen Port
+#define THINGPLUG_GMMP_SERVICE_NAME "ThingPlug"         //마이페이지 > 서비스 정보 수정 > 서비스 기본 정보 > 서비스 아이디
+//unsigned char serverIp[] = {61, 250, 21, 211};
+//const int nServerPort = 31002;
+//const char* pszDomainCode = "ThingPlug";
+
+/*
+ *  Gateway/Device configuration starts here
+ */
+#define THINGPLUG_GMMP_DEVICE_SERIAL_NUM            "01234567890"//"0002f7f1ce5a"
+#define THINGPLUG_GMMP_DEVICE_MANUFACTURER_ID       "STMicro"//"DEV_lesmin"
+#define THINGPLUG_GMMP_SUB_DEVICE_MANUFACTURER_ID   "STM32"
+
+#endif /* CONFIG_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,481 @@
+#include "mbed.h"
+
+#if defined(TARGET_LPC1768)
+#include "C12832.h" //for LCD
+#include "LM75B.h" //for Temperature Sensor
+#elif defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_L152RE)
+//Sensor N/A for Nucleo
+#endif
+
+DigitalOut myled(LED1);
+
+#if defined(TARGET_LPC1768)
+C12832 lcd(p5, p7, p6, p8, p11);
+LM75B sensor(p28,p27);
+#endif
+
+Serial pc(USBTX, USBRX);
+
+#include "config.h"
+
+/**
+ * GMMP Test Code
+ */
+
+#include "GMMP.h"
+
+#define USER_CONTROL_LED 0x80
+
+#define HIGH 0x1
+#define LOW  0x0
+
+byte serverIp[] = THINGPLUG_GMMP_IP_ADDR;
+const int nServerPort = THINGPLUG_GMMP_PORT_NUM;
+const char* pszDomainCode = THINGPLUG_GMMP_SERVICE_NAME;
+const char* pszGWAuthID = THINGPLUG_GMMP_DEVICE_SERIAL_NUM;
+const char* pszGWMFID = THINGPLUG_GMMP_DEVICE_MANUFACTURER_ID;
+const char* pszDeviceMFID = THINGPLUG_GMMP_SUB_DEVICE_MANUFACTURER_ID;
+
+char* deviceId[LEN_DEVICE_ID];
+unsigned char g_mac[6] = DEFAULT_MAC_ADDR;
+
+
+uint32_t lastDeliverMilli = 0;
+uint32_t reportPeriodMilli = 3000;
+uint32_t lastHeartbeatMilli = 0;
+uint32_t heartbeatPeriodMilli = 10000;
+byte regDevCount = 0;
+byte regGwCount = 0;
+
+void startSensingTemp();
+void delay(int d);
+
+int Recv(GMMPHeader* pstGMMPHeader, void* pBody)
+{
+  U8 cMessageType = pstGMMPHeader->ucMessageType;
+
+  //info(F("MsgType: "));
+  //infoln(cMessageType);
+  INFO("MsgType: %d", cMessageType);
+
+  if (cMessageType == OPERATION_GW_REG_RSP) {
+    stGwRegistrationRspHdr* pstRspHdr = (stGwRegistrationRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+      //error(F("GWRegRes Error: "));
+      //errorln(pstRspHdr->ucResultCode);
+      ERR("GWRegRes Error: %d", pstRspHdr->ucResultCode);
+      free(pBody);
+      return 1;
+    }
+
+    SetAuthKey((char*) pstGMMPHeader->usAuthKey);
+    SetGWID((char*) pstRspHdr->usGWID);
+    infoln(("GWRegRes has been received:"));
+    infoln(g_szAuthKey);
+    infoln(GetGWID());
+    INFO("GWRegRes has been received:\r\n%s\r\n%s", g_szAuthKey, GetGWID());
+    regGwCount++;
+
+    int ret = GO_Profile(GetGWID(), NULL, 0);
+
+    if (ret != GMMP_SUCCESS) {
+      errorln("ReqProfile Err!!");
+      free(pBody);
+      return 1;
+    }
+
+    infoln("ProfileReq has been sent.");
+  } else if (cMessageType == OPERATION_PROFILE_RSP) {
+    stProfileRspHdr* pstRspHdr = (stProfileRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+      errorln("ProfileRes Error!");
+      free(pBody);
+      return 1;
+    }
+
+    //infoln(F("ProfileRes has been received:"));
+    //infoln(Char2int(pstRspHdr->unHeartbeatPeriod, 4));
+    //infoln(Char2int(pstRspHdr->unReportOffset, 4));
+    //infoln(Char2int(pstRspHdr->unReportPeriod, 4));
+    //infoln(Char2int(pstRspHdr->unResponseTimeout, 4));
+    INFO("ProfileRes has been received:\r\n%d\r\n%d\r\n%d\r\n%d",
+    Char2int(pstRspHdr->unHeartbeatPeriod, 4),
+    Char2int(pstRspHdr->unReportOffset, 4),
+    Char2int(pstRspHdr->unReportPeriod, 4),
+    Char2int(pstRspHdr->unResponseTimeout,4) );
+    /* 주기값 재설정
+       pstRspHdr->unHeartbeatPeriod;
+       pstRspHdr->unReportOffset;
+       pstRspHdr->unReportPeriod;
+       pstRspHdr->unResponseTimeout;
+     */
+
+    reportPeriodMilli = Char2int(pstRspHdr->unReportPeriod, 4) * 1000 * 60;
+    //reportPeriodMilli = Char2int(pstRspHdr->unReportPeriod, 4) * 300;
+    heartbeatPeriodMilli = Char2int(pstRspHdr->unHeartbeatPeriod, 4) * 1000 * 60;
+
+    long nHeartBeat = Char2int((char*) pstRspHdr->unHeartbeatPeriod, sizeof(pstGMMPHeader->usTID)); //수신받은 Heartbeat 주기 값을 확인.
+
+    //Profile 요청 수신 값에 Heartbeat 주기 값으로 변경
+    if (nHeartBeat > 0) {
+      //nTimerSec = nHeartBeat;
+    }
+
+    int ret = GO_Reg(GetGWID(), pszDeviceMFID);
+
+    if (ret != GMMP_SUCCESS) {
+      errorln("DevRegReq Err!!");
+      free(pBody);
+      return 1;
+    }
+
+    infoln("DevRegReq has been sent.");
+  } else if (cMessageType == OPERATION_DEVICE_REG_RSP) {
+    stDeviceRegistrationRspHdr* pstRspHdr = (stDeviceRegistrationRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+      errorln("DevRegRes Error!");
+      free(pBody);
+      return 1;
+    }
+
+    if (strlen((char*) pstRspHdr->usDeviceID) <= 0) {
+      errorln("No DeviceID!");
+      free(pBody);
+      return 1;
+    }
+
+    int len = strlen((char*) pstRspHdr->usDeviceID);
+    memset(deviceId, 0, LEN_DEVICE_ID);
+    memcpy(deviceId, pstRspHdr->usDeviceID, len);
+    infoln("DevRegRes has been received:");
+    infoln((char*) deviceId);
+    regDevCount++;
+
+  } else if (cMessageType == OPERATION_DELIVERY_RSP) {
+    stPacketDeliveryRspHdr* pstRspHdr = (stPacketDeliveryRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+//      errorln("DeliverRes Err!!");
+      ERR("DeliverRes Err(0x%2x)!!", pstRspHdr->ucResultCode); //lesmin
+      free(pBody);
+      return 1;
+    }
+    
+    infoln("DeliveryRes has been received.");
+  } else if (pstGMMPHeader->ucMessageType == OPERATION_HEARTBEAT_RSP) {
+    //stHeartBeatMsgRspHdr* pstRspHdr = (stHeartBeatMsgRspHdr*) pBody;
+    
+    infoln("HeartbeatRes has been received.");
+  } else if (cMessageType  == OPERATION_CONTROL_REQ) {
+    stControlReqHdr* pstReqHdr = (stControlReqHdr*) pBody;
+
+    infoln("ControlReq has been received.");
+
+    char cResult = 0x00;
+
+    int len = 0;
+    if (pstReqHdr->usMessageBody) {
+      len = strlen((char*) pstReqHdr->usMessageBody);
+    }
+    if (len > 0) {
+      info("Control Msg Body: ");
+      infoln((char*) pstReqHdr->usMessageBody);
+    }
+
+    long nTID = Char2int((char*) pstGMMPHeader->usTID, sizeof(pstGMMPHeader->usTID));
+
+    //debug(F("Received TID: "));
+    //debugln(nTID);
+    DBG("Received TID: %d\r\n", nTID);
+
+    int ret = GO_Control((char*) pstReqHdr->usGWID, (char*) pstReqHdr->usDeviceID, nTID,
+     (char)pstReqHdr->ucControlType, cResult);
+
+    if (ret != GMMP_SUCCESS) {
+      errorln("ControRes Err!!");
+      free(pBody);
+      return 1;
+    }
+
+    infoln("ControlRes has been sent.");
+
+    //infoln(pstReqHdr->ucControlType);
+    INFO("%d\r\n", pstReqHdr->ucControlType);
+
+    if (pstReqHdr->ucControlType != USER_CONTROL_LED) {
+      ERR("Unknown Control Msg: %d\r\n", pstReqHdr->ucControlType);
+      free(pBody);
+      return 1;
+    }
+
+    if (pstReqHdr->usMessageBody[0] == '1') {
+      myled = 1;
+      infoln("LED ON");
+    } else if (pstReqHdr->usMessageBody[0] == '0') {
+      myled = 0;
+      infoln("LED OFF");
+    } else {
+      errorln("Unknown Control Msg Body; it must be '1' or '0'.");
+      free(pBody);
+      return 1;
+    }
+
+    delay(1000); //msec
+
+    ret = GO_Notifi((char*) pstReqHdr->usGWID, (char*) pstReqHdr->usDeviceID,
+        (char)pstReqHdr->ucControlType, cResult, (char*) pstReqHdr->usMessageBody, 0);
+
+    if (ret != GMMP_SUCCESS) {
+      ERR("NotiReq Err: %d\r\n", ret);
+      free(pBody);
+      return 1;
+    }
+
+    infoln("Control NotiReq has been sent.");
+  } else if (cMessageType  == OPERATION_NOTIFICATION_RSP) {
+    stNotificationRspHdr* pstRspHdr = (stNotificationRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+      //error(F("NotiRes Err: "));
+      //errorln(pstRspHdr->ucResultCode);
+      ERR("NotiRes Err: %d\r\n", pstRspHdr->ucResultCode);
+      free(pBody);
+      return 1;
+    }
+
+    infoln("NotiRes has been received.");
+  } else if (cMessageType == OPERATION_GW_DEREG_RSP) {
+    stGwRegistrationRspHdr* pstRspHdr = (stGwRegistrationRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+      //error(F("DeregGwRes Err: "));
+      //errorln(pstRspHdr->ucResultCode);
+      ERR("DeregGwRes Err: %d\r\n", pstRspHdr->ucResultCode);
+      free(pBody);
+      return 1;
+    }
+
+    if (strlen((char*) pstRspHdr->usGWID) <= 0) {
+      errorln("No GW ID!!");
+      free(pBody);
+      return 1;
+    }
+
+    if (strlen((char*) pstGMMPHeader->usAuthKey) <= 0) {
+      errorln("No Auth Key!!");
+      free(pBody);
+      return 1;
+    }
+
+    infoln("DeregGwRes has been received.");
+  } else if (cMessageType == OPERATION_DEVICE_DEREG_RSP) {
+    stDeviceRegistrationRspHdr* pstRspHdr = (stDeviceRegistrationRspHdr*) pBody;
+
+    if (pstRspHdr->ucResultCode != 0x00) {
+      //error(F("DeregDevRes Err: "));
+      //errorln(pstRspHdr->ucResultCode);
+      ERR("DeregDevRes Err: %d\r\n", pstRspHdr->ucResultCode);
+      free(pBody);
+      return 1;
+    }
+
+    if (strlen((char*) pstRspHdr->usDeviceID) <= 0) {
+      errorln("No device ID!!");
+      free(pBody);
+      return 1;
+    }
+
+    infoln("DeregDevRes has been received.");
+  }
+
+  free(pBody);
+  return 0;
+}
+
+byte isSensing = 0;
+
+void startSensingTemp()
+{
+#if defined(TARGET_LPC1768)
+    if (sensor.open()) {
+        printf("Sensing started!\r\n");
+        isSensing = 1;
+    }
+    else
+#endif 
+	{
+        isSensing = 1; //emulate sensor
+    }
+}
+
+#define BUF_SIZE 10
+GMMPHeader header;
+void *pBody = NULL;
+char sendBuf[BUF_SIZE];
+
+Timer g_t; //for Millis()
+
+int setup(void) {
+
+   	pc.baud(9600);
+
+    g_t.start();
+
+  infoln("Start setup()");
+
+    startSensingTemp(); //lesmin
+
+#if defined(TARGET_LPC1768)
+	char mac_addr[19];
+	mbed_mac_address((char *)mac); //Use mbed mac addres
+	snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+	printf("MAC: %s\n", mac_addr);
+#endif
+  if ( Initialize(serverIp, nServerPort, pszDomainCode, pszGWAuthID, g_mac) ) {
+      infoln("Initialize failed!");
+      return -1; //lesmin
+  }
+
+  infoln("SetCallFunction");
+  SetCallFunction(Recv);
+  
+  int ret = GO_Reg(NULL, pszGWMFID);
+
+  if (ret != GMMP_SUCCESS) {
+    errorln("RegReq Error!!");
+    for(;;);
+  }
+
+  infoln("GW RegReq has been sent.");
+  return 0;
+}
+
+int millis(void) {
+    return g_t.read_ms();
+}
+
+float getTemperature(void) {
+#if defined(TARGET_LPC1768)
+    float Temp = sensor;
+
+    lcd.cls();
+    lcd.locate(0,3);
+    lcd.printf("Temp = %.3f\n", (float)Temp);
+
+    wait(1.0);
+    return Temp;
+#else
+	return 33.3;
+#endif
+}
+
+void delay(int d) {
+    float delay_time = d/1000.0;
+    wait(delay_time);
+}
+
+void loop(void)
+{
+  int ret = GetReadData(&header, &pBody);
+  if (ret != E_WOULDBLOCK) {
+    //infoln("******** NOT E_WOULDBLOCK *********");
+    //info(F("Free memory size: "));
+    //infoln(freeRam());
+  }
+
+  //byte addr[8];
+  float temperature;
+
+  int curMilli = millis();
+  byte mustDeliver = (curMilli - lastDeliverMilli) > reportPeriodMilli;
+  byte mustHeartbeat = (curMilli - lastHeartbeatMilli) > heartbeatPeriodMilli;
+ 
+  if (!isSensing) {
+    printf("Sensor is off...\r\n");
+    return;
+  }
+
+#if 1
+  //int mustGwDereg = digitalRead(DEREGISTRATION_GW_SWITCH_PIN);
+  int mustGwDereg = LOW; //lesmin
+
+  if (mustGwDereg == HIGH && regGwCount > 0) {
+    ret = GO_DeReg(GetGWID(), NULL);
+
+    if (ret != GMMP_SUCCESS) {
+      errorln("DeregGwReq Error!!");
+      delay(1000);
+      return;
+    }
+
+    regGwCount--;
+    return;
+  }
+
+  //int mustDevDereg = digitalRead(DEREGISTRATION_DEVICE_SWITCH_PIN);
+  int mustDevDereg = LOW;
+
+  if (mustDevDereg == HIGH && regDevCount > 0) {
+    ret = GO_DeReg(GetGWID(), (char*) deviceId);
+
+    if (ret != GMMP_SUCCESS) {
+      errorln("DeregDevReq Error!!");
+      delay(1000);
+      return;
+    }
+
+    regDevCount--;
+    return;
+  }
+
+  if (mustHeartbeat) {
+    DBG("lastHeartbeatMilli: %d\r\ncurMilli: %d\r\n", lastHeartbeatMilli, curMilli);
+
+    ret = GO_HB(GetGWID());
+
+    if (ret != GMMP_SUCCESS) {
+      errorln("HeartbeatReq Error!!");
+      return;
+    }
+
+    infoln("HeartbeatReq has been sent.");
+
+    lastHeartbeatMilli = curMilli;
+  }
+
+  if (!mustDeliver) {
+    return;
+  }
+#endif
+
+  temperature = getTemperature();
+
+  memset(sendBuf, 0, BUF_SIZE);
+
+  sprintf(sendBuf, "%.1f", temperature);
+
+  ret = GO_Delivery(GetGWID(), NULL, DELIVERY_COLLECT_DATA, 0x01, sendBuf);
+
+  info("Sent Data: ");
+  infoln(sendBuf);
+  INFO("Sent Data: %s\r\n", sendBuf);
+
+  if (ret != GMMP_SUCCESS) {
+    errorln("DeliveryReq Error!!");
+    return;
+  }
+
+  lastDeliverMilli = curMilli;
+}
+
+int main() {
+
+    if( setup() < 0 ) return 0;
+
+    while(1) {
+        loop();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#5aed8bae1001
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Aug 09 14:24:15 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b9ad9a133dc7
\ No newline at end of file