Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include "stm32f4xx.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_tim.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_usart.h"
- #include "misc.h"
- #include "string.h"
- #define CR 0x0D
- #define LR 0x0A
- #define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
- #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
- #define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
- #define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
- #define TRCENA 0x01000000
- #define LINEMAX 300
- struct __FILE
- {
- int dummy;
- };
- FILE __stdout;
- FILE __stdin;
- int fputc(int ch,FILE *f)
- {
- if (DEMCR & TRCENA) {
- while (ITM_Port32(0) == 0);
- ITM_Port8(0) = ch;}
- return(ch);
- }
- /*int putcharx(int ch)
- {
- while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
- USART_SendData(USART2, (uint8_t)ch);
- return ch;
- } */
- unsigned char payload[40] = {0x40,0xD8,0x6B,0x06,0x40,0xE8,0xB3,0x62,0xB1,0x01,0x00,0x01,0x00,0x01,0x01,0x00,0x02,0x01,0x00,0x03,0x01,0x00,0x04,0x01,0x00,0x05,0x01,0x00,0x06,0x01,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- volatile char line_buffer[LINEMAX+1];
- volatile int Line_valid=0;
- uint8_t buffer1[50];
- uint8_t buffer2[50];
- uint8_t buffer3[50];
- uint8_t buffer_OK[50];
- uint8_t buffer_creg[50];
- unsigned char ch1;
- unsigned char end_flag=0;
- unsigned int read_data=0;
- unsigned int count=0;
- unsigned char gsm_rx=0;
- unsigned char rx_zigbee=0;
- unsigned int cmd=4;
- uint8_t gsm_buf_index=0;
- unsigned int rx_index2=0;
- unsigned int zigbee_receive_flag=0;
- unsigned int pTxData[24];
- unsigned char GSM_char[]="00";
- unsigned char gsm_buf[LINEMAX];
- unsigned char rx_buf_zigbee[LINEMAX];
- unsigned char write_buf_pointer=0;
- unsigned char server_tx_buf[128];
- uint8_t gsm_attempt=0;
- int i;
- uint8_t global_cell_id=0;
- uint8_t check_gprs=0;
- uint8_t set_apn=0;
- uint8_t wireless_ok=0;
- uint8_t zigbee_flag=0;
- uint8_t gsm_flag=0;
- char urc_code;
- char network_status;
- extern __IO uint32_t TimmingDelay;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_struct;
- void Delay_sec(__IO uint32_t time);
- void GPIOInitialize(void);
- void UART_Initialize(void);
- void NVICInitialize(void);
- void TIMER_Initialize(void);
- void InitializeLED(void);
- void USART_puts(volatile char *s);
- void extract_Data(char *s1, char *s2);
- void delay_routine(uint16_t j);
- void USART_SendPayload(void);
- void gateway_to_zigbee(void);
- void GSM_Send_Command(unsigned char *s1,unsigned char *s2,const unsigned char *s3, const unsigned char *s4, unsigned char *id, unsigned char *gsm_attempt, unsigned char clr);
- void received_response_80(unsigned char *s1,unsigned char *s2);
- void transmit_response_80(unsigned char *s1,unsigned char *s2);
- void request_To_server(const char *string);
- void addCharToBuffer(char get_data);
- void Blinker(unsigned int val);
- void Indication_On(unsigned int val);
- const char *CopyConst(char *dest, const char *src);
- uint8_t Network_Reg(unsigned char * command,unsigned char *response);
- uint8_t Fetch_local_IP(unsigned char *response);
- uint8_t GET_Cell_ID(void);
- uint8_t Set_APN_Name(unsigned char *response);
- uint8_t Check_GPRS(unsigned char * command,unsigned char *response);
- const char at_command1[] = "AT\r";
- const char at_command2[] = "AT+CREG?\r";
- const char at_command3[] = "AT+CGATT=1\r";
- const char at_command4[] = "AT+CIPSHUT";
- const char at_CSTT[] = "AT+CSTT=";
- const char APN_Name[]="\"airtelgprs.com\"\r";
- const char at_command6[] = "AT+CIICR";
- const char at_command7[] = "AT+CIPSTART=\"TCP\",\"52.25.26.236\",\"80\"";
- const char at_command8[] = "AT+CIPSEND\r";
- const char get_request[] = "\"GET/52.25.26.236 /test1.php?name=$$";//GET/52.25.26.236 /test1.php?name=ravindra kant HTTP1.1\r\n
- const char at_commandN[] = "AT+CREG=0\r";
- const char HTTP[] = "HTTP1.1\r\n\"";
- const char HOST_IP[] = "\"HOST: 52.25.26.236\r\n\n\"";
- const char at_response1[] = "\r\nOK\r\n";
- const char at_response2[] = "\r\n+CREG: 1,5\r\n\r\nOK\r\n";
- const char at_response3[] = "\r\n+CREG: 0,5\r\n\r\nOK\r\n";
- const char at_response4[] = "\r\nSHUT OK\r\n";
- const char at_response5[] = "ERROR";
- const char at_response6[] ="\r\n> ";
- /*******************************************
- *IRQ handler for USART1 STM32 port B PB6,PB7
- *AHB perpherals line 1 and APB bus 1
- *Modules Connected GSM
- ********************************************/
- void USART1_IRQHandler()
- {
- if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //it checks receive complete interrupt(USART_IT_RXNE) on USARTx
- {
- gsm_rx = USART_ReceiveData(USART1);
- gsm_buf[gsm_buf_index++]=gsm_rx;
- gsm_buf[gsm_buf_index]='\0';
- }
- }
- /*******************************************
- *IRQ handler for USART2 STM32 port D PD5,PD6
- *AHB perpherals line 1 and APB bus 2
- *Modules Connected Arduino
- ********************************************/
- void USART2_IRQHandler()
- {
- if(USART_GetITStatus(USART2,USART_IT_RXNE)!= RESET)
- {
- rx_zigbee = USART_ReceiveData(USART2);
- rx_buf_zigbee[rx_index2++]=rx_zigbee;
- zigbee_flag=1;
- }
- }
- void TIM2_IRQHandler()
- {
- if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET) //checking interrupt status
- {
- TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
- GPIO_ToggleBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13);
- }
- }
- /*******************************************
- *Delay Routine for 1ms
- *frequecy of oscillator is turned down to 168,000
- *from 168 MHz
- *********************************************/
- void Delay_sec(__IO uint32_t time)
- {
- TimmingDelay = time;
- while(TimmingDelay !=0);
- //TimmingDelay--;
- }
- void TIMER_Initialize(void)
- {
- TIM_TimeBaseInitTypeDef timerInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //APB1
- timerInitStructure.TIM_Prescaler=40000;
- timerInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- timerInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
- timerInitStructure.TIM_Period=10000;
- timerInitStructure.TIM_RepetitionCounter=0;
- TIM_TimeBaseInit(TIM2,&timerInitStructure);
- TIM_Cmd(TIM2,ENABLE);
- TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //timer interrupt
- }
- /**************************************************
- *Initialization of USART 1
- *Baudrate=9600
- *APB 2
- *AHB 1
- *Data Format 8N1
- *No Hardware flow control (required RTS and CTS)
- **************************************************/
- void UART_Initialize(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure); // USART configuration
- // USART_Init(USART2, &USART_InitStructure); // USART configuration
- USART_Cmd(USART1, ENABLE); // Enable USART
- // USART_Cmd(USART2, ENABLE); // Enable USART
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- }
- /**************************************************
- *Initialization of GPIO
- *Pin no 5 and 6 for PORT D
- *Pin no 6 and 7 for Port B
- *Output Type PushPull
- *Speed 100MHz
- **************************************************/
- void GPIOInitialize(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Enable clock for GPIOB
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);//Connect PB6 to USART1_Tx
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);//Connect PB7 to USART1_Rx
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);//connect Pa2 to USART2_Tx
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);//Connect Pa3 to USART2_Rx
- }
- void InitializeLED(void) //led initialize
- {
- GPIO_InitTypeDef GPIO_InitDef;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
- GPIO_InitDef.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
- GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOD, &GPIO_InitDef);
- }
- /**************************************************
- *Nested Vectored Interrupt Controller
- *Baudrate=9600
- *APB 2
- *AHB 1
- *Data Format 8N1
- *No Hardware flow control (required RTS and CTS)
- **************************************************/
- void NVICInitialize(void)
- {
- NVIC_struct.NVIC_IRQChannel = USART2_IRQn; //USART2 priority
- NVIC_struct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_struct.NVIC_IRQChannelSubPriority = 1;
- NVIC_struct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_struct);
- //NVIC_struct.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_struct.NVIC_IRQChannel = USART1_IRQn; //USART1 priority
- NVIC_struct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_struct.NVIC_IRQChannelSubPriority = 0;
- NVIC_struct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_struct);
- }
- void USART_puts(volatile char *s)
- {
- //printf("%c",(volatile char *)&s[0]);
- while(*s)
- {
- while( !(USART1->SR & 0x00000040) );
- USART_SendData(USART1, *s);
- *s++;
- }
- }
- void receive_response_80(unsigned char *s1,unsigned char *s2)
- {
- uint8_t i=0;
- while(*s1++)
- {
- if(*s1 =='\n')
- i++;
- if(i==11)
- break;
- }
- while(*s1 && (*s1!='\n'))
- {
- *s2++ = *s1++;
- }
- *s2='\0';
- Delay_sec(200);
- USART_SendData(USART2,*s1);
- //send received response from server to zigbee
- }
- void transmit_response_80(unsigned char*s1,unsigned char *s2)
- {
- char s3[]=ravindra kant";
- //if(zigbee_flag & 1)
- /// {
- // zigbee_flag=0;
- s1 = &rx_buf_zigbee[0];
- while(*s1)
- {
- *s2++=*s1;
- }
- *s2='\0';
- //}
- Delay_sec(200);
- USART_puts("AT+CIPSTART=\"TCP\",\"52.25.26.236\",\"80\"\r");//USART_puts(&TCP_request[0]);
- Delay_sec(3000);
- USART_puts("AT+CIPSEND\r");//USART_puts(&at_command8[0]);
- Delay_sec(1000);
- request_To_server(get_request);
- request_To_server(s3); //sending data received from zigbee
- request_To_server(HTTP);
- request_To_server(HOST_IP);
- USART_puts(&server_tx_buf[0]);
- USART_SendData(USART1,0x1A); //end of line is determined by ctrl-Z or 0x1A
- Delay_sec(2000); //this wait has to be checked later upon requirement only
- }
- void request_To_server(const char *string)
- {
- while(*string)
- {
- addCharToBuffer(*string++);
- }
- }
- void addCharToBuffer(char get_data)
- {
- server_tx_buf[write_buf_pointer++]=get_data;
- if(write_buf_pointer >=128)
- {write_buf_pointer=127;}
- }
- void Indication_On(unsigned int val)
- {
- GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_14);
- Delay_sec(val);
- GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_14);
- }
- void Blinker(unsigned int val)
- {
- while(val > 0)
- {
- GPIO_ToggleBits(GPIOD,GPIO_Pin_13 | GPIO_Pin_15);
- val--;
- }
- }
- void delay_routine(uint16_t j)
- {
- while(j!=0)
- {
- j--;
- }
- }
- int main(void)
- {
- uint16_t ch;
- SystemInit();
- GPIOInitialize();
- UART_Initialize();
- //NVICInitialize();
- TIMER_Initialize();
- InitializeLED();
- SysTick_Config(SystemCoreClock/1000); //normal clock is 168,000
- while(1)
- {
- if(USART_ReceiveData(USART1)) //unable to receive anything in normal case
- {GPIO_WriteBit( GPIOD, GPIO_Pin_12, Bit_SET);// GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13);
- Delay_sec(1000);
- GPIO_WriteBit(GPIOD, GPIO_Pin_12, Bit_RESET);}
- }
- }
- #ifdef USE_FULL_ASSERT
- void assert_failed(uint8_t* file, uint32_t line)
- {
- while (1)
- {}
- }
- #endif
Add Comment
Please, Sign In to add comment