824
|
1 /*
|
|
2 note that the chip in this arduino has been replaced with a '328
|
|
3 */
|
|
4 #include "ST7565.h"
|
|
5 #include <OneWire.h>
|
|
6 #include <DallasTemperature.h>
|
|
7
|
|
8 #define BACKLIGHT_LED 10
|
|
9
|
|
10 ST7565 glcd(9, 8, 7, 6, 5);
|
|
11
|
|
12 OneWire oneWire(3); // digital IO 3
|
|
13 DallasTemperature sensors(&oneWire);
|
|
14 DeviceAddress tempSensorAddress;
|
|
15 #define NUM_TEMPERATURE_RETRIES 5
|
|
16
|
|
17 char newtxt[21*8+1];
|
|
18 unsigned int written;
|
|
19 unsigned char cmd;
|
|
20
|
|
21 #define GETHEADER -2
|
|
22 #define GETCOMMAND -1
|
|
23
|
|
24 void setup() {
|
|
25 Serial.begin(9600);
|
|
26 Serial.flush();
|
|
27
|
|
28 pinMode(11, INPUT); // low means door is closed
|
|
29 digitalWrite(11, HIGH);
|
|
30
|
|
31 pinMode(12, OUTPUT);
|
|
32 digitalWrite(12, LOW);
|
|
33
|
|
34 pinMode(BACKLIGHT_LED, OUTPUT);
|
|
35 analogWrite(BACKLIGHT_LED, 200);
|
|
36
|
|
37 glcd.st7565_init();
|
|
38 glcd.st7565_command(CMD_DISPLAY_ON);
|
|
39 glcd.st7565_command(CMD_SET_ALLPTS_NORMAL);
|
|
40 glcd.st7565_set_brightness(0x18);
|
|
41
|
|
42 glcd.display(); // show splashscreen
|
|
43
|
|
44 newtxt[21*8] = 0;
|
|
45 written = GETHEADER; // GETHEADER --recv 0xff--> GETCOMMAND --recv 0x00--> 0
|
|
46 }
|
|
47
|
|
48 void initSensors() {
|
|
49 sensors.begin();
|
|
50 sensors.getAddress(tempSensorAddress, 0);
|
|
51 sensors.setResolution(tempSensorAddress, 12);
|
|
52 }
|
|
53
|
|
54 void loop() {
|
|
55 /*
|
|
56 send 0xff 0x00,
|
|
57 then up to 21*8 bytes of text to replace the display, with a null terminator.
|
|
58
|
|
59 or 0xff 0x01, then get back '0\n' or '1\n' for the door sensor
|
|
60
|
|
61 or 0xff 0x02, then get back temperature followed by newline
|
|
62
|
|
63 or 0xff 0x03 then a byte for the screen brightness
|
|
64
|
|
65 or 0xff 0x0f then 00 or 01 to set the front lights on pin 12
|
|
66 */
|
|
67 float newTemp;
|
|
68 int i, printed;
|
|
69 int inb = Serial.read();
|
|
70 if (inb == -1) {
|
|
71 return;
|
|
72 }
|
|
73
|
|
74 if (written == GETHEADER) {
|
|
75 if (inb == 0xff) {
|
|
76 written = GETCOMMAND;
|
|
77 }
|
|
78 }
|
|
79 else if(written == GETCOMMAND) {
|
|
80 cmd = inb;
|
|
81
|
|
82 switch(cmd) {
|
|
83 case 0:
|
|
84 written = 0; // get chars below
|
|
85 return;
|
|
86
|
|
87 case 1:
|
|
88 Serial.print(digitalRead(11) ?
|
|
89 "{\"door\":\"open\"}\n" :
|
|
90 "{\"door\":\"closed\"}\n");
|
|
91 written = GETHEADER;
|
|
92 return;
|
|
93
|
|
94 case 2:
|
|
95
|
|
96 for (i=0; i<NUM_TEMPERATURE_RETRIES; i++) {
|
|
97
|
|
98 sensors.requestTemperatures();
|
|
99 newTemp = sensors.getTempF(tempSensorAddress);
|
|
100 if (i < NUM_TEMPERATURE_RETRIES-1 &&
|
|
101 (newTemp < -100 || newTemp > 180)) {
|
|
102 // too many errors that were fixed by restarting arduino.
|
|
103 // trying repeating this much init
|
|
104 initSensors();
|
|
105 continue;
|
|
106 }
|
|
107 Serial.print("{\"temp\":");
|
|
108 Serial.print(newTemp);
|
|
109 Serial.print(", \"retries\":");
|
|
110 Serial.print(i);
|
|
111 Serial.print("}\n");
|
|
112 break;
|
|
113 }
|
|
114 written = GETHEADER;
|
|
115 return;
|
|
116
|
|
117 case 3:
|
|
118 case 4:
|
|
119 written = 0; // these are handled below
|
|
120 return;
|
|
121
|
|
122 // otherwise take chars
|
|
123 }
|
|
124 }
|
|
125 else {
|
|
126 if (cmd == 3) {
|
|
127 analogWrite(BACKLIGHT_LED, 255 - inb);
|
|
128 written = GETHEADER;
|
|
129 return;
|
|
130 }
|
|
131 if (cmd == 4) {
|
|
132 digitalWrite(12, inb);
|
|
133 written = GETHEADER;
|
|
134 return;
|
|
135 }
|
|
136 newtxt[written] = inb;
|
|
137 written++;
|
|
138
|
|
139 if (inb == 0 || (written > 21 * 8)) {
|
|
140 glcd.clear();
|
|
141 glcd.drawstring(0,0, newtxt);
|
|
142 glcd.display();
|
|
143
|
|
144 written = GETHEADER;
|
|
145 return;
|
|
146 }
|
|
147 }
|
|
148 }
|
|
149
|
|
150
|
|
151
|
|
152
|
|
153
|
|
154
|