//========================================================= // Serial //========================================================= // Question //--------------------------------------------------------- String serial_question(String msg) { Serial.print(msg + " : "); while(!Serial.available()); msg = Serial.readStringUntil('\n'); while(Serial.available()) { Serial.read(); } msg.trim(); Serial.println(msg); return msg; } //========================================================= // Server //========================================================= // Connect To Server //--------------------------------------------------------- void server_connect() { // If Connected Just Skip if(tcpClient.connected()) { return; } // Try To Connect To Server while(!tcpClient.connect(scoreboard_server, atoi(scoreboard_port))) { LED_TOGGLE; delay(200); } // Stay On LED_ON; // Connection Established Serial.println(MSG_TCP_CONNECTED); } //--------------------------------------------------------- // JSON Event Generator //--------------------------------------------------------- String json_button_message(String buttonName) { // Empty Space JSON.clear(); // Device Name JSON["device"] = joypad_identifier; JSON["type"] = "score"; // JSON["cid"] = ESP.getChipId(); // JSON["mac"] = WiFi.macAddress(); // JSON["ipa"] = WiFi.localIP().toString(); // JSON["battery"] = analogRead(A0); // JSON["signal"] = WiFi.RSSI(); JSON["button"] = buttonName; // JSON["time"] = millis(); // JSON["time"] = ntp_print_time(); // String Data Buffer String ouput; // Print To Serial serializeJson(JSON, ouput); // Save Memory JSON.clear(); // Return The String return ouput; } //--------------------------------------------------------- // JSON Telemetry Generator //--------------------------------------------------------- String json_telemetry_message() { // Empty Space JSON.clear(); // Device Name JSON["device"] = joypad_identifier; JSON["type"] = "telemetry"; JSON["signal"] = WiFi.RSSI(); JSON["battery"] = analogRead(A0); JSON["mac"] = WiFi.macAddress(); JSON["time"] = ntp_print_time(); // String Data Buffer String ouput; // Print To Serial serializeJson(JSON, ouput); // Save Memory JSON.clear(); // Return The String return ouput; } //--------------------------------------------------------- // JSON Telemetry Generator //--------------------------------------------------------- void read_buttons() { // TopLeft - Send Message if(btnRedOne.pressed()) { String x = json_button_message("RED1"); Serial.println(x); tcpClient.print(x); delay(150); } // BottomLeft - Send Message if(btnRedTwo.pressed()) { String x = json_button_message("RED2"); Serial.println(x); tcpClient.print(x); delay(150); } // TopRight - Send Message if(btnBlueOne.pressed()) { String x = json_button_message("BLU1"); Serial.println(x); tcpClient.print(x); delay(150); } // TopRight - Send Message if(btnBlueTwo.pressed()) { String x = json_button_message("BLU2"); Serial.println(x); tcpClient.print(x); delay(150); } } //--------------------------------------------------------- // JSON Telemetry Generator //--------------------------------------------------------- void buzz() { digitalWrite(_motor_pin, HIGH); delay(250); digitalWrite(_motor_pin, LOW); } //--------------------------------------------------------- // JSON Telemetry Generator //--------------------------------------------------------- void send_via_serial() { // Sending TCP Data if(Serial.available()) { String msg = Serial.readStringUntil('\n'); while(Serial.available()) { Serial.read(); } msg.trim(); // Create JSON Object JSON["type"] = "cmd"; JSON["header"] = "point"; JSON["body"] = msg; // Move The Message To String Variable serializeJson(JSON, msg); // Sending Data Out Serial.println(MSG_TCP_SEND + String(" : ") + msg); tcpClient.print(msg); // Empty Data JSON.clear(); } } //--------------------------------------------------------- // JSON Telemetry Generator //--------------------------------------------------------- void read_tcp() { // Received TCP Data if(tcpClient.available()) { String x = tcpClient.readStringUntil('\n'); while(tcpClient.available()) { tcpClient.read(); } x.trim(); Serial.print(" -> tcp signal in : "); Serial.println(x); // Clear JSON JSON.clear(); // Deserialize the JSON document DeserializationError error = deserializeJson(JSON, x); // Test if parsing succeeds if (error) { Serial.print(F("deserializeJson() failed: ")); Serial.println(error.f_str()); return; } // Read Message Type String mType = JSON["type"]; // Read Command String command = JSON["command"]; // Read BuZZer if(command == "buzz") { buzz(); } // Clear JSON JSON.clear(); } } //--------------------------------------------------------- // JSON Telemetry Generator //--------------------------------------------------------- void send_keep_alive() { // Keep Sending To Avoid Disconnection if(millis() - timing >= 15000) { // Buffer Data String x = json_telemetry_message(); // Send Out Data Serial.println(x); tcpClient.print(x); // Update Time Snap Shot timing = millis(); } } //========================================================= // END //=========================================================