-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoArduino.ino
195 lines (163 loc) · 4.56 KB
/
CoArduino.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
University of Parma - Italy
This file is part of ArduinoCoAP
ArduinoCoAP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author:
Daniele Sportillo (daniele.sportillo@gmail.com)
*/
#include "option.h"
#include "options.h"
#include "coapmessage.h"
#include "config.h"
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetBonjour.h>
#include <dht.h>
#define DEBUG
#ifdef DEBUG
#include <MemoryFree.h>
#endif
#define DHT22_PIN 7
EthernetUDP Udp;
dht DHT;
char *ftoa(char *a, double f, int precision) {
int p[] = {0,10,100};
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}
//I DON'T CARE ABOUT ACK
/*void sendAck(CoapMessage* msg) {
if(msg->getType()!=0) return;
Serial.print("\n====ACK SENT====\n");
CoapMessage* ack = new CoapMessage(2, 0, msg->getToken(), "");
//ack->toString();
ack->send(Udp, msg->getRemoteIPAddr(), msg->getRemotePort());
delete ack;
}*/
void onReceivedMessage(CoapMessage* msg) {
//sendAck(msg);
#ifdef DEBUG
Serial.println("\n====MESSAGE RECEIVED=====\n");
msg->toString();
#endif
byte responseCode;
double value;
int chk = DHT.read22(DHT22_PIN);
if(msg->isRequest()) {
char* reqService = msg->getOptions()->getOptionByNumber(11)->getValue();
if(strcmp(reqService,"temp")==0)
value = DHT.temperature;
else if(strcmp(reqService,"humidity")==0)
value = DHT.humidity;
switch(msg->getCode()) {
case 1 :
responseCode = 69;
break;
case 2 :
responseCode = 65;
break;
case 3 :
responseCode = 134;
break;
case 4 :
responseCode = 134;
break;
default:
responseCode = 134;
}
char val[6];
ftoa(val,value, 2);
CoapMessage* response = new CoapMessage(2, responseCode, msg->getToken(), val);
#ifdef DEBUG
response->toString();
#endif
response->send(Udp, msg->getRemoteIPAddr(), msg->getRemotePort());
#ifdef DEBUG
Serial.print("freeMemory()=");
Serial.println(freeMemory());
#endif
if(response != NULL){
delete response;
response = NULL;
}
if(reqService != NULL){
free(reqService);
reqService = NULL;
}
}
}
void setup() {
Serial.begin(9600);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 102);
int localPort = 8888;
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
EthernetBonjour.begin();
EthernetBonjour.addServiceRecord("Temp Coap Sensor._coap", localPort, MDNSServiceUDP, "\xApath=/temp");
EthernetBonjour.addServiceRecord("Humidity Coap Sensor._coap", localPort, MDNSServiceUDP, "\xEpath=/humidity");
Serial.println("Bonjourned!");
//IF REGISTRATION BY PUT
/*CoapMessage* init = new CoapMessage(1, 3, "temp");
init->toString();
init->send(Udp, server, 4000);
delete init;
init = NULL;*/
}
void loop() {
EthernetBonjour.run();
byte packetSize = Udp.parsePacket();
if(packetSize) {
char* packetBuffer = (char*)malloc(packetSize);
#ifdef DEBUG
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
Serial.print(".");
}
Serial.print(", port: ");
Serial.println(Udp.remotePort());
#endif
Udp.read(packetBuffer,packetSize);
CoapMessage* msg = new CoapMessage(packetBuffer, packetSize);
msg->setRemoteSocketAddress(Udp.remoteIP(), Udp.remotePort());
onReceivedMessage(msg);
Serial.print("freeMemory()=");
Serial.println(freeMemory());
if(packetBuffer != NULL) {
free(packetBuffer);
packetBuffer = NULL;
}
delete msg;
msg = NULL;
Serial.print("freeMemory()=");
Serial.println(freeMemory());
#ifdef DEBUG
Serial.println("\nDeleted\n");
#endif
}
delay(10);
}