-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cafeduino.pde
148 lines (119 loc) · 3.7 KB
/
Cafeduino.pde
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
#include <Print.h>
#include <Ethernet.h>
#include <Client.h>
#include <Server.h>
#include <string.h>
#include <stdio.h>
/* pin assignments */
int led1 = 6; // led 1
int led2 = 9; // led 2
int led3 = 10; // led 3
int piezo1 = 5; //piezo 1
int piezo2 = 4; //piezo 2
int button1 = 2; //tare switch
int button2 = 3; //full switch
int v1 = 0; //measured value 1
int v2 = 0; //measured value 2
int tareVal = 0; // tare empty value
int maxVal = 1023; // tare max value
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xBE }; // MAC is 0xDEAFBEEFBABE
byte ip[] = { 192, 168, 1, 96 }; // IP is 10.0.0.9
byte gateway[] = { 192, 168, 1, 1 }; // Gateway is 10.0.0.1
byte subnet[] = { 255, 255, 255, 0 }; // Subnet is 255.0.0.0
byte remote[] = { 209,40,205,190 }; // pachube.com
Client pachube( remote, 80 );
Server localhost( 80 );
unsigned long flash_interval = 1000;
unsigned long interval = 60000; // 1 min
unsigned long update_time = millis();
char pachube_data[50];
char request_data[50];
int totalread = 0;
void setup()
{
Serial.begin(9600);
pinMode( led1, OUTPUT );
pinMode( led2, OUTPUT );
pinMode( led3, OUTPUT );
pinMode( button1, INPUT );
pinMode( button2, INPUT );
Ethernet.begin( mac, ip, gateway, subnet );
delay(500);
localhost.begin();
}
void loop()
{
v1 = analogRead( piezo1 );
v2 = analogRead( piezo2 );
if( digitalRead( button1 ) == HIGH )
tareVal = v1 + v2;
if ( digitalRead( button2 ) == HIGH )
maxVal = v1 + v2;
sprintf(pachube_data,"%d,%d,%d,%d,%ld",tareVal,v1,v2,maxVal,millis());
int content_length = strlen(pachube_data);
// only publish once per interval
if ( millis() > update_time + interval )
{
update_time = millis();
if ( pachube.connect() ) {
pachube.println("PUT /api/2582.csv HTTP/1.1");
pachube.println("Host: pachube.com");
//pachube.println("X-PachubeApiKey: key");
pachube.println("User-Agent: Arduino (Custom)");
pachube.println("Content-Type: text/csv");
pachube.print("Content-Length: ");
pachube.println(content_length);
pachube.println("Connection: close\n");
pachube.println(pachube_data);
pachube.stop();
}
}
// try to connect to a web client, if anyone is out there
Client web = localhost.available();
if (web) {
int toread = web.available();
int beenread = 0;
while ( beenread < toread && beenread < 50 ) {
request_data[beenread] = web.read();
beenread++;
if ( request_data[beenread-1] == '\n' ) {
request_data[beenread-1] == '\0';
break;
}
}
char rstr[3];
memset( rstr, '\0', 3 );
char req = sscanf( request_data, "GET %2c", rstr);
if ( req != EOF && strcmp( rstr, "/ " ) == 0 ) {
web.write( "HTTP/1.0 200 OK\n" );
web.write( "Connection: close\n" );
web.write( "Content-Length: " );
web.println( content_length );
web.write( "Content-Type: text/plain\n\n" );
web.println( pachube_data );
}
else {
web.write( "HTTP/1.0 404 Not Found\n" );
web.write( "Content-Type: text/plain\n\n" );
web.write( "URL not found." );
}
web.flush();
web.stop();
}
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
if( Serial.read() == 'r' ) {
// say what you got:
Serial.println(pachube_data);
}
}
int warn = LOW;
if ( (v1 + v2) < tareVal ) {
if ( millis() % (2 * flash_interval) < flash_interval )
warn = HIGH;
}
analogWrite( led1, constrain(map(v1,tareVal,maxVal,0,255),0,255) );
analogWrite( led2, constrain(map(v2,tareVal,maxVal,0,255),0,255) );
digitalWrite( led3, warn );
}