• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoShowcaseHome AutomationDIY using ESP8266 WiFi module for OpenHAB on pcDui ...
Previous Next

DIY using ESP8266 WiFi module for OpenHAB on pcDuino

Posted by: admin , May 23, 2015

[vc_row][vc_column][vc_column_text]HomeMatic-HomeGear-Raspberry-piHome automation is a hot topic AGAIN recently. Among the many open source solution,  we choose  openHAB. The openHAB  project is aimed to provide the general platform for home automation. openHAB can run perfect on the pcDuino platform, can be used to DIY smart home project.  pcDuino serves as the server/gateway in the whole system. The remote client node uses the STM8L Low Power Microcontroller, which is a custom designed MCU board. They communicates to the pcDuino OpenHAB server  using  the low power WiFi module ESP8266 Serial WIFI Module.  In this particular project, we implemented a remote client that detects the status of the door status using a door status contact sensor. It senses the status and communicate the current state to pcDuino through the WiFi, and then pcDuino pushed the data using mqtt to openHAB server that resides on pcDuino. The openHAB server will then update the status on the mobile side.  We also implement the other way around. We press a button on the mobile APP, and then openHAB will push a message to the remote node to turn on a LED. The mobile APP can be downloaded form Android or Apple app store by searching for ‘openhab’.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Software Installtion on pcDuino” tab_id=”1432366592212-3-3″][vc_column_text]First we need to install the openHAB relate software, the detail can refer to LinkSprite Learning Center: http://learn.linksprite.com/?s=openhab

If you do not want to install yourself.  Please download the install the the image that we prepare for you (openHAB was installed under  under the /opt/openhab Directory) ;

http://pan.baidu.com/s/1ntHtCyX?qq-pf-to=pcqq.c2c  Extraction code:fh6c[/vc_column_text][/vc_tab][vc_tab title=”Hardware Prepare” tab_id=”1432366501-2-61″][vc_column_text]1  x  STM8L Module Board (other suitable MCU is OK)

1  x  ST-link (use for STM8L download debugging )

1  x  Door switch sensor

1  x  ESP8266(WIFI)

2  x  AA 5 type battery

1  x  pcDuino3 [/vc_column_text][/vc_tab][vc_tab title=”SIM 8L test code ” tab_id=”1432366567184-2-4″][vc_column_text]Plz notice the following code command setting:

The IP address for pcDuino and the SSID, password for current Wireless Router

/* 
 * www.linksprite.com
 * 20/05/2015
 */

#include "stm8l10x.h"
#include "stdio.h"
#include "string.h"

#define LED_ON()       GPIO_ResetBits( GPIOC, GPIO_Pin_4 )
#define LED_OFF()      GPIO_SetBits( GPIOC, GPIO_Pin_4 )

#define key  GPIOB,GPIO_Pin_1

// CE_BIT:   Digital Input     Chip Enable Activates RX or TX mode
/**
  * @addtogroup USART_Printf
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#ifdef _RAISONANCE_
 #define PUTCHAR_PROTOTYPE int putchar (char c)
 #define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
 #define PUTCHAR_PROTOTYPE char putchar (char c)
 #define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
 #define PUTCHAR_PROTOTYPE int putchar (int c)
 #define GETCHAR_PROTOTYPE int getchar (void)
#endif

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void USART_Config(void);
void ESP8266_Start(void);
void delay(unsigned int time);
void TCPTEST(void);
void TCPSend(char* senddata);
int Send_Command(char* ATcommand,char* ReturnValue,unsigned long time_out);
void low_gpio(void);

int i=0,j=0;

static int doorflag = 0;

@far @interrupt void NonHandledInterrupt (void)
{
   EXTI_ClearITPendingBit(EXTI_Pin_1);
   if( (GPIO_ReadInputDataBit(key)==0)&&(doorflag==0) )
            { 
              LED_ON();
              TCPSend("OPEN");
              doorflag = 1;
            }

            if( (GPIO_ReadInputDataBit(key)) && (doorflag==1) )
            {
                 LED_OFF();
                 doorflag=0;
               TCPSend("CLOS");
             }
}

void main(void)
{
    int flag=0;
    USART_Config();
    delay(2);
    low_gpio();
    GPIO_Init(GPIOB,GPIO_Pin_1, GPIO_Mode_In_PU_IT);
    EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Rising_Falling);
    GPIO_Init( GPIOC, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Slow);
    LED_ON();
    USART_ClearFlag();
    enableInterrupts();

    flag = Send_Command("AT","OK",50);
    flag = Send_Command("AT+RST","OK",20);
    flag = Send_Command("AT+CWMODE=1","OK",50);
    flag = Send_Command("AT+CWJAP="ssid","password"","OK",50);
    flag = Send_Command("AT+CWJAP?","OK",50);
    flag = Send_Command("AT+CIPSTART="TCP","192.168.1.155",10000","OK",50);

    LED_OFF();

    while(1) halt();
}

void delay(unsigned int time)
{
    while(time--)
    {
        for(i=0;i<50;i++)
        {
            for(j=0;j<2000;j++);
        }
    }
}

void TCPSend(char* senddata)
{
    Send_Command("AT+CIPSEND=4",">",50);
    Send_Command(senddata,"OK",50);
    //delay(1);
}

void TCPTEST(void)
{
     static int led=0;
     int answer=0, count=0;
     char receive[30]=" ";
     unsigned long num=0;
     char rec='';
      char sw= ''; 
         do{
         if(USART->SR & USART_SR_RXNE)
         {
            rec = USART->DR; //USART_ReceiveData8();
            receive[count] = rec ;
            count++;
            USART_ClearFlag();
       }
         j++;
         if(j==1000)
     {   
             j=0;
             num++;
        }
         if(num>1) 
            {
                answer=0;
              break;
            }
         }
        while(strstr(receive,"OK")==NULL);
        if( (strstr(receive,"GO")!=NULL)||(strstr(receive,"33")!=NULL) ) led++;
      if(led%2) { LED_ON();}
      else {LED_OFF();}
         memset(receive,'',30);
         count = 0;
}

int Send_Command(char* ATcommand,char* ReturnValue,unsigned long time_out)
{
     int answer=0, count=0;
     char receive[100]=" ";
     unsigned long num=0;
     char rec='';
    if (ATcommand[0] != '')
    {
        printf(ATcommand);    // Send the AT command 
                printf("rn");
    }
         do{
         if(USART->SR & USART_SR_RXNE)
         {
            rec = USART->DR; 
            receive[count] = rec ;
            count++;
            USART_ClearFlag();
       }
         j++;
         if(j==1000)
     {      
                j = 0;
                num++;
                if(num>time_out) 
                {
                    answer=0;
                    break;
                }
       }
         }
         while(strstr(receive,ReturnValue)==NULL);
         memset(receive,'',100);
         count = 0;
         answer=1;
    return answer;
}

void low_gpio(void)
{
    GPIO_Init(GPIOB,GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOB,GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOB,GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOB,GPIO_Pin_4, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOB,GPIO_Pin_5, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOB,GPIO_Pin_6, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOB,GPIO_Pin_7, GPIO_Mode_In_FL_No_IT);

    GPIO_Init(GPIOA,GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOA,GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOA,GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOA,GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);

    GPIO_Init(GPIOC,GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOC,GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);
    GPIO_Init(GPIOC,GPIO_Pin_4, GPIO_Mode_In_FL_No_IT);

    //GPIO_Init(GPIOC,GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
    //GPIO_Init(GPIOC,GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);
}

/**
  * @brief  Configure USART peripheral to print characters on Hyperteminal
  * @param  None
  * @retval None
  */
static void USART_Config(void)
{/* High speed internal clock prescaler: 1*/
    CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv8);

     /*Set the USART RX and USART TX at high level*/
    GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_3|GPIO_Pin_2, ENABLE);
     //GPIO_ExternalPullUpConfig(GPIOC,GPIO_Pin_3|GPIO_Pin_2, DISABLE);
    /* Enable USART clock */
    CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);

    USART_DeInit();
    /* USART configuration ------------------------------------------------------*/
    /* USART configured as follow:
          - BaudRate = 115200 baud  
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - Receive and transmit enabled
    */
     USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1,
               USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));

                                //USART_ITConfig(USART_IT_RXNE, ENABLE);

    /* Enable general interrupts */
  //  enableInterrupts();
}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  c Character to send
  * @retval char Character sent
  */
PUTCHAR_PROTOTYPE
{
  /* Write a character to the USART */
  USART_SendData8(c);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);
  return (c);
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %drn", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

[/vc_column_text][/vc_tab][vc_tab title=”Run and test ” tab_id=”1432366698507-3-10″][vc_column_text]

1. Install openHAB and the mobile equipment APP for pcDuino 

图片1

2. Download the sample code of STM8L into STM8l

图片2

3. Separately Weld the 2 lines on the door forbid sensor to PB1 and GND ( code set the PB2 as Pullup input, when the Magnetic blocks door forbid sensor near the receive port, the IO level will be low.

图片3

4. Power for pcDuino , run the open HAB and python MQTT:

Start openhab::

$cd /opt/openhab

$sudo ./start.sh

( If refresh the well done openHAB image, add  the auto start sudo /opt/openhab/start.sh  to /etc/rc.local  )

Run MQTT:( Here use the python to build a socket server port to monitor the date sending from WIFI from STM8L Module )

cd /home/ubutu

$sudo python ./mqtt_publish.py

图片4

*The IP address is modified as the pcDuino assigned address ( input  $ifconfig to check), modify position as following:

图片5

5. When the below program start, we press the reset key on STM8L Module, then we can see the model is well connected with pcDuino, and start the date transmitting:

图片6

6. Now we can open the mobile equipment APP software, click settings, do the following set:

图片7

IP address will modify according to the pcDuino real address, about the user name and the password, set as following:

$cd /opt/openhab/configurations

$vim ./users.cfg

And the “test” is the user name, the “12345678” is the password:

t图片8

7. After setting done, click the right corner “Save” to save the setting, then back to the main interface:

When the door is open state:

图片9

 

图片10

When the door is close state:

图片11

图片12[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][/vc_column][/vc_row]

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors