|
|

|
|
|
|
|
|
# Ausgangslage
|
|
|
* UART WiFi Modul
|
|
|
* Baud Rate: 115200
|
|
|
* Based on ESP8266 ESP-06 SoC
|
|
|
* AT Firmware: esp_iot_sdk_v1.1.0 + Seeed modifications:
|
|
|
* 2x additional AT commands to control blue Link LED.
|
|
|
* [AT command set](https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf)
|
|
|
* +19.5dBm output power in 802.11b
|
|
|
|
|
|
* WeMos D1 CH340 ESP8266 UNO Board / Wireless Board ESP8266
|
|
|
* Arduino UNO kompatible
|
|
|
|
|
|
# Bedingungen
|
|
|
* Kommunikation mit einem Server über AP (Wireless Access Point)
|
|
|
* HTTPS Kommunikation mit einem Server
|
|
|
* Spielverlauf online darstellen
|
|
|
* Spielresultate auf Datenbank persistieren
|
|
|
|
|
|
# Aufbau
|
|
|
* [Seeeduino V4.2](http://wiki.seeed.cc/Seeeduino_v4.2)
|
|
|
* als [**Arduino UNO**](https://www.arduino.cc/en/Main/Software) programmieren
|
|
|
* [WeMos D1](http://wiki.seeed.cc/Seeeduino_v4.2)
|
|
|
* als **WeMos D1** programmieren, nicht als Arduino UNO
|
|
|
* [CH340 Treiber für Windows](https://download.bastelgarage.ch/Produkte/CH341SER.ZIP)
|
|
|
* [CH340 MacOS Treiber](https://github.com/adrianmihalko/ch340g-ch34g-ch34x-mac-os-x-driver)
|
|
|
* Dieses D1 WIFI UNO Board ist mit der Arduino IDE kompatibel und kann gleich programmiert werden.
|
|
|
* Zuerst musst du dieses Board unter dem Boards Manager in der Arduino IDE installieren.
|
|
|
* Du findest das Board unter dem Namen **esp8266**.
|
|
|
* Falls du das Board nicht findest musst du zuerst unter File=>Preferences=>Additonal Boards Manager URLs: http://arduino.esp8266.com/stable/package_esp8266com_index.json hinzufügen. Und anschliessend unter Tools=>Boards=>Boards Manager kannst du den ESP8266 suchen und installieren. Sobald du das gemacht hast findest du unter den Boards diverse ESP8266 Boards die du anwählen kannst. Für dieses Board musst du den **WeMos D1** anwählen.
|
|
|
|
|
|
* Input
|
|
|
* [Wii Nunchuck](http://wiki.seeed.cc/Grove-NunChuck) - <code>I2C</code> [Two-Wire Peripheral Interface](http://gammon.com.au/i2c)
|
|
|
* Output
|
|
|
* Serial Monitor (Konsole) <code>UART</code>
|
|
|
* [Led](http://wiki.seeed.cc/Grove-LED_Socket_Kit/) - <code>D8</code>
|
|
|
* Input/Output
|
|
|
* [Grove - UART WiFi](http://wiki.seeed.cc/Grove-UART_Wifi/) - <code>UART: 2=Rx, 3=Tx</code>
|
|
|
* Input / Output
|
|
|
* HTTPS
|
|
|
|
|
|
# Handlungsziele
|
|
|
|
|
|
|Vorgehen|Hannok [Handlungsnotwendige Kenntnisse](https://cf.ict-berufsbildung.ch/modules.php?name=Mbk&a=20101&cmodnr=242&noheader=1)|
|
|
|
|:-------|:------|
|
|
|
|Kommunikation | UART |
|
|
|
|Input/Output| AT Command SET |
|
|
|
|sinnvolle Datentypen | CHAR |
|
|
|
|Testbarkeit | Loggen, ASSERT |
|
|
|
|Kommunikation | UART / SPI / |
|
|
|
|Input/Output| HTTPs GET request / respond |
|
|
|
|sinnvolle Datentypen | String array |
|
|
|
|Testbarkeit | DEBUG, Loggen, ASSERT |
|
|
|
|
|
|
# Code Sample
|
|
|
|
|
|
[**ESP8266 AT Instruction Set**](https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf)
|
|
|
- Libraries:
|
|
|
- [**ESP8266WiFi**](https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi)
|
|
|
- [ArduinoJson](https://arduinojson.org/)
|
|
|
|
|
|
```c
|
|
|
#include <SoftwareSerial.h>
|
|
|
/* HTTP over TLS (HTTPS) example sketch
|
|
|
ESP8266 needs WLAN nach IEEE 802.11 b/g/n)
|
|
|
*/
|
|
|
#include <ESP8266WiFi.h>
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
/* Use Serial Monitor with AT COMMAND SET as Input, e.g:
|
|
|
//#define DEBUG
|
|
|
#define DELAY 500
|
|
|
|
|
|
AT
|
|
|
AT+LEDON
|
|
|
AT+LEDOFF
|
|
|
const char* SSID = "---";
|
|
|
const char* PASSWORD = "---";
|
|
|
|
|
|
more Infos: SP8266 AT Instruction Set
|
|
|
*/
|
|
|
const char* HOST = "m242-project.komeo.net";
|
|
|
const int HTTPS_PORT = 443;
|
|
|
|
|
|
#define DELAY 20
|
|
|
#define BAUD_RATE_MONITOR 9600
|
|
|
#define BAUD_RATE_WIFI 115200
|
|
|
// use web browser to view and copy SHA1 fingerprint of the certificate
|
|
|
const char FINGERPRINT[] PROGMEM = "1f04e70199fae0a8a2185d4922b5f9630a4e68e3";
|
|
|
|
|
|
SoftwareSerial WiFi(2, 3); // Rx, Tx
|
|
|
// server respond string array
|
|
|
const int SIZE = 50;
|
|
|
String respond[SIZE];
|
|
|
int id = 1;
|
|
|
//char request[SIZE] = "/score.php?id=";
|
|
|
|
|
|
void setup() {
|
|
|
Serial.begin(BAUD_RATE_MONITOR);
|
|
|
while (!Serial); // wait for monitor UART
|
|
|
WiFi.begin(BAUD_RATE_WIFI);
|
|
|
while (!WiFi); // wait for WiFi UART
|
|
|
Serial.println("Ready - Input AT Commands on Serial Monitor\n");
|
|
|
Serial.begin(19200);
|
|
|
Serial.println("ESP8266 setup:");
|
|
|
Serial.printf("> connecting to '%s' ", SSID);
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
WiFi.begin(SSID, PASSWORD);
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
delay(DELAY);
|
|
|
Serial.print(".");
|
|
|
}
|
|
|
|
|
|
Serial.print(" > connected to IP address: ");
|
|
|
Serial.println(WiFi.localIP());
|
|
|
}
|
|
|
|
|
|
WiFiClientSecure connect2server() {
|
|
|
// Use WiFiClientSecure class to create TLS connection
|
|
|
WiFiClientSecure client;
|
|
|
#ifdef DEBUG
|
|
|
Serial.printf("> connecting to '%s'\n", HOST);
|
|
|
Serial.printf("> using fingerprint '%s'", FINGERPRINT);
|
|
|
#endif
|
|
|
client.setFingerprint(FINGERPRINT);
|
|
|
|
|
|
if (!client.connect(HOST, HTTPS_PORT)) {
|
|
|
Serial.println(" > connection failed");
|
|
|
}
|
|
|
#ifdef DEBUG
|
|
|
if (client.verify(FINGERPRINT, HOST)) {
|
|
|
Serial.println(" > certificate matches");
|
|
|
} else {
|
|
|
Serial.println(" > certificate doesn't match");
|
|
|
}
|
|
|
#endif
|
|
|
return client;
|
|
|
}
|
|
|
|
|
|
String sendGETRequest(WiFiClientSecure client, String request) {
|
|
|
int count = 0;
|
|
|
Serial.print("> sending request: ");
|
|
|
Serial.println(request);
|
|
|
client.print(String("GET ") + request + " " +
|
|
|
"HTTP/1.1\r\n" +
|
|
|
"Host: " + HOST + "\r\n" +
|
|
|
"User-Agent: BuildFailureDetectorESP8266\r\n" +
|
|
|
"Accept-Charset: utf-8\r\n" +
|
|
|
"Connection: close\r\n\r\n");
|
|
|
|
|
|
// read all the lines of the reply from server and print them to Serial
|
|
|
Serial.print("< server responds: ");
|
|
|
while (client.available()) {
|
|
|
respond[count++] = client.readStringUntil('\r');
|
|
|
}
|
|
|
#ifdef DEBUG
|
|
|
Serial.println();
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
Serial.print(respond[i]);
|
|
|
}
|
|
|
#endif
|
|
|
return respond[9];
|
|
|
}
|
|
|
|
|
|
void parse2json (String player) {
|
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
|
int len = player.length();
|
|
|
char json[len];
|
|
|
player.toCharArray(json, len);
|
|
|
JsonObject& root = jsonBuffer.parseObject(json);
|
|
|
if (!root.success()) {
|
|
|
Serial.println("parseObject() failed");
|
|
|
return;
|
|
|
}
|
|
|
String id = root["id"];
|
|
|
String name = root["name"];
|
|
|
String firstname = root["firstname"];
|
|
|
String score = root["score"];
|
|
|
Serial.print(id);
|
|
|
Serial.print(" ");
|
|
|
Serial.print(name);
|
|
|
Serial.print(" ");
|
|
|
Serial.print(firstname);
|
|
|
Serial.print(" ");
|
|
|
Serial.println(score);
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
if (Serial.available()) WiFi.write(Serial.read());
|
|
|
delay(DELAY);
|
|
|
if (WiFi.available()) Serial.write(WiFi.read());
|
|
|
WiFiClientSecure client = connect2server();
|
|
|
String request = "/score.php?id=";
|
|
|
request += id++;
|
|
|
String player = sendGETRequest(client, request);
|
|
|
//Serial.println(player);
|
|
|
parse2json(player);
|
|
|
delay(DELAY);
|
|
|
}
|
|
|
``` |