// UART DATA To Raspberry
// Raspberry To Web
void SendToWeb() {
	UARTClass * uart = UARTClass::getInstance();
	uart->setup();
	uart->startCommunicate();

	while (true) {
		try {
			ClientSocket client("218.150.181.154", 10000);
			while (true) {
				try {
					client << uart->getMsg(); // 웹으로 메세지 보내기
											  //cout << uart->getMsg() << endl;
					uart->clear();
				}
				catch (SocketException& e) {
					cout << "Exception was caught: " << e.description() << "\n";
				}
				delay(1000);	// 10sec 
			}
		}
		catch (SocketException& e) {
			cout << "ClientSocket Constructor Exception" << e.description() << endl;
		}
		delay(100);
	}
}
int main() {
	UARTClass * uart = UARTClass::getInstance();
	AltitudeController *altiCon = AltitudeController::getInstance();
	UltrasonicClass *sonar = UltrasonicClass::getInstance();
	DroneController *drone = DroneController::getInstance();

	uart->setup();
	uart->startCommunicate();

	// *************** ( ^_^ )*****************
	thread serverTh([&]() { RecvFromWeb(); });
	serverTh.detach();
	//thread clientTh([&]() { SendToWeb(); });
	//clientTh.detach();
	// *****************************************
	delay(3000);

	while (1) {
		uart->SendSerial(drone->get_pwm_string());
		cout << "throttle : " << drone->get_pwm(droneControlType::throttle) << endl;
		if (is_takeoff) {
			altiCon->calculate();
		}
		delay(100);
	}

	return 0;
}