The convergence of farming with cutting-edge technologies, like the Internet of Things (IoT), has led to the emergence of a smart farming revolution. IoT facilitates the interconnection of numerous devices across different agricultural ecosystems, enabling automation and ultimately enhancing the efficiency and quality of production. However, the implementation of IoT entails an array of potential risks. The accelerated adoption of IoT in the domain of smart farming has amplified the existing cybersecurity concerns, specifically those pertaining to access control. In extensive IoT environments that require scalability, the conventional centralized access control system is insufficient. Therefore, to address these gaps, we propose a novel decentralized access control framework. The framework applies blockchain technology as the decentralization approach with smart contract application focuses on the application scenario in smart farming to protect and secure IoT devices from unauthorised access by anomalous entities. The proposed framework adopted attribute-based access control (ABAC) and role-based access control (RBAC) to establish access rules and access permissions for IoT. The framework is validated via simulation to determine the price of gas consumption when executing smart contracts to retrieve attributes, roles and access rules between three smart contracts and provide the baseline value for future research references. Thus, this paper offers valuable insight into ongoing research on decentralized access control for IoT security to protect and secure IoT resources in the smart farming environment.