-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDs_FakeFinder.ino
More file actions
313 lines (248 loc) · 8.98 KB
/
Ds_FakeFinder.ino
File metadata and controls
313 lines (248 loc) · 8.98 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Parts of this code I found on the internet, other parts I wrote myself. It provided as is.
It works fine for me.
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#define ONE_WIRE_BUS 0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
//display
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display
// reset pin
Adafruit_SSD1306 display(-1);
uint8_t deviceCount = 0;
const uint8_t PARASITE = 0;
uint8_t highAlarmValue = 25;
uint8_t lowAlarmValue = 10;
uint32_t timeCon = 0;
uint8_t resol = 12;
uint32_t millChngRes = 0;
void setup(void)
{
Serial.begin(19200);
Wire.begin(5, 14);
while (!Serial);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(22,10); // Start at top-left corner
display.println("DS18B20");
display.setCursor(5,40); // Start at top-left corner
display.println("FakeFinder");
display.display();
delay(1700);
// ============== find all i2c adresses
Serial.println("====================================");
Serial.println(F("Finding all i2c adresses !"));
byte error, address;
int nDevices;
nDevices = 0;
for (address = 1; address < 127; address++ ) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.println("-");
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
} else if (error == 4) {
Serial.println("-");
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
} //for loop
if (nDevices == 0){
Serial.println("No I2C devices found");
}
delay(100);
// ============== i2c test end.
Serial.print("DallasTemperature library version: ");
Serial.println(DALLASTEMPLIBVERSION);
}// setup end
void loop(void){
if(resol==13){
resol=9;
}
sensors.begin();
//deviceCount = sensors.getDS18Count();
deviceCount = sensors.getDeviceCount();
Serial.println("");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Dev.#: " + String(deviceCount));
display.setCursor(0,11);
display.println(SetResolution(resol));
display.setCursor(0,22);
delay(50);
uint32_t startMillis = millis();
sensors.requestTemperatures(); // Request temperature mesurments
timeCon = millis()-startMillis; // calculate time
Serial.println("************************************************************");
Serial.print("TimeCon:");
Serial.print(timeCon);
Serial.println(" ms");
Serial.println("************************************************************");
delay(50);
display.println("SET alarm: " + method2());
display.setCursor(0,33);
display.println("GOT alarm: " + readDevicesMethod2());
display.setCursor(0,44);
display.println("TEMP: " + String(sensors.getTempCByIndex(0)) + " C");
display.setCursor(0,55);
display.println("TimeCon: " + String(timeCon) + " ms");
display.display();
Serial.println("");
delay(50);
// change resolution every xx sec
if (millis() > millChngRes + 5000 && millis()>12000){
resol++;
millChngRes = millis();
}
}// === loop end
//================================================
String method2(){ // using direct OneWire write commands
String msg;
Serial.println("============================================================");
Serial.print("Setting New Hi/Lo alarm values: ");
highAlarmValue++;
lowAlarmValue++;
Serial.print(highAlarmValue);
Serial.print("/");
Serial.println(lowAlarmValue);
Serial.println("Writing to devices");
oneWire.reset_search();
uint8_t addr[8];
while (oneWire.search(addr)){
if (OneWire::crc8(addr, 7) == addr[7]){
Serial.print(".");
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x4E); // Write to scratchpad
oneWire.write(highAlarmValue); // Write high alarm value
oneWire.write(lowAlarmValue); // Write low alarm value
oneWire.write(0x7F); // Write configuration register, 12 bit temp res
delay(30); // dallas temp lib doesn't delay
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x48, PARASITE); //copy scratchpad to eeprom, 1 - parasite power
delay(20); // need at least 10ms eeprom write delay
if (PARASITE) delay(10);
msg = String(highAlarmValue) + " / " + String(lowAlarmValue);
} else {
Serial.println("Bad device addr!");
msg = "Bad device addr!";
}
}
Serial.println("Done");
Serial.println("------------------------------------------------------------");
return msg;
}
//================================================
//================================================
String readDevicesMethod2(){ // using direct OneWire write commands
Serial.println("Reading...");
uint8_t hAlarmValue;
uint8_t lAlarmValue;
uint8_t addr[8];
uint8_t data[9];
String msg;
oneWire.reset_search();
while (oneWire.search(addr)){
printAddress(addr);
if (OneWire::crc8(addr, 7) == addr[7]){
oneWire.reset();
oneWire.select(addr);
oneWire.write(0xB8); // Copy eeprom to scratchpad cmd, missing from DallasTemperature library
delay(50);
oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // Read scratchpad cmd
delay(50);
for (uint8_t i = 0; i < 9; i++){
data[i] = 0;
data[i] = oneWire.read();
}
hAlarmValue = data[2]; // byte 2 is high temp alarm
lAlarmValue = data[3]; // byte 3 is low temp alarm
Serial.print("Hi/Lo now is: ");
Serial.print(hAlarmValue);
Serial.print("/");
Serial.println(lAlarmValue);
msg = String(hAlarmValue) + " / " + String(lAlarmValue);
} else {
Serial.println("Bad device addr!");
msg = "Bad device addr!";
}
}
Serial.println("============================================================");
return msg;
}
//================================================
//================================================
// Loop through each device, print out address
String SetResolution(uint8_t resSet){
String msg;
for(int i=0;i<deviceCount; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{ Serial.println("");
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(resSet, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, resSet);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
msg += "SetTo/Real: ";
msg +=String(resSet, DEC);
msg += " / ";
msg += String(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
msg = "Err.";
}
}
return msg;
}
//================================================
//================================================
void printAddress(DeviceAddress deviceAddress){
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress[i] < 16) Serial.print("0"); // zero pad the address if necessary
Serial.print(deviceAddress[i], HEX);
if (i < 7) Serial.print(":");
}
Serial.println(" | " + String(sensors.getTempC(deviceAddress))+ " C");
}
//================================================