Context-oriented programming is proposed as a solution for adapting cyber-physical systems to continuously changing environments. It represents environmental situations as contexts that define context-dependent functionality through layered functions. This allows decoupling functionality so that implementations are simpler. The approach is made language-independent through design concepts and programming support like ConesC. Evaluation shows it reduces complexity and coupling while incurring negligible overhead, enabling easier maintenance and verification against environmental evolutions. It has been applied to wildlife tracking and drone coordination.