The Internet of Things paradigm has brought opportunities to meet several challenges by interconnecting IoT resources, such as sensors, actuators, and gateways on a massive scale. The IoT gateways play an important role in the IoT applications to bridge between sensor networks and the external environment through the Internet. Typically, the IoT gateways collect and send the data collected from sensors and actuators to external platforms where they will be remotely analyzed. However, the users desire a more adapted IoT gateway that can improve the IoT data privacy preservation before sending them to these external platforms. Thus, an IoT gateway that enables a better control over the set of private IoT resources and protects the collected personal data and their privacy is required. For this purpose, we propose a Semantic IoT Gateway that helps implement a dynamic and flexible privacy-preserving solution for the IoT domain. First, it enables to match between the data consumer's terms of service and the data owner's privacy preferences by generating an adapted privacy policy. Second, it converts the privacy policy into a custom smart contract. Finally, it connects a set of private IoT resources to a distributed network using the blockchain technology to host the generated smart contracts. A smart contract is an executable code that runs on top of the blockchain to facilitate, execute and enforce an agreement between untrusted parties without the involvement of a trusted third party. Our proposal, which is highlighted through an example and experimentation on a real-world use-case, has given the expected results.