Advertisement
Guest User

Untitled

a guest
Jun 10th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. #include "stm32f4xx_conf.h"
  2. #include "stm32f4xx_exti.h"
  3. #include "stm32f4xx_gpio.h"
  4. #include "stm32f4xx_rcc.h"
  5. #include "stm32f4xx_usart.h"
  6. #include "stm32f4xx.h"
  7. #include "stm32f4_discovery.h"
  8. #include <string.h>
  9. #include <stdio.h>
  10.  
  11. //ESP8266 Rx -> STM32 PC10
  12. //ESP8266 Tx -> STM32 PC11
  13. //ESP8266 CH_PD -> STM32 3V
  14. //ESP8266 VCC -> STM32 3V
  15. //ESP8266 GND -> STM32 GND
  16. //192.168.4.1
  17. void Delay_us(volatile uint32_t delay);
  18. void USART3Init(void);
  19. void startServer(void);
  20. void connectToAP(char* name, char* password);
  21. void startclient(void);
  22. void USART_Send(volatile char *c);
  23. void connectToWiFi(void);
  24.  
  25. //const char* apname = "nazawa";
  26. //const char* appasswd = "asdfghjkl";
  27. //const int apport = 80;
  28.  
  29. //const char* pname = "nazawa";
  30. //const char* passwd = "passwd";
  31. const char* ip = "ip";
  32.  
  33. void Delay_us(volatile uint32_t delay)
  34. {
  35.     delay*=24;
  36.     while(delay--);
  37. }
  38.  
  39. void USART3Init(void)
  40. {
  41.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  42.  
  43.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  44.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  45.  
  46.     GPIO_InitTypeDef GPIO_InitStructure;
  47.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  48.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  49.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  50.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  51.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  52.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  53.     GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
  54.     GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
  55.  
  56.     USART_InitTypeDef USART_InitStructure;
  57.     USART_InitStructure.USART_BaudRate = 115200;
  58.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  59.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  60.     USART_InitStructure.USART_Parity = USART_Parity_No;
  61.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  62.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  63.     USART_Init(USART3, &USART_InitStructure);
  64.  
  65.     NVIC_InitTypeDef NVIC_InitStructure;
  66.     USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  67.     NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  68.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  69.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
  70.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  71.     NVIC_Init(&NVIC_InitStructure);
  72.     NVIC_EnableIRQ(USART3_IRQn);
  73.  
  74.     USART_Cmd(USART3, ENABLE);
  75. }
  76.  
  77. void startServer(void)
  78. {
  79.     //Delay_us(5000);
  80.     USART_Send("AT+CWMODE=3\r\n");      // Access Point
  81.     //Delay_us(200000);
  82.     //char buffer[64];
  83.     //int n = sprintf(buffer,"AT+CWSAP=\"%s\",\"%s\",4,2\r\n",name,passwd);
  84.     //USART_Send(buffer);
  85.     //Delay_us(200000);
  86.     USART_Send("AT+CIPMUX=1\r\n");          //Enable multiple connections (required)
  87.     //Delay_us(200000);
  88.     USART_Send("AT+CIPSERVER=1,80\r\n");
  89.     //Delay_us(200000);
  90. }
  91.  
  92. void connectToAP(char* name, char* password)
  93. {
  94.     char buffer[64];
  95.     int n = sprintf(buffer,"AT+CWJAP=\"%s\",\"%s\"\r\n",name,password);
  96.     USART_Send(buffer);
  97. }
  98.  
  99. void startclient(void)
  100. {
  101.  
  102.     USART_Send("AT+CWMODE=1\r\n");
  103.     //Delay_us(200000);
  104.     USART_Send("AT+CIPMUX=0\r\n");
  105.     //Delay_us(2000000);
  106.  
  107. }
  108.  
  109.  
  110. void USART3_IRQHandler(void)
  111. {
  112.     USART_Send("AT+CWJAP=\"Redmi\",\"asdfghjk\"\r\n");
  113.     char buff;
  114.  
  115.     if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
  116.     {
  117.         buff= USART3->DR;
  118.         switch(buff)
  119.         {
  120.  
  121.         case 49:
  122.         {
  123.             //USART_Send("AT+CIPSEND=1,2\r\n");
  124.             //USART_Send("AT+CIPSEND="1\n");
  125.             break;
  126.         }
  127.         case 50:
  128.         {
  129.             //USART_Send("AT+CIPSEND=2,2\r\n");
  130.             //USART_Send("AT+CIPSEND="2\n");
  131.                     break;
  132.         }
  133.         default:  break;
  134.         }
  135.  
  136.     }
  137.  
  138. }
  139.  
  140. void USART_Send(volatile char *c)
  141. {
  142.     int length = strlen(c);
  143.         for(int i=0; i<length; i++) {
  144.             USART_SendData(USART3, c[i]);
  145.             //Delay_us(500);
  146.         }
  147. }
  148. //void USART_Send(char *c)
  149. //{
  150. //  while(*c)
  151. //  {
  152. //      if (*c != 0)
  153. //      {
  154. //          USART_SendData(USART3, *c);
  155. //          *c++;
  156. //      }
  157. //  }
  158. //}
  159.  
  160.  
  161. void connectToWiFi(void)
  162. {
  163.  
  164.     char buffer[64];
  165.     int n = sprintf(buffer,"AT+CIPSTART=\"TCP\",\"%s\",1001\r\n",ip);
  166.     USART_Send(buffer);
  167.     //Delay_us(1000000);
  168.  
  169. }
  170.  
  171. int main()
  172. {
  173.     USART3Init();
  174.     //startServer();
  175.     //USART_Send("AT+RST\r\n");
  176.     //Delay_us(10000);
  177.     USART_Send("AT+CWMODE=1\r\n");
  178.     //Delay_us(1000);
  179.     //connectToAP("Redmi","asdfghjk");
  180.     //Delay_us(1000);
  181.     //USART_Send("AT+CWJAP=\"Redmi\",\"asdfghjk\"\r\n");
  182.     //USART_Send("AT+CIPSERVER=1,80\r\n");
  183.     //Delay_us(20000);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement