Hello,
Today I would like to share with you my second little project. Its an Arduino temperature logger which saves data on micro SD card and thanks to a short Python script it can also send data to Ubidots IOT application. The circuit itself is very simple, in order to complete it all you need is:
11 - jumper wires
1 - 4.7K ohm resistor
1 - DS18B20 temperature sensor
1 - SD card reader
1 - Arduino UNO
Here's the circuit's schematic:
Here's a video showing how assemble the circuit:
Arduino sketch for this project:
#include <OneWire.h> //library for communication with temperature sensor #include <DallasTemperature.h> //temperature sensor library #include <SD.h> //sd card reader library available in every Arduino IDE #include <SPI.h> //library for communication with SD card reader // Data wire is plugged into port 2 on the Arduino #define ONE_WIRE_BUS 2 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); File logs; // variable responsible for writing to file float temp; //variable storing the current temperature int number = 0; //variable holding the number indicating current entry in csv file unsigned int hour = 20; //variable storing current hour unsigned int minute = 45; //variable storing current minute unsigned int day = 2; //variable storing current day of the week unsigned int date = 15; //variable storing current day of the month unsigned int month = 12; //variable storing current month unsigned int year = 2015; //variable storing current year unsigned int leap_year = 0; //variable for checking whether it's a leap year or not // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); void setup(void) { // start serial port Serial.begin(9600); SD.begin(10); // Start up the library sensors.begin(); } //end of void setup void loop(void) { //checking whether it's leap year or not if (year % 4 == 0) leap_year = 1; else leap_year = 0; //changing minutes if (minute < 55) minute +=5; else minute = 0; //changing hour if ((minute == 0) && (hour < 24)) hour++; if ((minute == 0) && (hour == 24)) hour = 0; //changing day of the week if((minute == 0) && (hour == 0) && (day <8)) day +=1; if((minute == 0) && (hour == 0) && (day == 8)) day = 1; //changing day of the month //formula for 30 day months if ((date < 31) && ( (month == 4) || (month == 6) || (month == 9) || (month == 11)) && (hour == 0) && (minute ==0)) { date++; } if ((date == 31) && ( (month == 4) || (month == 6) || (month == 9) || (month == 11)) && (hour == 0) && (minute ==0)) { date = 0; month++; } //formula for February if ( ((date < 29) && (month == 2) && (hour == 0) && (minute == 0) && (leap_year == 0)) || ((date < 30) && (month == 2) && (hour == 0) && (minute == 0) && (leap_year == 1)) ) { date = date +1; } if ( (date == 29) && (month == 2) && (hour == 0) && (minute == 0) && (leap_year == 0) || ((date == 30) && (month == 2) && (hour == 0) && (minute == 0) && (leap_year == 1)) ) { date = 0; month = 3; } //formula for 31 day months if ((date < 32) && ( (month == 1) || (month == 3) || (month ==5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (hour == 0) && (minute ==0)) { date++; } if ((date == 32) && ( (month == 1) || (month == 3) || (month ==5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (hour == 0) && (minute ==0)) { date = 1; if(month <12) month++; else month = 1; } //changing year if( (hour == 0) && (minute == 0) && (date == 1) && (month == 1)) year++; sensors.requestTemperatures(); // Send the command to get temperatures temp = sensors.getTempCByIndex(0); //assigning sensor's vale to the variable Serial.println(temp); logs = SD.open("logs.csv", FILE_WRITE); //assigning file to which we want to write the data if(logs) //if opening of the file was successful { if (number == 0) logs.println("sep=,"); //we add this line to the file so that excel 2013 and newer software knows that this is coma separated file //then we write entry number, date and sensor read logs.print(number); logs.print(","); logs.print(temp); //finally we add temperature read from sensor logs.print(","); if (hour < 10) logs.print("0"); //we add 0 is hour is lower than 10 logs.print(hour); logs.print(":"); if(minute <10) logs.print("0"); //we add 0 if minute is lower than 10 logs.print(minute); logs.print(" "); switch(day) //we add day of the week appropriately { case 1: logs.print("Monday"); break; case 2: logs.print("Tuesday"); break; case 3: logs.print("Wednesday"); break; case 4: logs.print("Thursday"); break; case 5: logs.print("Friday"); break; case 6: logs.print("Saturday"); break; case 7: logs.print("Sunday"); break; } logs.print(" "); logs.print(date); logs.print(" "); switch(month) { case 1: logs.print("January"); break; case 2: logs.print("February"); break; case 3: logs.print("March"); break; case 4: logs.print("April"); break; case 5: logs.print("May"); break; case 6: logs.print("June"); break; case 7: logs.print("July"); break; case 8: logs.print("August"); break; case 9: logs.print("September"); break; case 10: logs.print("October"); break; case 11: logs.print("November"); break; case 12: logs.print("December"); break; } logs.print(" "); logs.println(year); //printing year to the file Serial.println("Saving successful"); logs.close();//we close and save the file } logs.flush(); delay(300000); // we delay program for 5 minutes and then take another read //delay(60000); //for testing purposes 1 minute //delay(2000);//for testing purposes 2 seconds number = number + 1; // we add 1 to the entry counter }
Python script which allows communication between Arduino and Ubidots application:
from ubidots import ApiClient #importing ubidots api from time import * #importing standard time library import serial #importing pyserial library api = ApiClient ('your_account_key') #key of our ubidots account ser = serial.Serial('COM6', 9600, timeout = 0) #establishing communication with Arduino serial port temp_val = api.get_variable('your_variable_key') #ubidots variable's key with which we want to communicate while True: current_temp = ser.readline(6) #reading first 6 characters from serial port if ( (current_temp[:1] == '-')or(current_temp[:1] == '1') or (current_temp[:1] == '2') or (current_temp[:1] == '3') or (current_temp[:1] == '4') or (current_temp[:1] == '5') or (current_temp[:1] == '6') or (current_temp[:1] == '7') or (current_temp[:1] == '8') or (current_temp[:1] == '9') ): float_current_temp = float(current_temp[:5]) #converting value read from the serial port a float new_value = temp_temp.save_value({'value':float_current_temp}) #sending value to ubidots print ("sending successful")
Video explaining Arduino sketch and Python script, as well as providing introduction to Ubidots:
Please comment. Any feedback is very valuable for me, especially constructive criticism. If you like what I'm doing you can subscribe my channel on YouTube (https://www.youtube.com/user/W0j45), follow me on Twitter (https://twitter.com/milczarekw) and Instagram (https://www.instagram.com/milczarekw/).
Thank you for reading!
Top Comments