A smart home provides automated services based on the context of the home environment and user activity. Context acquiring, processing, reasoning, and disseminating to the services are complex tasks for a context-aware system. An appropriate middleware architecture could handle such complexity. In this paper, we proposed a middleware architecture for a context-aware system in smart home environment. Here, the context is modeled based on ontology using Web Ontology Language (OWL). In addition, a profile applied improved rule-based reasoning algorithm is integrated into this middleware to infer high-level contexts from available low-level contexts. Experimental result shows that the middleware provides more accurate and faster reasoning outcome compare with the traditional rule-based reasoning method. Moreover, context-aware service is also selected using the rule-based algorithm, where the service can be extended easily by adding new service rules in the service rule base.