1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include <EthernetServer.h>
30 #include <EthernetClient.h>
32 #include <EthernetUdp.h>
35 const char *getResult(OCStackResult result);
37 #define PCF(str) ((const prog_char*)(F(str)))
38 #define dht11_pin 7 //Temperature & Humidity pin
40 const prog_char TAG[] PROGMEM = "THServer";
43 int gTHUnderObservation = 0;
45 void createTHResource();
46 typedef struct THRESOURCE {
47 OCResourceHandle handle;
54 String responsePayloadPut = "{\"oc\":[{\"href\":\"\",\"rep\":{\"temperature\":\"0\",\"humidity\":\"0\"}}]}";
55 String responsePayloadGet = "{\"oc\":[{\"href\":\"\",\"rep\":{\"temperature\":\"0\",\"humidity\":\"0\"}}]}";
65 while (!digitalRead(dht11_pin));
66 delayMicroseconds(30);
67 if (digitalRead(dht11_pin) != 0 )
69 while (digitalRead(dht11_pin));
75 String JsonGenerator(THResource TH){
76 String a = "{\"oc\":[{\"href\":\"\",\"rep\":{\"temperature\":\"";
77 String b = "\",\"humidity\":\"";
82 ss = a + TH.temp + b + TH.humid + c;
86 static uint8_t modCounter = 0;
87 void *ChangeTHRepresentation (void *param)
90 OCStackResult result = OC_STACK_ERROR;
93 if(modCounter % 10 == 0) // Matching the timing that the Linux Server App uses for the same functionality.
95 /* Temperature & Humidity value check */
97 byte i;// start condition
99 digitalWrite(dht11_pin, LOW);
101 digitalWrite(dht11_pin, HIGH);
102 delayMicroseconds(1);
104 pinMode(dht11_pin, INPUT);
105 delayMicroseconds(40);
107 if (digitalRead(dht11_pin))
109 Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
114 delayMicroseconds(80);
115 if (!digitalRead(dht11_pin))
117 Serial.println("dht11 start condition 2 not met"); // wait for second response signal:HIGH
121 delayMicroseconds(80); // now ready for data reception
123 { dht11_dat[i] = read_dht11_dat();} // recieved 40 bits data.
125 pinMode(dht11_pin, OUTPUT);
126 digitalWrite(dht11_pin, HIGH);
127 byte dht11_check_sum = dht11_dat[0]+dht11_dat[2]; // check check_sum
128 if(dht11_dat[4]!= dht11_check_sum)
130 Serial.println("DHT11 checksum error");
133 delay(2000); // fresh time
134 TH.humid = dht11_dat[0];
135 TH.temp = dht11_dat[2];
137 responsePayloadGet = JsonGenerator(TH);
140 if (gTHUnderObservation)
142 OC_LOG_V(INFO, TAG, " =====> Notifying stack of new humid level %d\n", TH.humid);
143 OC_LOG_V(INFO, TAG, " =====> Notifying stack of new temp level %d\n", TH.temp);
145 result = OCNotifyObservers (TH.handle);
147 if (OC_STACK_NO_OBSERVERS == result)
149 gTHUnderObservation = 0;
156 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest ) {
157 const char* typeOfMessage;
159 OCEntityHandlerResult ehRet = OC_EH_OK;
163 typeOfMessage = "OC_INIT_FLAG";
165 case OC_REQUEST_FLAG:
166 typeOfMessage = "OC_REQUEST_FLAG";
168 case OC_OBSERVE_FLAG:
169 typeOfMessage = "OC_OBSERVE_FLAG";
172 typeOfMessage = "UNKNOWN";
174 OC_LOG_V(INFO, TAG, "Receiving message type: %s", typeOfMessage);
175 if(entityHandlerRequest && flag == OC_REQUEST_FLAG) {
176 if(OC_REST_GET == entityHandlerRequest->method) {
178 int str_len = responsePayloadGet.length() + 1;
179 char charBuf[str_len+1];
181 responsePayloadGet.toCharArray(charBuf, str_len);
183 if(strlen(charBuf) < entityHandlerRequest->resJSONPayloadLen)
185 strncpy((char *)entityHandlerRequest->resJSONPayload, charBuf, entityHandlerRequest->resJSONPayloadLen);
190 if(OC_REST_PUT == entityHandlerRequest->method) {
192 int str_len1 = responsePayloadPut.length() + 1;
193 char charBuf1[str_len1];
195 responsePayloadPut.toCharArray(charBuf1, str_len1);
197 if(strlen(charBuf1) < entityHandlerRequest->resJSONPayloadLen)
199 strncpy((char *)entityHandlerRequest->resJSONPayload, charBuf1, entityHandlerRequest->resJSONPayloadLen);
204 } else if (entityHandlerRequest && flag == OC_OBSERVE_FLAG) {
205 gTHUnderObservation = 1;
211 /* The setup function is called once at startup of the sketch */
214 pinMode(dht11_pin, OUTPUT);
215 digitalWrite(dht11_pin, HIGH);
219 OC_LOG(DEBUG, TAG, PCF("THServer is starting..."));
220 uint16_t port = USE_RANDOM_PORT;
222 /* Mac address of my ethernet shield */
223 uint8_t ETHERNET_MAC[] = {0x90, 0xA2, 0xDA, 0x0E, 0xB8, 0xAB};
224 uint8_t error = Ethernet.begin(ETHERNET_MAC);
225 Serial.print(Ethernet.localIP());
228 OC_LOG_V(ERROR, TAG, "error is: %d", error);
232 if (OCInit(NULL, port, OC_SERVER) != OC_STACK_OK)
234 OC_LOG(ERROR, TAG, PCF("OCStack init error"));
239 * Declare and create the resource: TH
246 /* The loop function is called in an endless loop */
250 if (OCProcess() != OC_STACK_OK)
252 OC_LOG(ERROR, TAG, PCF("OCStack process error"));
255 ChangeTHRepresentation(NULL);
258 void createTHResource() {
262 OCStackResult res = OCCreateResource(&TH.handle,
263 "SoftSensorManager.Sensor",
265 "/Thing_TempHumSensor",
267 OC_DISCOVERABLE|OC_OBSERVABLE);
268 OC_LOG_V(INFO, TAG, "Created TH resource with result: %s", getResult(res));
271 const char *getResult(OCStackResult result) {
274 return "OC_STACK_OK";
275 case OC_STACK_INVALID_URI:
276 return "OC_STACK_INVALID_URI";
277 case OC_STACK_INVALID_QUERY:
278 return "OC_STACK_INVALID_QUERY";
279 case OC_STACK_INVALID_IP:
280 return "OC_STACK_INVALID_IP";
281 case OC_STACK_INVALID_PORT:
282 return "OC_STACK_INVALID_PORT";
283 case OC_STACK_INVALID_CALLBACK:
284 return "OC_STACK_INVALID_CALLBACK";
285 case OC_STACK_INVALID_METHOD:
286 return "OC_STACK_INVALID_METHOD";
287 case OC_STACK_NO_MEMORY:
288 return "OC_STACK_NO_MEMORY";
289 case OC_STACK_COMM_ERROR:
290 return "OC_STACK_COMM_ERROR";
291 case OC_STACK_INVALID_PARAM:
292 return "OC_STACK_INVALID_PARAM";
293 case OC_STACK_NOTIMPL:
294 return "OC_STACK_NOTIMPL";
295 case OC_STACK_NO_RESOURCE:
296 return "OC_STACK_NO_RESOURCE";
297 case OC_STACK_RESOURCE_ERROR:
298 return "OC_STACK_RESOURCE_ERROR";
299 case OC_STACK_SLOW_RESOURCE:
300 return "OC_STACK_SLOW_RESOURCE";
301 case OC_STACK_NO_OBSERVERS:
302 return "OC_STACK_NO_OBSERVERS";
304 return "OC_STACK_ERROR";