forked from HannesOberreiter/bScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piotr_SHTC3.ino
376 lines (310 loc) · 10.8 KB
/
piotr_SHTC3.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <SoftwareSerial.h>
#include <avr/sleep.h>
#include "HX711.h"
//#include "DHT.h"
#include <Arduino.h>
#include "SHTC3.h"
//Connection PINS to GSM Shield, change if needed
SoftwareSerial mySerial(10, 11);
//Url to btree API, change if you use your own
const char URL[] PROGMEM = "www.xxxxxxx.pl";
const char API[] PROGMEM = "/send_data_ident.php?";
//Resquestb.in Testing Request
//const char URL[] PROGMEM = "requestb.in";
//const char API[] PROGMEM = "YOU_REQUEST_BIN_URL";
//change this constants as needed
const char APN[] PROGMEM = "internet"; //APN from your provider, given is internet (PLAY)
const char KEY[] PROGMEM= "-"; //API Key from your profile page www.btree.at/app
const char ACTION[] PROGMEM = "CREATE"; //Use CREATE_DEMO if you just want to check the connection
const char TIMEZONE[] PROGMEM = "Europe/Warsaw"; //php timezoneformat, used to save with the correct current date/time
//This is the name of your Hive, www.btree.at/app will create a new if it doenst exist
const char IDENT[] PROGMEM = "ScaleHouse";
//Array Table for our fixed strings
const char * const MARRAY[7] PROGMEM =
{
APN,
URL,
API,
KEY,
ACTION,
TIMEZONE,
IDENT,
};
//Sensor Data
char weight[7];
char temp1[6]= "0.00";
char temp2[6] = "0.00";
char rain[6] = "0.00";
char hum[7] = "0.00";
char ident[] = "0001";
//Battery Data
char charging[1]= "0";
char remains[3] = "00";
char voltage[5] = "00.00";
//Code Variables
float helper;
char conv[150];
char postdata[150];
char response[150];
//atTiny Message PIN
#define FINISHED 2 //Tell the atTiny we are finished
//Scale
const int LOADCELL_DOUT_PIN = 5;
const int LOADCELL_SCK_PIN = 6;
//#define DIGITALOUT 5 //HX711 DT - stara wesja library HX711 - wyłączone
//#define CLOCK 6 //HX711 CKL - stara wesja library HX711 - wyłączone
//HX711 scale(DIGITALOUT, CLOCK); //- stara wesja library HX711 - wyłączone
HX711 scale;
//ENTER YOUR CALIBRATED DATA HERE
//as example is my first try given, with 200kg load cell and 3.3V calibrated
float SCALE = 20794.48;
long offset = 78600;
//DHT22
// #define DHTPIN 10 // what pin we're connected to /*****/
// #define DHTTYPE DHT22 // DHT 22 /*****/
//SHTC3
SHTC3 sht(Wire);
void setup()
{
delay(500);
mySerial.begin(9600);
Serial.begin(9600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
delay(1000);
Power_UP();
delay(2000);
/*if(sendATcommand2("AT+CPIN?", "+CPIN: READY", "NOTHING", 100) == 0) {
Serial.println(F("Error SIM locked, please unlock SIM"));
} else {
Serial.println(F("Good, SIM is unlocked!"));
}*/
//Serial.println(F("Find provider!"));
//sendATcommand2("AT+COPS?", "OK", "NOTHING", 100);
Serial.println(F("Connecting .... "));
while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000)== 0 );
Serial.println(F("Setup finished!"));
delay(2000);
Request();
}
void loop()
{
//Nothing in Loop
}
void Power_UP()
{
uint8_t answer=0;
Serial.println(F("Checking if GSM is running ..."));
answer = sendATcommand2("AT", "OK", "NOTHING", 2000);
if (answer == 0)
{
// power on pulse
//Serial.println("Starting GSM ...");
pinMode(9, OUTPUT);
digitalWrite(9,LOW);
delay(1000);
digitalWrite(9,HIGH);
delay(2000);
digitalWrite(9,LOW);
delay(8000);
// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand2("AT", "OK", "NOTHING", 2000);
}
}
Serial.println(F("GSM ready!"));
delay(1000);
// Read battery capacity
sendATcommand2("AT+CBC", "OK", "NOTHING", 500);
Serial.print("Battery capacity: [%] ");
Serial.println(String(response).substring(17,19));
Serial.print("Battery voltage: [V] ");
Serial.print(String(response).substring(20,20)+String(response).substring(21,24));
//Print GSM Reciving Strenth
Serial.println(F("Get access strength .."));
sendATcommand2("AT+CSQ", "OK", "NOTHING", 1000);
}
int8_t sendATcommand2(char const* ATcommand, char const* expected_answer1, char const* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
unsigned long previous;
memset(response, '\0', 150); // Initialize the string
delay(100);
while( mySerial.available() > 0) mySerial.read(); // Clean the input buffer
mySerial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(mySerial.available() != 0){
response[x] = mySerial.read();
x++;
if (x >= 150) { //Overflow protection
Serial.println(response);
memset(response, 0, 150); // Initialize the string
x=0;
}
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}
void Request()
{
if(sendATcommand2("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E setting connection to GPRS!"));
delay(2000);
Request();
}
snprintf_P(conv, sizeof(conv), PSTR("AT+SAPBR=3,1,\"APN\",\"%S\""), (char*) pgm_read_word(&MARRAY[0]));
if(sendATcommand2(conv, "OK", "ERROR", 500) != 1) {
Serial.println(F("#E APN!"));
delay(2000);
Request();
}
if(sendATcommand2("AT+SAPBR=1,1", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E already attached to GPRS"));
//return;
}
if(sendATcommand2("AT+SAPBR=2,1", "OK", "ERROR", 1000) != 1) {
Serial.println(F("#E IP!"));
delay(2000);
Request();
}
/*if(sendATcommand2("AT+HTTPTERM", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E nothing to terminate"));
//return;
}*/
if(sendATcommand2("AT+HTTPINIT", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E HTTP!"));
delay(2000);
//Request();
}
if(sendATcommand2("AT+HTTPPARA=\"CID\",1", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E BEARER!"));
delay(2000);
Request();
}
if(sendATcommand2("AT+HTTPPARA=\"REDIR\",1", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E setting redirect"));
//return;
}
//------------------------------------------------
//-------------------Sensor Readings--------------
//------------------------------------------------
//HX711 definitions
delay(2000);
scale.set_scale();
scale.set_offset(offset);
scale.set_scale(SCALE);
delay(2000);
//Get weight and convert to char
dtostrf(scale.get_units(5), 0, 2, weight);
Serial.println(F("Weight:\t"));
Serial.println(weight);
//************SHTC3 SENSOR****************
delay(2000);
Wire.begin();
sht.begin(true);
sht.sample();
float t = sht.readTempC();
float h = sht.readHumidity();
dtostrf(t, 5, 2, temp1);
Serial.println(F("Temperature[°C]:"));
Serial.println(temp1);
dtostrf(h, 5, 2, hum);
Serial.println(F("Humidity[%]:"));
Serial.println(hum);
//------------------------------------------------
//-------------------Sensor Readings--------------
//------------------------------------------------
memset(postdata, '\0', 150); // Initialize the string
//snprintf(postdata, sizeof(postdata), "weight=%s&temp1=%s&temp2=%s&hum=%s&rain=%s&ident=%s", weight, temp1, temp2, hum, rain, ident);
snprintf(postdata, sizeof(postdata), "weight=%s&temp1=%s&temp2=%s&hum=%s&rain=%s&ident=%s&charging=%s", weight, temp1, temp2, hum, rain, ident, charging);
snprintf(conv, sizeof(conv), "AT+HTTPPARA=\"URL\", \"http://%S%S%s\"", (char*) pgm_read_word(&MARRAY[1]), (char*) pgm_read_word(&MARRAY[2]), postdata);
if(sendATcommand2(conv, "OK", "ERROR", 500) != 1) {
Serial.println(F("#E URL!"));
delay(2000);
Request();
}
if(sendATcommand2("AT+HTTPPARA=\"UA\",\"arduino\"", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E USERAGENT!"));
delay(2000);
Request();
}
if(sendATcommand2("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded;\"", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E HTTP Content-Type!"));
Request();
}
memset(postdata, '\0', 150); // Initialize the string
snprintf(postdata, sizeof(postdata), "HTTP_X_API=%S&ident=%S&timezone=%S&action=%S", (char*) pgm_read_word(&MARRAY[3]), (char*) pgm_read_word(&MARRAY[6]), (char*) pgm_read_word(&MARRAY[5]), (char*) pgm_read_word(&MARRAY[4]));
snprintf(conv, sizeof(conv), "AT+HTTPDATA=%d, 20000", strlen(postdata));
if(sendATcommand2(conv, "DOWNLOAD", "ERROR", 3500) != 1) {
Serial.println(F("#E prepare sending"));
delay(2000);
Request();
}
Serial.println(String(postdata));
if(sendATcommand2(postdata, "OK", "ERROR", 3500) != 1) {
Serial.println(F("#E POSTING"));
delay(2000);
Request();
}
/*if(sendATcommand2("AT+HTTPSCONT?", "OK", "ERROR", 3000) != 1) {
Serial.println("#E READING CONTENT!");
return;
}*/
delay(500);
if(sendATcommand2("AT+HTTPACTION=1", "NOTHING", "ERROR", 6000) != 0) {
Serial.println(F("#E EXECUTING POST!"));
Request();
}
delay(3000);
if(sendATcommand2("AT+HTTPREAD", "OK", "ERROR", 5000) !=1) {
Serial.println(F("#E READING!"));
delay(2000);
Request();
}
if(sendATcommand2("AT+HTTPTERM", "OK", "ERROR", 500) != 1) {
Serial.println(F("#E TERMINATE!"));
delay(2000);
Request();
}
done();
}
/*void battery()
//************BATTERY STATUS****************
dtostrf(h, 5, 2, charging);
Serial.println(F("cHARGING[%]:"));
Serial.println(charging); */
void done(){
//Shutdown GSM
pinMode(9, OUTPUT);
digitalWrite(9,LOW);
delay(1000);
digitalWrite(9,HIGH);
delay(2000);
digitalWrite(9,LOW);
delay(3000);
//Shutdown Scale
scale.power_down();
//Tell atTiny we are finished (should disconnect Arduino and following could should be unnessaccery)
pinMode(FINISHED, OUTPUT);
digitalWrite(FINISHED,LOW);
delay(10000); // musi byc > 8 sekund zeby ATTiny85 zrobilo odczyt, zanim Arduino pojdzie w sleep mode
//Set Arduino Sleep mode and send to sleep
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
}