... | ... | @@ -3,6 +3,7 @@ |
|
|
# Ausgangslage
|
|
|
* WeMos D1 CH340 ESP8266 UNO Board / Wireless Board ESP8266
|
|
|
* Arduino UNO kompatible
|
|
|
* LCD SunStar 2004A
|
|
|
|
|
|
# Bedingungen
|
|
|
* HTTPS Kommunikation mit einem Server
|
... | ... | @@ -14,14 +15,14 @@ |
|
|
* 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.
|
|
|
|
|
|
* 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. 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
|
|
|
* LCD [SunStar 2004A ](https://www.beta-estore.com/download/rk/RK-10290_410.pdf) <code>I2C</code>
|
|
|
* Serial Monitor (Konsole) <code>UART</code>
|
|
|
* [Led](http://wiki.seeed.cc/Grove-LED_Socket_Kit/) - <code>D8</code>
|
|
|
* Input / Output
|
... | ... | @@ -31,9 +32,9 @@ |
|
|
|
|
|
|Vorgehen|Hannok [Handlungsnotwendige Kenntnisse](https://cf.ict-berufsbildung.ch/modules.php?name=Mbk&a=20101&cmodnr=242&noheader=1)|
|
|
|
|:-------|:------|
|
|
|
|Kommunikation | UART / SPI / |
|
|
|
|Kommunikation | UART / I2C / HTTPS / Rx/Tx |
|
|
|
|Input/Output| HTTPs GET request / respond |
|
|
|
|sinnvolle Datentypen | String array |
|
|
|
|sinnvolle Datentypen | define vs. const char* vs. String |
|
|
|
|Testbarkeit | DEBUG, Loggen, ASSERT |
|
|
|
|
|
|
# Code Sample
|
... | ... | @@ -48,117 +49,102 @@ |
|
|
*/
|
|
|
#include <ESP8266WiFi.h>
|
|
|
#include <ArduinoJson.h>
|
|
|
#include <Wire.h>
|
|
|
|
|
|
//#define DEBUG
|
|
|
#define DELAY 500
|
|
|
|
|
|
const char* SSID = "---";
|
|
|
const char* PASSWORD = "---";
|
|
|
// use Newliquidcrystal_1.3.5
|
|
|
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
|
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
|
|
const char* HOST = "m242-project.komeo.net";
|
|
|
const int HTTPS_PORT = 443;
|
|
|
#define DELAY 1000
|
|
|
#define LINES 4
|
|
|
#define COUNTS 10
|
|
|
|
|
|
// use this params when connecting to komeo project
|
|
|
const char* SSID = "solve"; // use available
|
|
|
const char* PASSWORD = "84866882"; // set correct pw
|
|
|
const char* HOST = "m242-project.komeo.net"; // use yor class project
|
|
|
const int HTTPS_PORT = 443; // this is htts port
|
|
|
// use web browser to view and copy SHA1 fingerprint of the certificate
|
|
|
const char FINGERPRINT[] PROGMEM = "1f04e70199fae0a8a2185d4922b5f9630a4e68e3";
|
|
|
|
|
|
// server respond string array
|
|
|
const int SIZE = 50;
|
|
|
String respond[SIZE];
|
|
|
int id = 1;
|
|
|
//char request[SIZE] = "/score.php?id=";
|
|
|
// LCD > I2C Address 0x27,EN,RW,RS,D4,D5,D6,D7
|
|
|
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);
|
|
|
//LiquidCrystal_I2C lcd(0x27, 20, 4);
|
|
|
|
|
|
int count_SSID = 0;
|
|
|
|
|
|
void setup() {
|
|
|
lcd.begin(20, 4);
|
|
|
lcd.setBacklightPin(3, POSITIVE);
|
|
|
lcd. setBacklight(HIGH);
|
|
|
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());
|
|
|
search_SSID();
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
// search SSID of WiFi Networks
|
|
|
void search_SSID() {
|
|
|
Serial.print("searching SSID ... ");
|
|
|
lcd.clear();
|
|
|
lcd.home();
|
|
|
lcd.print("searching SSID >> ");
|
|
|
count_SSID = WiFi.scanNetworks();
|
|
|
delay(DELAY);
|
|
|
if (count_SSID == 0) {
|
|
|
Serial.println("\nno SSID nearby found!");
|
|
|
lcd.setCursor(0, 1);
|
|
|
lcd.print("no SSID nearby found!");
|
|
|
} else {
|
|
|
Serial.println(" > certificate doesn't match");
|
|
|
Serial.println(count_SSID);
|
|
|
lcd.print(count_SSID);
|
|
|
}
|
|
|
#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');
|
|
|
for (int i = 0; i < count_SSID; i++) {
|
|
|
Serial.print(i + 1);
|
|
|
Serial.print(" - SSID: ");
|
|
|
Serial.print(WiFi.SSID(i));
|
|
|
Serial.print(" - SIGNAL_STRENGTH: ");
|
|
|
Serial.print(WiFi.RSSI(i));
|
|
|
Serial.print("dB - SECURED: ");
|
|
|
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? "No" : "Yes");
|
|
|
}
|
|
|
#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);
|
|
|
// display found SSID on LCD
|
|
|
void display_SSID() {
|
|
|
static int count = 0;
|
|
|
static int pos = 1;
|
|
|
static int new_search = 1;
|
|
|
lcd.setCursor(0, pos);
|
|
|
lcd.print(" ");
|
|
|
lcd.setCursor(0, pos);
|
|
|
lcd.print(count + 1);
|
|
|
lcd.setCursor(2, pos);
|
|
|
String ssid = (String)WiFi.SSID(count).substring(0, 14);
|
|
|
lcd.print(ssid);
|
|
|
lcd.setCursor(16, pos);
|
|
|
lcd.print((String)WiFi.RSSI(count));
|
|
|
lcd.setCursor(19, pos);
|
|
|
String enc = (String)((WiFi.encryptionType(count) == ENC_TYPE_NONE) ? "" : "*");
|
|
|
lcd.print(enc);
|
|
|
|
|
|
// rotate SSID
|
|
|
if (pos == LINES - 1) pos = 1;
|
|
|
else if (pos == count_SSID) pos = 1;
|
|
|
else pos++;
|
|
|
if (count + 1 > count_SSID - 1) count = 0;
|
|
|
else count++;
|
|
|
|
|
|
if (new_search > COUNTS) {
|
|
|
new_search = 1;
|
|
|
count = 0;
|
|
|
pos = 1;
|
|
|
search_SSID();
|
|
|
} else new_search++;
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
WiFiClientSecure client = connect2server();
|
|
|
String request = "/score.php?id=";
|
|
|
request += id++;
|
|
|
String player = sendGETRequest(client, request);
|
|
|
//Serial.println(player);
|
|
|
parse2json(player);
|
|
|
if (count_SSID > 0) display_SSID();
|
|
|
else search_SSID();
|
|
|
delay(DELAY);
|
|
|
}
|
|
|
``` |