forked from hephaestus9/Power_Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmpAndRHT_Serial.ino
163 lines (145 loc) · 4.16 KB
/
AmpAndRHT_Serial.ino
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
#define DEBUG true
#include <DHT22.h>
#define DHT22_PIN 2
#define LM358N1_PIN 0
#define LM358N2_PIN 1
#define ledPin 13
int maxReadingLM358N1 = 0;
int maxReadingLM358N2 = 0;
int currReadingLM358N1 = 0;
int currReadingLM358N2 = 0;
float main1 = 0.0;
float main2 = 0.0;
float comb = 0.0;
int ledState = LOW;
int packetSent = false;
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
void setup(){
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(ledPin, OUTPUT);
}
void loop(){
//----------------Amperage--------------------------------------------------
//Reset values for new packet
if (packetSent){
packetSent = false;
maxReadingLM358N1 = 0;
maxReadingLM358N2 = 0;
}
int i;
for (i=0; i<250; i++) {
// Get ADC reading for both sensors
currReadingLM358N1 = analogRead(LM358N1_PIN);
currReadingLM358N2 = analogRead(LM358N2_PIN);
if (currReadingLM358N1 > maxReadingLM358N1) {
maxReadingLM358N1 = currReadingLM358N1;
}
if (currReadingLM358N2 > maxReadingLM358N2) {
maxReadingLM358N2 = currReadingLM358N2;
}
}
// \/ -- Arduino AREF \/--------Burden Resistance
main1 = (maxReadingLM358N1 * 5 * 3100.0) / (1023.0 * sqrt(2) * 150);
main2 = (maxReadingLM358N2 * 5 * 3100.0) / (1023.0 * sqrt(2) * 150);
comb = main1 + main2;
//-----------------------------------------------------------------------------
//---------------------RHT-----------------------------------------------------
DHT22_ERROR_t errorCode;
float temperature = 0;
float tempF = 0;
float humidity = 0;
DHT22_ERROR_t errorCode2;
float temperature2 = 0;
float tempF2 = 0;
float humidity2 = 0;
// The sensor can only be read from every 1-2s, and requires a minimum
// 2s warm-up after power-on.
delay(2000);
errorCode = myDHT22.readData();
temperature = myDHT22.getTemperatureC();
humidity = myDHT22.getHumidity();
tempF = (temperature*1.8)+32;
#if DEBUG
Serial.println("Temp: ");
Serial.println(tempF);
Serial.println("Humidity: ");
Serial.println(humidity);
Serial.println();
Serial.println("Raw Reading");
Serial.println("Main 1: ");
Serial.println(maxReadingLM358N1);
Serial.println("Main 2: ");
Serial.println(maxReadingLM358N2);
Serial.println();
Serial.println("Calculated Amperage");
Serial.println("Main 1: ");
Serial.println(main1);
Serial.println("Main 2: ");
Serial.println(main2);
Serial.println("Combined: ");
Serial.println(comb);
Serial.println("#################################");
packetSent = true;
#endif
Serial.print(tempF);
Serial.print(":");
Serial.print(humidity);
Serial.print(":");
Serial.print(main1);
Serial.print(":");
Serial.print(main2);
Serial.print(":");
Serial.print(comb);
Serial.println();
ledState = HIGH;
digitalWrite(ledPin, ledState);
delay(5);
ledState = LOW;
digitalWrite(ledPin, ledState);
ledState = HIGH;
digitalWrite(ledPin, ledState);
delay(5);
ledState = LOW;
digitalWrite(ledPin, ledState);
ledState = HIGH;
digitalWrite(ledPin, ledState);
delay(5);
ledState = LOW;
digitalWrite(ledPin, ledState);
packetSent = true;
switch(errorCode)
{
case DHT_ERROR_NONE:
temperature = myDHT22.getTemperatureC();
humidity = myDHT22.getHumidity();
break;
case DHT_ERROR_CHECKSUM:
temperature = 100;
humidity = 100;
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.println("RHT03 Polled to quick ");
break;
}
//---------------------------------------------------------------------------
}