-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc3200-launchpad-energi-friskby.ino
238 lines (189 loc) · 6.44 KB
/
cc3200-launchpad-energi-friskby.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
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
#include <WiFi.h>
#include <Wire.h>
#include "Adafruit_TMP006.h"
/**************** Configuration ****************/
// Seconds between readings/postings
#define POSTING_INTERVAL_MSEC 20000
// Milliseconds to wait for server-reply. Set to 0 when not debugging.
#define WAIT_FOR_REPLY_MSEC 400
// Voltage divider factor
#define ADC_VOLT_DIV_FACTOR 5
// Misc. Probably don't touch these
#define MAX_VALUE_STRING_LEN 20
#define MAX_MESSAGE_BODY_SIZE 600
#define HTTP_PORT 80
#define CC3200_ADC_REF_MV 1400 // From TI CC3200 Launchpad SDK ADC example code
#define CC3200_ADC_MAX_READING 4095
// Network info
char ssid[] = "Nyskapingsparken";
char password[] = "";
// Friskby server, name or IP
const char server[] = "friskby.herokuapp.com";
//IPAddress server(192,168,1,42);
// Sensor struct - do not change
union Reading {
int asInt;
float asFloat;
};
struct sensor {
char* sensorId;
char* apiKey;
Reading reading;
void (*initSensor)();
void (*readSensor)(); // Note! readSensor() must populate valueStr[]
char valueStr[MAX_VALUE_STRING_LEN]; // Value as a string (will be used in msg payload)
};
// Sensors - Add new sensors below and update NUM_SENSORS
// Examples
// sensor sensorName = {"SensorIDfromFriskBy", "APIKeyFromFriskBy", 0.0f, nameOfFunctionDoingInit, nameOfFunctionDoingReading, {0}};
sensor dieTemp = {"ARUtest", "d6b065e8-42a7-4bd8-90bc-df939d91de99", 0.0f, initDieTempSensor, readDieTempSensor, {0}};
sensor dust = {"ARUdustTest", "70aaa8c4-db8f-45d7-a713-e6add4d0d52d", 0.0f, initDustSensor, readDustSensor, {0}};
#define NUM_SENSORS 2
// Add sensor-pointer to listOfSensors
sensor* listOfSensors[NUM_SENSORS] = {&dieTemp, &dust};
// Sensor-specific config
#define TEMP_I2C_ADDRESS 0x41
#define DUST_SENSOR_ADC_PIN 6 // ADC Channel 2, Pin 59 on board (ADC Ch.1 (pin 58) gave unstable readings)
#define DUST_SENSOR_DIG_PIN 5 // Pin 61 on board
#define DUST_SENSOR_SAMPLING_TIME 280 // From http://www.dfrobot.com/wiki/index.php/Sharp_GP2Y1010AU
#define DUST_SENSOR_DELTA_TIME 40
/**************** End of configuration ****************/
// Data structures
unsigned long lastConnectionTime = 0;
WiFiClient client;
char timestamp[] = "2015-11-10T22:32:00Z";
Adafruit_TMP006 tmp006(TEMP_I2C_ADDRESS);
void setup() {
// Initialize serial
Serial.begin(115200);
// Connect to WPA/WPA2 network.
Serial.print("Connecting to network: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Wait for connection
while ( WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("\nConnected");
Serial.println("Waiting for IP address");
// Wait for IP
while (WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(1000);
}
printWifiStatus();
// Connected. Init sensors.
for(int i = 0; i < NUM_SENSORS; i++) {
listOfSensors[i]->initSensor();
}
}
void loop() {
// Read sensors
for(int i = 0; i < NUM_SENSORS; i++) {
listOfSensors[i]->readSensor();
}
// Send data to server
postToServer();
// Note! There is additional sleeping in sensor readings and server reply
sleep(POSTING_INTERVAL_MSEC);
}
void initDieTempSensor() {
// Init communication with temp sensor
if (!tmp006.begin()) {
Serial.println("Temp sensor not found");
while(1);
}
}
void readDieTempSensor() {
tmp006.wake();
dieTemp.reading.asFloat = tmp006.readDieTempC();
// Sleep caused bad readings. But we are not worried about power consumption atm
// TODO Dig into datasheets, probably need some delay after wake()
// tmp006.sleep();
// Create string of the float value (sprintf with float not supported(and will cause undefined behavior))
memset(dieTemp.valueStr, 0, MAX_VALUE_STRING_LEN);
int intPart = (int)dieTemp.reading.asFloat;
int fractPart = (dieTemp.reading.asFloat * 100) - (intPart * 100);
sprintf(dieTemp.valueStr,"%d.%02d", intPart, fractPart);
Serial.print("DieTemp-sensor reading: ");
Serial.println(dieTemp.valueStr);
}
void initDustSensor() {
pinMode(DUST_SENSOR_DIG_PIN, OUTPUT);
}
void readDustSensor() {
// Turn on LED
digitalWrite(DUST_SENSOR_DIG_PIN, LOW);
sleep(DUST_SENSOR_SAMPLING_TIME);
// Take reading
// Raw ADC value
dust.reading.asInt = analogRead(DUST_SENSOR_ADC_PIN) * ADC_VOLT_DIV_FACTOR;
// Millivolt
//dust.reading.asInt = ((analogRead(DUST_SENSOR_ADC_PIN) * CC3200_ADC_REF_MV * ADC_VOLT_DIV_FACTOR) / CC3200_ADC_MAX_READING);
sleep(DUST_SENSOR_DELTA_TIME);
// Turn off LED
digitalWrite(DUST_SENSOR_DIG_PIN, HIGH);
// Create string
memset(dust.valueStr, 0, MAX_VALUE_STRING_LEN);
sprintf(dust.valueStr, "%d", dust.reading.asInt);
Serial.print("Dust-sensor reading: ");
Serial.print(dust.valueStr);
Serial.println("");
}
void postToServer() {
// If connection successful
if (client.connect(server, HTTP_PORT)) {
// Iterate sensors and add to message body
for(int i = 0; i < NUM_SENSORS; i++) {
char msgBody[MAX_MESSAGE_BODY_SIZE] = {0};
sprintf(
msgBody,
"{\"sensorid\":\"%s\",\"timestamp\":\"%s\",\"value\":%s,\"key\":\"%s\"}",
listOfSensors[i]->sensorId, timestamp, listOfSensors[i]->valueStr, listOfSensors[i]->apiKey
);
Serial.print("Sending Message for sensor ");
Serial.print(listOfSensors[i]->sensorId);
Serial.print("\nBody: ");
Serial.println(msgBody);
Serial.print("Message length: ");
Serial.println(strlen(msgBody));
// Send request
client.println("POST /sensor/api/reading/ HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("User-Agent: Energia/1.1");
client.print("Content-length:");
client.println(strlen(msgBody));
client.println("Content-type: application/json\r\n");
client.print(msgBody);
// Wait for reply and print it
if(WAIT_FOR_REPLY_MSEC != 0) {
delay(WAIT_FOR_REPLY_MSEC);
Serial.println("Reply received:");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
}
}
// Close connection
client.stop();
// Note the time that the connection was made
lastConnectionTime = millis();
}
else {
Serial.println("connection failed");
}
}
void printWifiStatus() {
// Print IP address
IPAddress ip = WiFi.localIP();
Serial.print("\nIP Address: ");
Serial.println(ip);
// Print RSSI
long rssi = WiFi.RSSI();
Serial.print("RSSI: ");
Serial.print(rssi);
Serial.println(" dBm");
}