Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4_discovery.h"
- //ESP8266 Rx -> STM32 PC10
- //ESP8266 Tx -> STM32 PC11
- //ESP8266 CH_PD -> STM32 3V
- //ESP8266 VCC -> STM32 3V
- //ESP8266 GND -> STM32 GND
- const char* apname = "nazawa";
- const char* appasswd = "asdfghjkl";
- const int apport = 80;
- const char* name = "nazawa";
- const char* passwd = "passwd";
- const char* ip = "ip";
- const int wifiport = "portport";
- void Delay_us(volatile uint32_t delay)
- {
- delay*=24;
- while(delay--);
- }
- void startServer(void)
- {
- //Delay_us(5000);
- USART_Send("AT+CWMODE=2\r\n"); // Access Point
- //Delay_us(200000);
- //char buffer[64];
- //int n = sprintf(buffer,"AT+CWSAP=\"%s\",\"%s\",4,2\r\n",name,passwd);
- //USART_Send(buffer);
- //USART_Send("AT+CWSAP=\"%s\",\"%s\",3,2\r\n",apname,appasswd);
- //Delay_us(200000);
- USART_Send("AT+CIPMUX=1\r\n"); //Enable multiple connections (required)
- //Delay_us(200000);
- USART_Send("AT+CIPSERVER=1,23\r\n");
- //Delay_us(200000);
- }
- void connectToAP(char* name, char* password)
- {
- char buffer[64];
- int n = sprintf(buffer,"AT+CWJAP=\"%s\",\"%s\"\r\n",name,password);
- USART_Send(buffer);
- }
- void startclient(void)
- {
- USART_Send("AT+CWMODE=1\r\n");
- //Delay_us(200000);
- USART_Send("AT+CIPMUX=0\r\n");
- //Delay_us(2000000);
- }
- void USART3Init(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = 115200;
- 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(USART3, &USART_InitStructure);
- NVIC_InitTypeDef NVIC_InitStructure;
- USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_EnableIRQ(USART3_IRQn);
- USART_Cmd(USART3, ENABLE);
- }
- void USART3_IRQHandler(void)
- {
- char a;
- if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
- {
- a= USART3->DR;
- if (a==4)
- {
- //nie wiem zapal diod
- }
- }
- }
- void USART_Send(volatile char *c)
- {
- int length = strlen(c);
- for(int i=0; i<length; i++) {
- USART_SendData(USART3, c[i]);
- //Delay_us(500);
- }
- }
- //uint8_t usartGetChar(void)
- //{
- // // czekaj na odebranie danych
- // while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
- //
- // return USART_ReceiveData(USART3);
- //}
- void connectToWiFi(void)
- {
- char buffer[64];
- int n = sprintf(buffer,"AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",ip,wifiport);
- USART_Send(buffer);
- //Delay_us(1000000);
- }
- int main()
- {
- USART3Init();
- startServer();
- //connectToAP("Redmi","asdfghjk");
- // Delay_us(20000000);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement