Advertisement
Guest User

Untitled

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