// WIFI #include const char* ssid = "xxx"; const char* password = "xxxx"; WiFiClient wifi; // websocket #include WebSocketsClient webSocket; String WSserver = "host"; int WSport = 81; //screen #include // Hardware-specific library #include TFT_eSPI tft = TFT_eSPI(); // Invoke custom library //force good pins for wemos shield and avoid to modify user_setup config #define TFT_CS PIN_D0 // Chip select control pin D8 #define TFT_DC PIN_D8 // Data Command control pin //#define TFT_RST PIN_D4 // Reset pin (could connect to NodeMCU RST, see next line) #define TFT_RST -1 //touchscreen #define TOUCH_CS PIN_D3 void setup() { // Setup the TFT display tft.init(); tft.setRotation(0); // Must be setRotation(0) for this sketch to work correctly tft.fillScreen(TFT_BLACK); // Setup baud rate and draw top banner Serial.begin(115000); // init wifi connect(); tft.setTextColor(TFT_WHITE, TFT_BLUE); tft.fillRect(0,0,240,16, TFT_BLUE); tft.drawCentreString(" drawer remote control ",120,0,2); // init touchscreen uint16_t calData[5] = { 465, 3271, 429, 3170, 3 }; tft.setTouch(calData); // draw buttons tft.setTextColor(TFT_WHITE); tft.fillRoundRect(0, 200, 60, 120, 3, TFT_DARKCYAN); tft.drawString(" left ",15,240,2); tft.fillRoundRect(60, 200, 120, 60, 3, TFT_PURPLE); tft.drawString(" up ",110,220,2); tft.fillRoundRect(60, 260, 120, 60, 3, TFT_ORANGE); tft.drawString(" down ",100,280,2); tft.fillRoundRect(180, 200, 60, 120, 3, TFT_DARKGREY); tft.drawString(" right ",190,240,2); tft.fillRoundRect(0, 120, 120, 60, 3, TFT_DARKGREEN); tft.drawString(" pen down ",30,140,2); tft.fillRoundRect(120, 120, 120, 60, 3, TFT_RED); tft.drawString(" pen up ",150,140,2); //init websocket Serial.println("Start Websocket Client"); webSocket.begin(WSserver, WSport); webSocket.onEvent(webSocketEvent); } void connect() { Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, would try to act as both a client and an access-point and could cause network-issues with your other WiFi-devices on your WiFi-network. */ WiFi.mode(WIFI_STA); if (password == "") WiFi.begin(ssid); else WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) { switch(type) { case WStype_DISCONNECTED: Serial.printf("[WSc] Disconnected!\n"); break; case WStype_CONNECTED: Serial.printf("[WSc] Connected to url: %s\n", payload); break; case WStype_TEXT: Serial.printf("[WSc] get text: %s\n", payload); break; case WStype_BIN: Serial.printf("[WSc] get binary lenght: %u\n", lenght); hexdump(payload, lenght); break; case WStype_ERROR: Serial.printf("We received an error \n"); break; default: break; } } void loop(void) { //webSocket.loop(); uint16_t x = 0, y = 0; // To store the touch coordinates // Pressed will be set true is there is a valid touch on the screen boolean pressed = tft.getTouch(&x, &y); if (pressed) { Serial.print("x,y = "); Serial.print(x); Serial.print(","); Serial.println(y); //touch start at 0,0 top right & 240,320 bottom left if (x < 130) { if (y < 160) { webSocket.sendTXT("w"); Serial.println(" pen down \r"); } else { webSocket.sendTXT("x"); Serial.println(" pen up \r"); } } else { if (y > 240) { webSocket.sendTXT("l"); Serial.println(" left \r"); } if (y < 60) { webSocket.sendTXT("r"); Serial.println(" right \r"); } if (y > 60 && y < 240) { if (x < 200) { webSocket.sendTXT("u"); Serial.println(" up \r"); } else { webSocket.sendTXT("d"); Serial.println(" down \r"); } } } } }