mbed WIZwiki-W7500 platform with Smart IoT Cloud Server Thinger.io


This project is based on mbed WIZwiki-W7500 platform launced by WIZnet. WIZwiki-W7500 can connect to the smart IoT cloud server called thinger.io.

Overview

This project started from my finding an easy & optimized smart IoT cloud services for easy to work with WIZwiki-W7500 platform. I found a cloud server called as thinger.io. But, thinger.io cloud server supports just only the device APIs based on Arduino IDE. But I need a device APIs based on mbed. Fortunately, thinger.io supports the Ardunio Ethernet device APIs, and I can port these APIs to mbed ethernet device API for WIZwiki-W7500.

In porting, The developer of thinger.io give me so much help & support.

Thru this post, I thank him server soooo much.

I tested my thinger.io device API for mbed with YuRobot Easy module shiled v1 have many sensors and LEDs.

WIZwiki-W7500

This IoT platform made by WIZnet and lauched on mbed as silicon parter.

There are many example on mbed. For more detail, Refer to Wiki site or mbed.

This is compatible with Arduino PIN map.

Thinger.io

Thinger.io is a platform that allows connecting things to the Internet.

And what’s new?

  • Hardware agnostic: Connect anything! from basic Arduinos with very limited resources, to more complex embedded systems running linux like Raspberry Pi.

  • It is Open Source, so you can take the code an build your own cloud if you want. It provides thing API discovery right out of the box, so you can code your things and interact easily from the web. Turning a led on from the Internet just take one line of code in the device, seriously!. It has been designed with developers in mind, so any developer will love to work with this platform.

For more detail, Refer to thinger.io.

<NOTICE> This site povids beta sevice. If you want to test your device on thinger.io, You should get a beta access keys.

YuRobot Easy shiled V1

YoRobot Shield provide many sensor such as light, temperature, humidity, IR and etc.


What I do

Compatible Client Class for Arduino

This class - TCPSocketConnectionArdu - need to minimize modification to Arduino Ethernet Device API of thinger.io.

class TCPSocketConnectionArdu : public TCPSocketConnection {

    public:

    /** TCP socket connection **/
    TCPSocketConnectionArdu() {};
    virtual ~TCPSocketConnectionArdu() { TCPSocketConnection::close(); };

    ssize_t write(uint8_t);

    /** Send data to the remote host.
    \param data The buffer to send to the host.
    \param length The length of the buffer to send.
    \return the number of written bytes on success (>=0) or -1 on failure
     */
    virtual ssize_t write(const void *buf, size_t size);
    virtual int available();
    ssize_t read();    
    virtual ssize_t read(void *buf, size_t size);    
    virtual ssize_t readBytes(uint8_t* buf, size_t size);
    virtual ssize_t peek();
    virtual void flush();
    virtual void stop();

    /** Check if the socket is connected
    \return true if connected, false otherwise.
    */
    virtual uint8_t connected();
    virtual operator bool(); 

    friend class TCPSocketServer;
};

Ethernet & TCP Client API for mbed : ThingerMBedEthernet, ThingerMBedClient

This is ported from ThingerEthernet & ThingerClient.h in Arduino Ethernet Library of thinger.io

  • ThingerClient
#ifndef _DHCP_   // You should be define your network if no DHCP
    #define DEV_IP_ADDRESS "xxx.xxx.xxx.xxx"
    #define DEV_GW_ADDRESS "xxx.xxx.xxx.xxx"
    #define DEV_SN_ADDRESS "255.255.255.0"
#endif    

class ThingerEthernet : public ThingerClient {
public:

    ThingerEthernet(const char* user, const char* device, const char* device_credential) :
            ThingerClient(client_, user, device, device_credential), connected_(false)
    {}
    ~ThingerEthernet(){
    }

protected:
    virtual bool network_connected(){
        return connected_;
    }

    virtual bool connect_network(){
        if(connected_) return true;
        uint8_t mac[6] = { 0x1D, 0xdc, 0x08, 0x00, 0x62, 0x11 };
        unsigned long ethernet_timeout = millis();

#ifdef _DHCP_        
    #ifdef _DEBUG_
        printf("[NETWORK] Getting DHCP IP Address...\r\n");
    #endif
        ((EthernetInterface*)WIZnet_Chip::getInstance()).init(mac);
#else
        ((EthernetInterface*)WIZnet_Chip::getInstance())->init(mac,DEV_IP_ADDRESS,DEV_SN_ADDRESS,DEV_GW_ADDRESS);
#endif
        ((EthernetInterface*)WIZnet_Chip::getInstance())->ethernet_link();
        delay(3000);
    #ifdef _DEBUG_
        #ifdef _DHCP_
            printf("[NETWORK] Waiting for IP...\r\n");
        #endif
    #endif
        connected_ = true;    
        if(((EthernetInterface*)WIZnet_Chip::getInstance())->connect() > 0) connected_ = false;
#ifdef _DEBUG_
        printf("[NETWORK] Got Ip Address: %s\r\n",((EthernetInterface*)WIZnet_Chip::getInstance())->getIPAddress());
#endif
        delay(1000);
        return connected_;
    }

private:
    bool connected_;
    TCPSocketConnectionArdu  client_;
};
  • ThingerClient
//---For MBED---//

#ifndef Client
    #define Client              TCPSocketConnectionArdu
#endif

#ifndef millis
    #define millis()        us_ticker_read()/1000
#endif

#ifndef delay    
    #define delay               wait_ms
#endif    

class ThingerClient : public thinger::thinger {
public:

    ThingerClient(Client& client, const char* user, const char* device, const char* device_credential) : client_(client), username_(user), device_id_(device), device_password_(device_credential),
            temp_data_(NULL), out_size_(0)
    {
    }
    ~ThingerClient()
    {
    }

protected:
    virtual bool read(char* buffer, size_t size)
    {
        size_t total_read = 0;
        while(total_read<size){
            int read = client_.readBytes((uint8_t*)buffer, size-total_read);
            if(read<0) return false;
            total_read += read;
        }
        return total_read == size;
    }

    // TODO Allow removing this Nagle's algorithm implementation if the underlying device already implements it
    virtual bool write(const char* buffer, size_t size, bool flush=false){
        if(size>0){
            temp_data_ = (uint8_t*) realloc(temp_data_, out_size_ + size);
            memcpy(&temp_data_[out_size_], buffer, size);
            out_size_ += size;
        }

        if(flush && out_size_>0){
            #ifdef _DEBUG_
                printf("[THINGER] Writing bytes: %d", out_size_);
            #endif
            size_t written = client_.write(temp_data_, out_size_);
            bool success = written == out_size_;
            free(temp_data_);
            temp_data_ = NULL;
            out_size_ = 0;

            #ifdef _DEBUG_
                printf(" [%s]\r\n",success ? "OK" : "FAIL");
            #endif

            //FIXME Without this small delay or activating the debug (which takes time), the CC3200 does not work well. Why?
            #ifdef __CC3200R1M1RGC__
                delay(1);
            #endif
            return success;
        }
        return true;
    }

    virtual void disconnected(){
        thinger_state_listener(SOCKET_TIMEOUT);
        client_.stop();
        thinger_state_listener(SOCKET_DISCONNECTED);
    }
    virtual bool connect_network(){
        return true;
    }
    virtual bool network_connected(){
        return true;
    }

    enum THINGER_STATE{
        NETWORK_CONNECTING,
        NETWORK_CONNECTED,
        NETWORK_CONNECT_ERROR,
        SOCKET_CONNECTING,
        SOCKET_CONNECTED,
        SOCKET_CONNECTION_ERROR,
        SOCKET_DISCONNECTED,
        SOCKET_TIMEOUT,
        THINGER_AUTHENTICATING,
        THINGER_AUTHENTICATED,
        THINGER_AUTH_FAILED
    };

    virtual void thinger_state_listener(THINGER_STATE state){
        #ifdef _DEBUG_
        switch(state){
            case NETWORK_CONNECTING:
                printf("[NETWORK] Starting connection...\r\n");
                break;
            case NETWORK_CONNECTED:
                printf("[NETWORK] Connected!\r\n");
                break;
            case NETWORK_CONNECT_ERROR:
                printf("[NETWORK] Cannot connect!\r\n");
                break;
            case SOCKET_CONNECTING:
                printf("[_SOCKET] Connecting to %s : %d ...", THINGER_SERVER,THINGER_PORT);   
                break;
            case SOCKET_CONNECTED:
                printf("[_SOCKET] Connected!\r\n");
                break;
            case SOCKET_CONNECTION_ERROR:
                printf("[_SOCKET] Error while connecting!\r\n");
                break;
            case SOCKET_DISCONNECTED:
                printf("[_SOCKET] Is now closed!\r\n");
                break;
            case SOCKET_TIMEOUT:
                printf("[_SOCKET] Timeout!\r\n");
                break;
            case THINGER_AUTHENTICATING:
                printf("[THINGER] Authenticating. User: %s Device: %s\r\n",username_,device_id_);
                break;
            case THINGER_AUTHENTICATED:
                printf("[THINGER] Authenticated!\r\n");
                break;
            case THINGER_AUTH_FAILED:
                printf("[THINGER] Auth Failed! Check username, device id, or device credentials.\r\n");
                break;
        }
        #endif
    }
    bool handle_connection()
    {
        bool network = network_connected();
        if(!network){
            thinger_state_listener(NETWORK_CONNECTING);
            network = connect_network();
            if(!network){
                thinger_state_listener(NETWORK_CONNECT_ERROR);
                return false;
            }
            thinger_state_listener(NETWORK_CONNECTED);
        }
        bool client = client_.connected();
        if(!client){
            client = connect_client();
            if(!client){
                return false;
            }
        }
        return network && client;
    }
    bool connect_client(){
        bool connected = false;
        client_.stop(); // cleanup previous socket
        thinger_state_listener(SOCKET_CONNECTING);
        if (client_.connect(THINGER_SERVER, THINGER_PORT) == 0) {
            delay(3000);
            thinger_state_listener(SOCKET_CONNECTED);
            thinger_state_listener(THINGER_AUTHENTICATING);
            connected = thinger::thinger::connect(username_, device_id_, device_password_);
            if(!connected){
                thinger_state_listener(THINGER_AUTH_FAILED);
                client_.stop();
                thinger_state_listener(SOCKET_DISCONNECTED);
            }
            else{
                thinger_state_listener(THINGER_AUTHENTICATED);
            }
        }
        else{
            thinger_state_listener(SOCKET_CONNECTION_ERROR);
        }
        return connected;
    }

public:
    void handle(){
        if(handle_connection()){
            #ifdef _DEBUG_
            if(client_.available()>0){
                printf("[THINGER] Available bytes: %d", client_.available());
            }
            #endif
            thinger::thinger::handle(millis(), client_.available()>0);
        }else{
            delay(RECONNECTION_TIMEOUT); // get some delay for a connection retry
        }
    }
private:
    Client& client_;
    const char* username_;
    const char* device_id_;
    const char* device_password_;
    uint8_t * temp_data_;
    size_t out_size_;
};

Registration WIZwiki-W7500 to thinger.io

<NOTICE> You should remmber your DEvice credentials becase the credentials can't be shown after add device.
  • You’re ready to work WIZwiki-W7500 with thinger.io Cloud Service.

Add to your main

#include "mbed.h"
#include "EthernetInterface.h"
#include "DHT.h"
#include "TCPSocketConnectionArdu.h"
#include "ThingerMBedEthernet.h"
#include "ThingerMBedClient.h"

/* ThingerIO Define */
#define USERNAME "MidnightCow"
#define DEVICE_ID "WIZwiki_W7500_mbed_01"
#define DEVICE_CREDENTIAL "your access key" // Should be your access key.

/* YUROBOT SHILED1 PIN Define*/
#define myLED1      D13
#define myLED2      D12

#define myRLED      D9
#define myGLED      D10
#define myBLED      D11

#define myBUZZ      D5

#define myTEMPHUMM  D4

#define myLIGHT     A1

//using namespace std;
DigitalOut myled1(myLED1);
DigitalOut myled2(myLED2);

DigitalOut myrled(myRLED);
DigitalOut mygled(myGLED);
DigitalOut mybled(myBLED);

DigitalOut myBuzz(myBUZZ);

AnalogIn   mylight(myLIGHT);

DHT myTempHumm(myTEMPHUMM, DHT11);

void my_led1(pson& in)
{
    myled1 = (in) ? 1 : 0;
}
void my_led2(pson& in)
{
    myled2 = (in) ? 1 : 0;
}

void my_TempHumm(pson& out)
{
    if(myTempHumm.readData() == 0)
    {
        out["Cecelcius"] = myTempHumm.ReadTemperature(CELCIUS);
        out["Humidity"]  = myTempHumm.ReadHumidity();        
    }
    else
    {
        out["Cecelcius"] = 65535;
        out["Humidity"]  = 65535;
    }
}

/*
void my_rgbled(pson& in)
{
    myrled = (in["Red"])   ?  1 : 0;
    mygled = (in["Green"]) ?  1 : 0;
    mybled = (in["Blue"])  ?  1 : 0;    
}
*/

int main() {
    EthernetInterface eth;
    ThingerEthernet thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
    myled1 = 1;
    myled2 = 1;

    // resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
    thing["myled1"] << my_led1;
    thing["myled2"] << my_led2;
    thing["mySensor"] >> my_TempHumm;
    //thing["myRGB"] << my_rgbled;

    while(1)
    {
        thing.handle();
    }
}

Demo Movie

WIZwiki-W7500 with ThingerIO from MidnightCow on Vimeo.

Conclusion

Thinger.io cloud sevice is very powerful for small IoT Device, becase This service support a small encoded packet called as Protoson.

So, WIZwiki-W7500 platform is suitable for thinger.io.

Thank you and enjoy them.


You can import my thinger.io mbed example here.

https://developer.mbed.org/users/MidnightCow/code/ThingerIO/

저작자 표시 동일 조건 변경 허락
신고
블로그 이미지

밤소 MidnightCow

위즈네트 칩(W5300, W5200, W7100, W7500) 개발자

Digital Photo Frame(DPF) Updated From a Remote FTP Server


Overview

This program(Remote DPF) is started from that I want to show my children photo to my parents live away from me.

You can upload a children photo on the FTP server anytime and anywhere.
If you have this DPF, You can download from a remote FTP server the updated photo and also always show that.

The downloaded phoho is saved to the micro SD card in DPF, and is displayed on 2.8” TFT LCD.


Component

  • Software
    • IDE : mbed Web-Complier (Free IDE)
    • Terminal : Hercules program (Freeware) or Any program
    • FTP : ALFTP program (Freeware) or Any FTP program

How to Operation

  1. For updating a new BMP file in SD card, Connect the FTP server whenever the specified time is expired.

  2. When updating is done or no new BMP file, DPF sequentially displays a BMP file in the SD card.

Demo

  • Updated new BMP file from FTP Server
  • Movie

Digital Photo Frame controlled remotely by using FTP protocol. from MidnightCow on Vimeo.

Download

https://developer.mbed.org/users/MidnightCow/code/Digital_Photo_Frame_with_FTP_SD_WIZwiki-/

저작자 표시 동일 조건 변경 허락
신고
블로그 이미지

밤소 MidnightCow

위즈네트 칩(W5300, W5200, W7100, W7500) 개발자

HTTP SD Card File Server on WIZwiki-W7500 platform

WIZwiki-W7500 platform과 ARMembed Web-Compiler를 이용하여, Web-Browser 상에서 SD File 목록을 볼 수 있도록 하는 프로그램을 작성해 보자.

Import Program on ARMmbed developer site

mbed 사이트에는 다양한 Example이 존재한다. 직접 모든 code를 작성할 수 있지만, Open source를 활용하면 손쉽고 빠르게 원하는 프로그램을 구현할 수 있다.

Search a program

mbed 검색에서 우리가 원하는 응용프로그램을 Key Word는 HTTP와 SD로 검색해 보자.
다행히 원하는 프로그램을 다음과 같이 볼 수 있다. 자 Import 하자.

주의  : 프로그램 작성 시 검색이 되었지만, 현재 검색이 되지 않는 관계로 아래 URL을 참조하자.
       https://developer.mbed.org/teams/FRDM-K64F-Code-Share/code/HTTP_SD_Server_K64F/

 

Import program

해당 Program을 성공적으로 Import 했을 경우 다음과 같이 Web-Compiler의 Program Workspace 창에 나타난다.

 

Select a platform

ARMmbed Web-Compiler의 우측 상단의 [mbed LPC1768] icon을 클릭하고, [Add Platform]을 눌러 Platfrom 목록에서 WIZwik-W7500 Platform를 찾아 선택하자.

WIZwiki-W7500 Platform을 선택할 경우 Web-Browser의 우측 창에 다음과 같이 나타난다.
[Add to your mbed Compiler] 버튼이 없을 경우 아래 Example programs 중 하나를 Import하면 WIZwiki-W7500 Platform을 당신의 mbed-Compiler에 추가할 수 있다.

 

How to port mbed-LPC1768 into WIZwiki-W7500

mbed-rtos 는 현재 사용하지 않는 라이브러리이므로 삭제(우 클릭 후 delect 선택)한다. 또한 lwip 기반의 EthernetInterface Library를 WIZnetInterface Library로 변경(삭제 후 추가)한다.

WIZnetInterface Library는 https://developer.mbed.org/teams/WIZnet/에서 Import 할 수 있다. (현재 검색이 잘 안되니, URL 참조).
WIZnetInterface Library를 Example HTTP_SD_Server_K64F 프로그램으로 import 한다.

 

Compile HTTP_SD_Server_K64F example for WIZwiki-W7500

지금까지 잘 따라 왔다면, 다음과 같이 Platform과 Library를 성공적으로 변경했을 것이다. 자 이제 과감하게 컴파일을 해보자.

[Compile] 버튼을 누르자. 다음과 같은 Error가 발생한다.

Can not open source input file "device.h" : No such file or directory "#include "device.h""

이 Error는 mbed Library가 Update 되지 않기 때문에 발생한다. 이를 해결하기 위해서는 [Program Workspace] 창의 “mbed” Library를 더블클릭하고, [Library Details] 창의 “Update” 버튼을 눌러 mbed Library를 Update한다.

다시 컴파일하자. 그럼 SDFileSystem 에서 4개, eth.init()에서 1개 총 5개의 Error가 발생한다.

4 Error : SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
1 Error : eth.init();

 

main.c

//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500
  • Ethernet Initialization
    eth.init() 대신 DHCP를 사용할 경우 eth.init(MAC) 를 사용하고, Static IP를 사용할 경우 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY)를 사용한다. 여기서는 static IP를 사용한다.
#define MAC     "\x--\x--\x--\x--\x--\x--"   // Should be Replaced -- with your MAC
#define IP      "192.168.77.100"             // Should belong with your lan.
#define MASK    "255.255.255.0"
#define GATEWAY "192.168.77.1"
//eth.init((uint8_t*)MAC);    // Use DHCP for WIZnetInterface
eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway

 

Upload the binary image on WIZwiki-W7500

WIZwiki-W7500은 CMSIS-DAP를 지원하여, W7500의 Code flash가 USB storage로 인식된다. Web-compiler에서 compile을 성공적으로 마쳤을 경우, Binaray Image를 Download할 수 있게 된다. 이때, Local Directory로 저장한 후 해당 Binary image를 WIZwiki-W7500의 USB storage(MBED)로 copy 하거나, image를 바로 MBED로 저장하면 된다.

 

Demos

해당 Project는 아래 URL이나 WIZwiki-W7500 Example programs에서 Import할 수 있도록 만들어 두었다.
동작 상태를 확인하기 위해 Ticker를 활용하여 LED1 LED2를 On/oFF toggle하는 코드도 추가되었다.

 

Initialization

 

Connect to WIZwiki-W7500 through Web-Browser

  • Terminal

  • Web-Browser

 

Click [img] directory

 

Click [WIZnet_log_IoT.png] file

저작자 표시 동일 조건 변경 허락
신고
블로그 이미지

밤소 MidnightCow

위즈네트 칩(W5300, W5200, W7100, W7500) 개발자

WIZWiki-W7500 Platform on ARMmbed

mBed Platform WIZwiki_W7500에 대해 간략히 살펴보고, mbed web-compiler를 활용g하는 방법을 알아보자.

mbed Platform WIZwiki-W7500

WIZnet사에서 개발한 W7500 - Cortex M0 + TCP/IP Core + 128KB flash + 16KB Ram - mcu를 기반으로 하는 Open source hardware platform으로, ARMmbed enabled Platform 이다.
자세한 내용은 wizwiki를 참조하세요.

 

  • Block Diagram
    WIZWiki-W7500 보드는 크게 3개의 Main IC - W7500, Ethernet PHY, MCU for debugging - 로 구성된다.
    ARMmbed enabled 지원을 위한 CMSIS-DAP을 내장하고 있어, mbed Web-Compiler로 통해 만들어진 binary image를 USB storage로 인식된 W7500의 Code flash로 단순히 copy하는 것만으로 Firmware를 Upload할 수 있다.

 

  • Part Layout

  • Extension Pin - Arduino Compatible
    WIZwiki-W7500의 확장 핀들은 Aruduino Board와 호환가능하도록 설계되어 있으며, 각종 Arduino Shield 들과 Stacking 할 수 있어 다양한 응용을 개발 할 수 있다.

ARMmbed

ARMmbed는 CortexM 계열의 ARM 개발자들을 위한 Open Community 사이트다. Web 개발 환경을 무료로 제공할 뿐 아니라, 개발자끼리 서로 code를 공유할 수 있다. ARM은 Silicon Partner와 Platform Partner와 같은 Eco-system을 제공하고, 개발자들은 자유롭게 이를 활용하여 손쉽게 빠르고 원하는 제품을 개발할 수 있도록 도와 준다.
ARMmbed에 접속을 하고 Sign-up 후 Web-Compiler를 무료로 사용할 수 있다.
다음 그림 우측 상단 Red Box 참조.

Web-Compiler

다음은 mbed Web-Compiler를 실행한 화면이다. 자신이 원하는 Example이나 응용을 찾아서 import Program 혹은 Import Library를 하면 자신의 Web-Compiler에 import한 Program이나 Library가 로딩된다.

 

How to import a program or library to your web-compiler

mbed developer site에서 원하는 program이나 library의 Keyword로 검색을 하고, 해당 프로그램을 click하자.

 

  • import
    해당 프로그램을 클릭할 경우 우측 상단에 [Import this program] 이나 [Import this Library]를 클릭하여 자신의 Web Compiler로 Loading 한다.

저작자 표시 동일 조건 변경 허락
신고
블로그 이미지

밤소 MidnightCow

위즈네트 칩(W5300, W5200, W7100, W7500) 개발자