Internet of Things (IoT) network consists of many devices that communicate together and exchange data. IoT network has many applications especially in smart city and smart campus. IoT devices usually produce a huge amount of data that are stored in the cloud to be analyzed later. Data in general and IoT devices data in particular suffer from major security issues such as the availability and the integrity of data. Blockchain is a new technology that offers an interesting solution for the security of sensitive IoT data by protecting data against malicious tampering. However, the data stored in the blockchain cannot be altered, therefore, it should be validated before being stored in the blockchain especially that IoT devices are vulnerable to attacks. Machine learning algorithms are very useful to detect compromised IoT devices to ensure that only reliable data are stored in the blockchain.The main aim of this paper is to propose a framework for storing reliable IoT data in a secure way that preserves the integrity and availability properties. First, to detect compromised IoT devices, several machine learning algorithms are evaluated. The IoTID20 dataset for anomalous activity detection in IoT network is used to build our machine learning model. IoTID20 dataset contains 80 network features and 625783 records. For our experiment, 4000 records are selected randomly from the dataset. Two algorithms are used to select features namely Logistic Regression and Pearson's correlation. A total of 15 features are selected in order to classify packets as (Normal, Anomaly). Several machine learning algorithms are trained: Logistic Regression, Random Forest, Decision Tree, K Nearest Neighbors, AdaBoost, and Naïve Bayes. These algorithms are compared based on the following measurements: accuracy, precision, recall, F1 score, and the time for classification. The results reveal that Random Forest and AdaBoost classifiers give very close results and are considered the best classifiers based on all performance metrics used in the paper except the time. Regarding the time required to detect malicious records, Decision Tree and Naïve Bayes are the best as the time required to predict 4000 records is 0.015 seconds.A private blockchain is built to store normal data received from IoT devices after being filtered. The data is signed and then distributed to all nodes in the network for verification. To provide scalability, the consensus algorithm of the blockchain is based on the proof of authority algorithm. As a proof of concepts, the blockchain is developed using Python programming language and the time required to verify the signatures is computed.