For test

Dependencies:   mbed

ESP8266.h

Committer:
shennongmin
Date:
2015-02-04
Revision:
30:46a560b71e61
Parent:
29:75185b05d6a4
Child:
31:ab3e7ca014c1

File content as of revision 30:46a560b71e61:

#ifndef __ESP8266_H__
#define __ESP8266_H__

#include "mbed.h"
#include "time.h"
#include "ArduinoSerial.h"

typedef enum {
    ESP8266_ECN_OPEN          = 0,
    ESP8266_ECN_WEP           = 1,
    ESP8266_ECN_WAP_PSK       = 2,
    ESP8266_ECN_WAP2_PSK      = 3,
    ESP8266_ECN_WAP_WAP2_PSK  = 4,
} ESP8266Encrypstion;

typedef enum {
    ESP8266_COMM_UDP              = 0,
    ESP8266_COMM_TCP              = 1,
} ESP8266CommMode; 

typedef enum {
    ESP8266_MODE_STA         = 1,
    ESP8266_MODE_SAP         = 2,
    ESP8266_MODE_STA_SAP     = 3,
} ESP8266WorkMode;

typedef enum {
    ESP8266_CHANNEL_1 = 1,
    ESP8266_CHANNEL_2,
    ESP8266_CHANNEL_3,
    ESP8266_CHANNEL_4,
    ESP8266_CHANNEL_5,
    ESP8266_CHANNEL_6,
    ESP8266_CHANNEL_7,
    ESP8266_CHANNEL_8,
    ESP8266_CHANNEL_9,
    ESP8266_CHANNEL_10,
    ESP8266_CHANNEL_11,
    ESP8266_CHANNEL_12,
    ESP8266_CHANNEL_13,
} ESP8266Channel;

class ESP8266
{
public:

    /**
     * Constructor. 
     * 
     * Need an serial port for communication between MCU and ESP8266. 
     * 
     * @param tx - the TX of serial port. 
     * @param rx - the RX of serial port. 
     * @param baud_rate - the baud rate of communication(default 9600).
     */
    ESP8266(PinName tx, PinName rx, int baud_rate = 9600);
    
    bool setStationMode(String ssid, String pwd);
    bool setSoftAPMode(String ssid, String pwd,
        ESP8266Channel chl = ESP8266_CHANNEL_7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
    
    bool setStationSoftAPMode(String ssid, String pwd, String sap_ssid, String sap_pwd ,
        ESP8266Channel chl = ESP8266_CHANNEL_7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
    
    bool ipConfig(uint8_t type, String addr, int port, uint8_t mux = 0, uint8_t id = 0);
    
    //send data in sigle connection mode
    bool send(String str);  
    
    //send data int multiple connection mode
    bool send(uint8_t id, String str);  
        
    int recvData(char *buf);
    
    //reset the module
    bool reset(void);    
    
    //set the working mode of module
    bool confMode(uint8_t mode);   
    
    //set the name and password of wifi 
    bool confJAP(String ssid , String pwd);    
    
    //set the parametter of SSID, password, channel, encryption in AP mode.
    bool confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn);       
    
    //inquire the current mode of wifi module
    String showMode(void);   
    
    //show the list of wifi hotspot
    String showAP(void);   
    
    //show the name of current wifi access port
    String showJAP(void);  
    
    //quit the connection of current wifi
    bool quitAP(void);    
    
    //show the parameter of ssid, password, channel, encryption in AP mode
    String showSAP(void);     

    //inquire the connection status
    String showStatus(void);  
    
    //show the current connection mode(sigle or multiple)
    String showMux(void);  
    
    //set the connection mode(sigle:0 or multiple:1)     
    bool confMux(int mux);    
    
    //create new tcp or udp connection (sigle connection mode)
    bool newMux(uint8_t type, String addr, int port);   
    
    //create new tcp or udp connection (multiple connection mode)(id:0-4) 
    bool newMux(uint8_t id, uint8_t type, String addr, int port);   
    
    //close tcp or udp (sigle connection mode)
    void closeMux(void);   
    
    //close tcp or udp (multiple connection mode)
    void closeMux(uint8_t id); 
    
    //show the current ip address
    String showIP(void);    
    
    //set the parameter of server
    bool confServer(uint8_t mode, int port);  

    // get m_mux_id
    int getMuxID(void);
    
private:
    ArduinoSerial m_uart;
    int m_mux_id;
};

#endif /* #ifndef __ESP8266_H__ */