1
+ #include < ESP8266WiFi.h>
2
+ #include < PubSubClient.h>
3
+
4
+ // 아래에 있는 내용은 당연히 있어야한느 것이다!
5
+ #include < OneWire.h>
6
+ #include < DallasTemperature.h>
7
+ #define ONE_WIRE_BUS D3 // 몇번핀에 연결했냐
8
+ #define TEMPERATURE_PRECISION 12
9
+ OneWire oneWire (ONE_WIRE_BUS);
10
+ DallasTemperature sensors (&oneWire);
11
+
12
+ DeviceAddress myaddress1,myaddress2,myaddress3;
13
+ DeviceAddress mysensors[]={
14
+ {0x28 ,0x92 ,0x4B ,0x79 ,0xA2 ,0x00 ,0x03 ,0x38 },
15
+ {0x28 ,0xA9 ,0x4B ,0x95 ,0xF0 ,0x01 ,0x3C ,0x79 },
16
+ {0x28 ,0xE2 ,0xEA ,0x95 ,0xF0 ,0x01 ,0x3C ,0x0A }
17
+ };
18
+ // mysensors[0]
19
+ // mysensors[1]
20
+ // mysensors[2]
21
+
22
+ float mytemp[3 ] = {0 ,0 ,0 };
23
+
24
+ // Update these with values suitable for your network.
25
+
26
+ const char * ssid = " nockanda" ;
27
+ const char * password = " 11213144" ;
28
+ const char * mqtt_server = " broker.mqtt-dashboard.com" ;
29
+
30
+ WiFiClient espClient;
31
+ PubSubClient client (espClient);
32
+ unsigned long lastMsg = 0 ;
33
+ #define MSG_BUFFER_SIZE (50 )
34
+ char msg[MSG_BUFFER_SIZE];
35
+ int value = 0 ;
36
+
37
+ void setup_wifi () {
38
+
39
+ delay (10 );
40
+ // We start by connecting to a WiFi network
41
+ Serial.println ();
42
+ Serial.print (" Connecting to " );
43
+ Serial.println (ssid);
44
+
45
+ WiFi.mode (WIFI_STA);
46
+ WiFi.begin (ssid, password);
47
+
48
+ while (WiFi.status () != WL_CONNECTED) {
49
+ delay (500 );
50
+ Serial.print (" ." );
51
+ }
52
+
53
+ randomSeed (micros ());
54
+
55
+ Serial.println (" " );
56
+ Serial.println (" WiFi connected" );
57
+ Serial.println (" IP address: " );
58
+ Serial.println (WiFi.localIP ());
59
+ }
60
+
61
+ void callback (char * topic, byte* payload, unsigned int length) {
62
+ Serial.print (" Message arrived [" );
63
+ Serial.print (topic);
64
+ Serial.print (" ] " );
65
+ for (int i = 0 ; i < length; i++) {
66
+ Serial.print ((char )payload[i]);
67
+ }
68
+ Serial.println ();
69
+
70
+ // Switch on the LED if an 1 was received as first character
71
+ if ((char )payload[0 ] == ' 1' ) {
72
+ digitalWrite (BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
73
+ // but actually the LED is on; this is because
74
+ // it is active low on the ESP-01)
75
+ } else {
76
+ digitalWrite (BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
77
+ }
78
+
79
+ }
80
+
81
+ void reconnect () {
82
+ // Loop until we're reconnected
83
+ while (!client.connected ()) {
84
+ Serial.print (" Attempting MQTT connection..." );
85
+ // Create a random client ID
86
+ String clientId = " ESP8266Client-" ;
87
+ clientId += String (random (0xffff ), HEX);
88
+ // Attempt to connect
89
+ if (client.connect (clientId.c_str ())) {
90
+ Serial.println (" connected" );
91
+ // Once connected, publish an announcement...
92
+ // client.publish("outTopic", "hello world");
93
+ // ... and resubscribe
94
+ // client.subscribe("inTopic");
95
+ } else {
96
+ Serial.print (" failed, rc=" );
97
+ Serial.print (client.state ());
98
+ Serial.println (" try again in 5 seconds" );
99
+ // Wait 5 seconds before retrying
100
+ delay (5000 );
101
+ }
102
+ }
103
+ }
104
+
105
+ void setup () {
106
+ pinMode (BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
107
+ Serial.begin (115200 );
108
+ sensors.begin (); // 있어야한느거
109
+ if (sensors.getDeviceCount () == 3 ){
110
+ // 발견한 주소를 대입한다!
111
+ sensors.getAddress (myaddress1, 0 );
112
+ sensors.getAddress (myaddress2, 1 );
113
+ sensors.getAddress (myaddress3, 2 );
114
+
115
+ sensors.setResolution (myaddress1, TEMPERATURE_PRECISION);
116
+ sensors.setResolution (myaddress2, TEMPERATURE_PRECISION);
117
+ sensors.setResolution (myaddress3, TEMPERATURE_PRECISION);
118
+ }else {
119
+ // Serial.println("연결되어있지 않거나 2개이상의 센서가 연결되었다!");
120
+ }
121
+ setup_wifi ();
122
+ client.setServer (mqtt_server, 1883 );
123
+ client.setCallback (callback);
124
+ }
125
+
126
+ void loop () {
127
+
128
+ if (!client.connected ()) {
129
+ reconnect ();
130
+ }
131
+ client.loop ();
132
+
133
+ sensors.requestTemperatures ();
134
+ // Serial.println("DONE");
135
+
136
+ printTemperature (myaddress1);
137
+ printTemperature (myaddress2);
138
+ printTemperature (myaddress3);
139
+
140
+ String myjson = " {\" temp1\" :" +String (mytemp[0 ])+" ,\" temp2\" :" +String (mytemp[1 ])+" ,\" temp3\" :" +String (mytemp[2 ])+" }" ;
141
+
142
+ client.publish (" nockanda/sensors" , myjson.c_str ());
143
+ }
144
+
145
+ void printTemperature (DeviceAddress deviceAddress)
146
+ {
147
+ float tempC = sensors.getTempC (deviceAddress);
148
+ if (tempC == DEVICE_DISCONNECTED_C)
149
+ {
150
+ Serial.println (" Error: Could not read temperature data" );
151
+ return ;
152
+ }
153
+
154
+ // 지금 입력받은 주소로 온도값을 측정하는데 누구껀지 확인해보자!
155
+ for (int i =0 ;i<3 ;i++){
156
+ if (is_match (deviceAddress,mysensors[i])){
157
+ // match
158
+ // Serial.print(i+1);
159
+ // Serial.println("번째 센서입니다!");
160
+ mytemp[i] = tempC;
161
+ break ;
162
+ }
163
+ }
164
+
165
+ // Serial.print("Temp C: ");
166
+ // Serial.println(tempC);
167
+ }
168
+
169
+ // 주소2개를 입력받아서 같은지 아닌지를 반환하는 함수!
170
+ bool is_match (DeviceAddress da1, DeviceAddress da2){
171
+ // 일단 같다고 보고 시작한다!
172
+ bool result = true ;
173
+ for (int i = 0 ;i<8 ;i++){
174
+ // 단 하나라도 틀리면 틀린것이다!
175
+ if (da1[i] != da2[i]){
176
+ result = false ;
177
+ break ;
178
+ }
179
+ }
180
+
181
+ return result;
182
+ }
0 commit comments