W tym przykładzie połączymy się najmniejszą wersją STM32 (bluepill) z WiFi i postawimy pierwszy serwer www.

Serwer będzie serwował plik tekstowy Hello World i nic więcej. Nagłówki jednak są znacznie bardziej rozbudowane, niż to jedno zdanie. Mój przykład oparłem na tym poradniku. Tam jednak nie ma prawidłowych nagłówków HTTP, więc strona się nie ładowała.

Hardware

  1. STM32
  2. ESP8266 WiFi
  3. Programator/konsola UART (np. FTDI)

Łączymy:
1. STM32 UART1 (PA9,PA10) – FTDI UART (łączymy na krzyż RX-TX i TX-RX), oprócz tego zasilanie 5v z programatora FTDI VCC podłączamy do 5V STM32, a GND do GND.
2. STM32 UART2 (PA2,PA3) – ESP8266 (RX-TX, TX-RX), napięcie to 3,3V (UWAGA!), dodatkowy pin w WiFi EN też łączymy z 3,3V, GND-GND

STM32 BluePill - ESP8266 WiFi

Schematy połączeń ww znajdziecie w powyższym poradniku.

C++

W kodzie z ww. poradnika zmieniono: nagłówki HTML, usunięto zbędne zmienne oraz kilka drobnych poprawek naniesiono. UWAGA! Zmieniono prędkość terminali na 115200 baud (dla nowej wersji kart WiFi). Źle dobrana prędkość powoduje, że program nie nawiąże połączenia, alternatywnie 9600.
Najważniejsza jest linia, w której trzeba zmienić hasło sieci WiFi i ID (funkcja wifi_init())

1
AT+CWJAP="NetworkID","NetworkPassword\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
boolean No_IP=false;                                  
String IP="";                                         //String variable to store IP Address
     
void check4IP(int t1)                                     //A function to check ip of ESP8266
{
  int t2=millis();
  while(t2+t1>millis())
  {
    while(Serial2.available()>0)
    {
      if(Serial2.find("WIFI GOT IP"))
      {
        No_IP=true;
      }
    }
  }
}

void fx_println(String cmd)
{ //wysyła polecenie na oba złącza serial (konsola + WiFi) + znak LN
    Serial.println(cmd);                  //Sends to serial monitor
    Serial2.println(cmd);                 //sends to ESP8266 via serial communication  
}

void fx_print(String cmd)
{ //wysyła polecenie na oba złącza serial (konsola + WiFi)
    Serial.print(cmd);                  //Sends to serial monitor
    Serial2.print(cmd);                 //sends to ESP8266 via serial communication  
}


void get_ip()                                           //After cheacking ip ,this is a function to get IP address
{
  IP="";
  char ch=0;
  while(1)
  {
    fx_println("AT+CIFSR");                   //GET IP AT COMMAND
    while(Serial2.available()>0)        
    {
      if(Serial2.find("STAIP,"))                   //This finds the STAIP that is the STATIC IP ADDRESS of ESP8266
      {
        delay(1000);
        Serial.print("IP Address:");
        while(Serial2.available()>0)
        {
          ch=Serial2.read();                      //Serial2 reads from ESP8266
          if(ch=='+')
          break;
          IP+=ch;
        }
      }
      if(ch=='+')
      break;
    }
    if(ch=='+')
    break;
    delay(1000);
  }
  Serial.print(IP);                                //prints IP address in Serial monitor
  Serial.println("Port:80");
}

void connect_wifi(String cmd, int t)                  //This function is for connecting ESP8266 with wifi network by using AT commands
{
  uint8_t i=0;
  while(1)
  {
    fx_println(cmd); //wysłanie na oba UARTy
    while(Serial2.available())
    {
      if(Serial2.find("OK"))
      i=8;
    }
    delay(t);
    if(i>5)
      break;
    i++;
  }
  Serial.println(i==8 ? "OK" : "Error");
}

void wifi_init()                                //This function contains AT commands that passes to connect_wifi()
{
      connect_wifi("AT",100);                   //Sends AT command with time(Command for Achknowledgement)
      connect_wifi("AT+CWMODE=3",100);          //Sends AT command with time (For setting mode of Wifi)
      connect_wifi("AT+CWQAP",100);            //Sends AT command with time (for Quit AP)
      connect_wifi("AT+RST",5000);             //Sends AT command with time (For RESETTING WIFI)
      check4IP(5000);
      if(!No_IP)
      {    
        Serial.println("Łączę z siecią Wifi....");
        connect_wifi("AT+CWJAP=\"NetworkID\",\"NetworkPassword\"",7000);         //USER I HASŁO DO WIFI
        if(No_IP)  
            Serial.println("Połączone!");
      }
      get_ip();
     
      connect_wifi("AT+CIPMUX=1",100);                          //Sends AT command with time (For creating multiple connections) Wiele połączeń - obsługa
      connect_wifi("AT+CIPSERVER=1,80",100);                    //Sends AT command with time (For setting up server with port 80) Serwer www!
}



void sendwebdata(String webPage)                          //This function is used to send webpage datas to the localserver
{
    int ii=0;
     while(1)
     {
      unsigned int l=webPage.length();
      fx_print("AT+CIPSEND=0,");
      Serial.println(l+2);
      Serial2.println(l+2);
      delay(100);
      fx_println(webPage);
      while(Serial2.available())
      {      
        if(Serial2.find("OK"))
        {
          ii=11;
          break;
        }
      }
      if(ii==11)
      break;
      delay(100);
     }
}

void setup()
{
   delay(5000); //not neccessary, time to open console.
   Serial.begin(115200);                //begins serial monitor with baud rate 9600
   Serial2.begin(115200);               //begins serial communication with esp8266 with baud rate 9600 (Change according to your esp8266 module)
   wifi_init();
   Serial.println("System Ready..");
}

void loop()
{
  uint16_t k=0;
  Serial.print(".");
  while(k<1000)
  {
    k++;
   while(Serial2.available())
   {
    if(Serial2.find("0,CONNECT"))
    {
      Serial.println("Start Sending...");
      Send();
      Serial.println("..Done. Connection Closed.");
      delay(1000);
    }
  }
  delay(1);
 }
}

void Send()                                      
{
    //MINIMALNA STRONA WWW (nagłówki HTTP konieczne jeśli otwieramy przeglądarką)
      String webpage = "HTTP/1.1 200 OK\nContent-Length: 13\nContent-Type: text/plain; charset=utf-8\n\nHello World!\n";
      sendwebdata(webpage);
      fx_println("AT+CIPCLOSE=0");                  //Closes the server connection
}
C, STM32