For no particular reason / Geen speciale reden

Meccano for Electronics

What Meccano is for mechanics is Arduino for electronics. The nice thing is that you can combine the two, mechanical actions triggered through electronics.
This first project is a topic which has a real need. It is to switch a heater on in a hot house I have in my garden.
The technique is nothing fancy, a digital thermometer sensor and a relais that switches the heater within a temperature range.
Extra is a humidity sensor, visibility is through a led display that shows alternatively the temperature and humidity.

Arduino Nano

The processor is a Arduino Nano clone, the headers were not connected and which I soldered myself. (with YouTube assistance).
The small breadboards are quite usable with a nano, you can place them on it and can use the ports and voltage connectors quite easily.

The humidity sensor and relais

The humidity sensor is a DHT11 type, which is of sufficient precision for this purpose. The temperature sensor is also quite standard. I may replace this one with one which I  can place outside the enclosure to have more accurate measurements. The heater is 2000 watt which I can switch using the relays.

The temperature sensor

 

Temperature reading
Humidity reading

The LED panels are nice. The characters you can display are a bit limited but sufficient for this purpose. I have extended the library a bit with settings for the decimal separator which I could not find elsewhere.

Programming the Arduino is actually easy. There are so many examples available that applications combining the examples is easy.

What needs to improve is making the connections work, I switched to single core copper of 0.8 mm, with some effort you can make DuPont connections with this type of wire alternatively you can also put them directly in the breadboard an advantages are that the connection is lower and you can create them in exact length.

I’m also practicing creating DuPont connections using a crimping tool. Not over complicated but you have to make them real precise to have good and stable connections, this is especially true when making a three or two fold connector. The shield really needs to click kin place smoothly if not it is nor good. Female connectors are more difficult to make versus the male type. I also came across a component that was too loose in a connector and kept falling out.

Soldering also needs to improve which can only be achieved through practice. Soldering header pins to boards is well possible but needs more practice and perhaps a better soldering iron.

Below all the code I needed to get the readings on the display or on the serial monitor.

Incomplete schematic drawing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// *****************************************************
// v1.2
// 17-09 modified reset and display for < 10
// JN Griep
// v1.1 Introduced timers
// 04-september-2017
// v1.0 initial release
// augustus -2017
// Temperatuur en vochtigheidsmeting
// For arduino Uno or Nano HardWare
// Purpose heating of a hothouse.
// *****************************************************
// Libraries to use
//******************************************************
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TM74HC595Display.h>
#include <TimerOne.h>
#include <DHT.h>
#include <elapsedMillis.h>

//*********** Timers window*******************
elapsedMillis TimeElapsedDisplay;
elapsedMillis TimeElapsedDatabase;
// ***********Temperature *********************
// Data wire is plugged into pin 2 on the Arduino (a 4.7K resistor is necessary)
#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);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//************Humidity ***********************
#define DHTTYPE DHT11
#define DHTPIN 4
DHT dht(DHTPIN, DHTTYPE);
//*************Relay to switch the heater*****
int Relay = 12;
// ************pins and instance for the display************
int SCLK = 7;
int RCLK = 6;
int DIO = 5;
TM74HC595Display disp(SCLK, RCLK, DIO);
//*************pins for the control led ********************
int RedPin = 11; // pin for red signal
int GreenPin = 10; // pin for green signal
// *************Variables *********************************
long IntervalDisplay = 5000;
long IntervalDatabase = 1000;
unsigned char LED_0F[24];
unsigned char LED_DISPLAY[7];
String ToDisplayTemp; //for display
String ToDisplayHumidity; //for display
float Temp;
float Humidity;
boolean ShowTemp = true;
//
// ***************************Test button
const int Button = 8;

 

void setup()
{
LED_0F[0] = 0xC0; //0
LED_0F[1] = 0xF9; //1
LED_0F[2] = 0xA4; //2
LED_0F[3] = 0xB0; //3
LED_0F[4] = 0x99; //4
LED_0F[5] = 0x92; //5
LED_0F[6] = 0x82; //6
LED_0F[7] = 0xF8; //7
LED_0F[8] = 0x80; //8
LED_0F[9] = 0x90; //9
LED_0F[10] = 0x40; //0.
LED_0F[11] = 0x79; //1.
LED_0F[12] = 0x24; //2.
LED_0F[13] = 0x30; //3.
LED_0F[14] = 0x19; //4.
LED_0F[15] = 0x12; //5.
LED_0F[16] = 0x02; //6.
LED_0F[17] = 0x78; //7.
LED_0F[18] = 0x00; //8.
LED_0F[19] = 0x10; //9.
LED_0F[20] = 0xC6; //C
LED_0F[21] = 0xBF; // - sign
LED_0F[22] = 0x89; //H
LED_0F[23] = 0xC1; //U

Serial.begin(9600); //Begin serial communication
sensors.begin(); //Initialize temperature
dht.begin(); //Initialize humidity
// dht.setup(4); // data pin 4
pinMode (Relay, OUTPUT); //pin for relay
digitalWrite (Relay,HIGH); // Switch it on to start
pinMode (RedPin,OUTPUT); // pin for red signal
pinMode (GreenPin,OUTPUT); // pin for green signal
pinMode (Button, INPUT_PULLUP);
digitalWrite (RedPin, HIGH); // Put Red led high to align with relay
//*******************Needed for Display**************
Timer1.initialize(1500); // set a timer of length 1500
Timer1.attachInterrupt(timerIsr); // attach the service routine here
}

void timerIsr()
{
disp.timerIsr(); //Display use
}

void loop() {

//******************** Read Humidity
Humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(Humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//******************* Get temperature
sensors.requestTemperatures();
Temp = (sensors.getTempCByIndex(0));// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
// ******************Debug only
// Serial.print("Temperature : \t");
// Serial.print( sensors.getTempCByIndex(0));
// Serial.print(" Humidity (%): \t");
// Serial.println(Humidity,0);

ToDisplayTemp = Temp;
ToDisplayHumidity = Humidity;

if (TimeElapsedDatabase > IntervalDatabase)
{
TimeElapsedDatabase = 0;
Serial.println( ToDisplayTemp + "," + ToDisplayHumidity);
}
if (Temp < 10.0)
{
LED_DISPLAY[0] = 0;
LED_DISPLAY[1] = ToDisplayTemp.substring (0,1).toInt()+10;
LED_DISPLAY[2] = ToDisplayTemp.substring (1,2).toInt();
LED_DISPLAY[3] = ToDisplayTemp.substring (2,3).toInt();
LED_DISPLAY[4] = ToDisplayTemp.substring (3,4).toInt();
}
else
{
LED_DISPLAY[0] = ToDisplayTemp.substring (0,1).toInt();
LED_DISPLAY[1] = ToDisplayTemp.substring (1,2).toInt()+10;
LED_DISPLAY[2] = ToDisplayTemp.substring (2,3).toInt();
LED_DISPLAY[3] = ToDisplayTemp.substring (3,4).toInt();
LED_DISPLAY[4] = ToDisplayTemp.substring (4,5).toInt();
}
LED_DISPLAY[5] = ToDisplayHumidity.substring (0,1).toInt();
LED_DISPLAY[6] = ToDisplayHumidity.substring (1,2).toInt();

if (TimeElapsedDisplay > IntervalDisplay)
{
TimeElapsedDisplay = 0;
switch (ShowTemp)
{
case true:
{
disp.clear();
disp.set(LED_0F[LED_DISPLAY[0]], 3); //send first character array to 1st indicator
disp.set(LED_0F[LED_DISPLAY[1]], 2); //send second character array to 2nd indicator including the decimal separator
disp.set(LED_0F[LED_DISPLAY[3]], 1); //send fourth character array to 3rd indicator
disp.set(LED_0F[20], 0); //send simbol "C" to 4rd indicators show_t = true;
ShowTemp = false;
break;
}
case false:
{
disp.clear();
disp.set(LED_0F[22], 3); //send simbol "H" to 1st indicators
disp.set(LED_0F[23], 2); //send simbol "U" to 2nd indicator
disp.set(LED_0F[LED_DISPLAY[5]], 1); //send first character array to 3rd indicator
disp.set(LED_0F[LED_DISPLAY[6]], 0); //send second character array to 4th indicator including the decimal separator
ShowTemp = true;
break;
}
}
}

// Do a reset when buttonState == LOW whatever the relay status or led is
//If button pressed...
if (digitalRead(Button) == LOW && digitalRead(Relay) == LOW)
{
digitalWrite(Relay,HIGH);
digitalWrite(RedPin, HIGH);
digitalWrite(GreenPin, LOW);
// Have it run for 5 seconds then continue
delay (5000);
digitalWrite(Relay,LOW);
digitalWrite(RedPin, LOW);
digitalWrite(GreenPin, HIGH);
}

else if (digitalRead(Button) == LOW && digitalRead(Relay) == HIGH)
{
digitalWrite(Relay,LOW);
digitalWrite(RedPin, LOW);
digitalWrite(GreenPin, HIGH);
//// Have it run for 5 seconds then continue
delay (5000);
digitalWrite(Relay,HIGH);
digitalWrite(RedPin, HIGH);
digitalWrite(GreenPin, LOW);
}

if (Temp < 9.0)
{

if (digitalRead (Relay) == LOW)
{
digitalWrite(Relay,HIGH);
//Serial.println("Switch it on");
}
if (digitalRead(RedPin == LOW && digitalRead (Relay) == HIGH))
{
digitalWrite(RedPin, HIGH);
digitalWrite(GreenPin, LOW);
}
}
if (Temp > 11.0)
{
if (digitalRead (Relay) == HIGH)
{
digitalWrite(Relay,LOW);
// Serial.println("Switch it off");
}
if (digitalRead(RedPin) == HIGH && digitalRead (Relay) == LOW)
{
digitalWrite(RedPin, LOW);
digitalWrite(GreenPin, HIGH);
}
}
}

 

Cron script that disables hangup on the serial port and launches the python script to do the database updates.

 

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
#
# Disable hangup
#
stty -F /dev/ttyUSB0 -hupcl
#
cd /home/pi
#
# start the Python script
python /home/pi/weatherstation.py
#

Python script to insert records in a mysql database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/python
# filename: weatherstation.py
# a script to insert weatherdata into a MySQL table
# get the current date
import MySQLdb
import datetime
import serial
bufsize = 0
ser = serial.Serial('/dev/ttyUSB0', 9600)

db = MySQLdb.connect(host="radio.local", user="XXXX", passwd="XXXXXXX", db="XX")

cursor = db.cursor()
add_temp = ("INSERT INTO temperatuur(date, temperatuur, humidity) VALUES (%s,%s,
%s)"
)

while True :
now = datetime.datetime.now()
data = (ser.readline())
data_temp = (now, data[:5], data[6:])
cursor.execute(add_temp,data_temp)
db.commit()

cursor.close()
db.close()