understanding networks
Moving onto the mega (++ sensors, no computer, data to the server)
A note about the Mega and the spi library: (for use with the SD card and ethernet shield)
On Arduino MEGA, SPI bus uses pins 51 (MOSI), 52(SCK), 50(MISO), and 53(SS)
Sensors include: humidity, barometric, light, temp, gps, rtc, accelerometer, more to come…
connects to a graph:
logs sensor data to a text file which gets written to a server: www.levinegabriella.com/understanding_networks/dataLogger.php
www.levinegabriella.com/understanding_networks/Temp_applet_js
001.
<code>
/
*
002.
Web client
003.
004.
This sketch connects to a website (http:
/
/
www.google.com)
005.
using an Arduino Wiznet Ethernet shield.
006.
007.
Circuit:
008.
*
Ethernet shield attached to pins
10
,
11
,
12
,
13
009.
010.
created
18
Dec
2009
011.
by David A. Mellis
012.
013.
*
/
014.
#include &amp;lt;Wire.h&amp;gt;
015.
#include &amp;lt;SPI.h&amp;gt;
016.
#include &amp;lt;Ethernet.h&amp;gt;
017.
#include &amp;quot;RTClib.h&amp;quot;
018.
#include &amp;lt;SD.h&amp;gt;
019.
020.
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
021.
const unsigned char OSS
=
0
;
/
/
Oversampling Setting
022.
/
/
Calibration values
023.
int ac1;
024.
int ac2;
025.
int ac3;
026.
unsigned int ac4;
027.
unsigned int ac5;
028.
unsigned int ac6;
029.
int b1;
030.
int b2;
031.
int mb;
032.
int mc;
033.
int md;
034.
/
/
b5
is
calculated
in
bmp085GetTemperature(...), this variable
is
also used
in
bmp085GetPressure(...)
035.
/
/
so ...Temperature(...) must be called before ...Pressure(...).
036.
long b5;
037.
038.
short temperature;
039.
long pressure;
040.
041.
/
/
Enter a MAC address
for
your controller below.
042.
/
/
Newer Ethernet shields have a MAC address printed on a sticker on the shield
043.
byte mac[]
=
{
044.
0x00
,
0xAA
,
0xBB
,
0xCC
,
0xDE
,
0x03
};
045.
IPAddress server(
69
,
89
,
31
,
63
);
/
/
my IP server
046.
const int requestInterval
=
30000
;
047.
long lastAttemptTime
=
0
;
/
/
last time you connected to the server,
in
milliseconds
048.
boolean requested;
049.
const int resetLED
=
13
;
050.
float temp;
051.
float voltage;
052.
/
/
Initialize the Ethernet client library
053.
/
/
with the IP address
and
port of the server
054.
/
/
that you want to connect to (port
80
is
default
for
HTTP):
055.
056.
const int chipSelect
=
53
;
/
/
changed
from
8
057.
const int LOCATION_FILE_NUMBER_LSB
=
0x00
;
058.
const int LOCATION_FILE_NUMBER_MSB
=
0x01
;
059.
060.
File dataFile;
061.
RTC_DS1307 RTC;
062.
EthernetClient client;
063.
DateTime now;
064.
065.
int ledPin
=
13
;
/
/
LED test pin to determine
if
we are successfully writing gps strings to a text file
066.
int ledState
=
LOW;
067.
068.
int rxPin
=
0
;
/
/
RX PIN
069.
int txPin
=
1
;
/
/
TX TX
070.
071.
void setup() {
072.
pinMode(ledPin, OUTPUT);
/
/
Initialize LED pin
073.
pinMode(rxPin, INPUT);
074.
pinMode(txPin, OUTPUT);
075.
/
/
start the serial library:
076.
Serial.begin(
38400
);
077.
pinMode(A2, OUTPUT);
078.
pinMode(A3, OUTPUT);
079.
/
/
A2
is
the ground, A3
is
the power:
080.
digitalWrite(A2, LOW);
081.
digitalWrite(A3, HIGH);
082.
pinMode(chipSelect, OUTPUT);
083.
084.
if
(!SD.begin(chipSelect)) {
085.
Serial.println(&amp;quot;Card failed,
or
not
present&amp;quot;);
086.
/
/
don't do anything more:
087.
088.
}
089.
Serial.println(&amp;quot;card initialized.&amp;quot;);
090.
091.
Wire.begin();
092.
RTC.begin();
093.
delay(
50
);
094.
bmp085Calibration();
095.
if
(! RTC.isrunning()) {
096.
Serial.println(&amp;quot;RTC
is
NOT running!&amp;quot;);
097.
/
/
following line sets the RTC to the date &amp;amp;amp; time this sketch was compiled
098.
RTC.adjust(DateTime(__DATE__, __TIME__));
099.
}
100.
dataFile
=
SD.open(&amp;quot;data.txt&amp;quot;, FILE_WRITE);
101.
delay(
500
);
102.
103.
/
/
start the Ethernet connection:
104.
Ethernet.begin(mac);
105.
if
(Ethernet.begin(mac)
=
=
0
) {
106.
Serial.println(&amp;quot;Failed to configure Ethernet using DHCP&amp;quot;);
107.
/
/
no point
in
carrying on, so do nothing forevermore:
108.
/
/
for
(;;)
109.
/
/
;
110.
}
111.
112.
/
/
connectToServer();
113.
/
/
give the Ethernet shield a second to initialize:
114.
delay(
1500
);
115.
blink(resetLED,
3
);
116.
Serial.println(&amp;quot;connecting...&amp;quot;);
117.
connectToServer();
118.
}
119.
120.
/
/
Stores all of the bmp085's calibration values into
global
variables
121.
/
/
Calibration values are required to calculate temp
and
pressure
122.
/
/
This function should be called at the beginning of the program
123.
void bmp085Calibration()
124.
{
125.
ac1
=
bmp085ReadInt(
0xAA
);
126.
ac2
=
bmp085ReadInt(
0xAC
);
127.
ac3
=
bmp085ReadInt(
0xAE
);
128.
ac4
=
bmp085ReadInt(
0xB0
);
129.
ac5
=
bmp085ReadInt(
0xB2
);
130.
ac6
=
bmp085ReadInt(
0xB4
);
131.
b1
=
bmp085ReadInt(
0xB6
);
132.
b2
=
bmp085ReadInt(
0xB8
);
133.
mb
=
bmp085ReadInt(
0xBA
);
134.
mc
=
bmp085ReadInt(
0xBC
);
135.
md
=
bmp085ReadInt(
0xBE
);
136.
}
137.
138.
void loop()
139.
{
140.
temperature
=
bmp085GetTemperature(bmp085ReadUT());
141.
pressure
=
bmp085GetPressure(bmp085ReadUP());
142.
now
=
RTC.now();
143.
if
(client.connected()){
144.
if
(!requested){
145.
146.
requested
=
makeRequest();
147.
/
/
Serial.println(&amp;quot;requesting!&amp;quot;);
148.
}
149.
if
(millis()
-
lastAttemptTime&amp;gt;requestInterval){
150.
/
/
if
youre
not
connected
and
two minutes have passed, attempt to connect again
151.
client.stop();
152.
/
/
Serial.println(&amp;quot;stopping
and
reconnecting!&amp;quot;);
153.
154.
/
/
getData();
155.
delay(
1500
);
156.
/
/
connectToServer();
157.
}
158.
/
/
if
there are incoming bytes available
159.
/
/
from
the server, read them
and
print
them:
160.
161.
}
162.
163.
if
(client.available()) {
164.
char c
=
client.read();
165.
/
/
Serial.
print
(c);
166.
}
167.
168.
/
/
if
the server's disconnected, stop the client:
169.
if
(!client.connected()) {
170.
/
/
Serial.println();
171.
/
/
Serial.println(&amp;quot;disconnecting.&amp;quot;);
172.
173.
client.stop();
174.
delay(
1500
);
175.
if
(millis()
-
lastAttemptTime&amp;gt;requestInterval){
176.
/
/
if
youre
not
connected
and
two minutes have passed, attempt to connect again
177.
connectToServer();
178.
/
/
try
to reconnect here...
179.
}
180.
}
181.
182.
}
183.
184.
void getData(){
185.
voltage
=
5
*
analogRead(A0)
/
1024.0
;
186.
/
/
float temp
=
5
*
analogRead(A1)
/
1024.0
;
187.
temp
=
(analogRead(A1))
/
10
;
188.
189.
if
(Serial.available()&amp;gt;
0
){
190.
byte gps
=
Serial.read();
/
/
echo incoming gps data
191.
Serial.write(gps);
192.
193.
if
(dataFile) {
194.
digitalWrite(ledPin, HIGH);
/
/
turn on the status led
195.
DateTime now
=
RTC.now();
196.
dataFile.write(gps);
197.
dataFile.
print
(now.month());
198.
dataFile.
print
(
'/'
);
199.
dataFile.
print
(now.day());
200.
dataFile.
print
(
'/'
);
201.
dataFile.
print
(now.year());
202.
dataFile.
print
(F(&amp;quot;,&amp;quot;));
203.
dataFile.
print
(now.hour());
204.
dataFile.
print
(F(&amp;quot;:&amp;quot;));
205.
dataFile.
print
(now.minute());
206.
dataFile.
print
(F(&amp;quot;:&amp;quot;));
207.
dataFile.
print
(now.second());
208.
dataFile.
print
(F(&amp;quot;,&amp;quot;));
209.
dataFile.
print
(voltage);
210.
dataFile.
print
(F(&amp;quot;,&amp;quot;));
211.
dataFile.
print
(temp);
212.
dataFile.
print
(temperature);
213.
dataFile.
print
(F(&amp;quot;,&amp;quot;));
214.
dataFile.
print
(pressure);
215.
dataFile.println();
216.
217.
}
218.
219.
dataFile.flush();
220.
}
221.
else
{digitalWrite(ledPin, LOW);}
222.
223.
}
224.
225.
void connectToServer(){
226.
/
/
Serial.println(&amp;quot;connecting to server...&amp;quot;);
227.
if
(client.connect(server,
80
)) {
228.
requested
=
false;
229.
}
230.
231.
lastAttemptTime
=
millis();
232.
}
233.
234.
boolean makeRequest() {
235.
/
/
Serial.println(&amp;quot;requesting&amp;quot;);
236.
getData();
237.
/
/
Make a HTTP request:
238.
239.
client.
print
(&amp;quot;GET
/
understanding_networks
/
dataLogger.php?data
=
&amp;quot;);
240.
client.
print
(now.month());
241.
client.
print
(
'/'
);
242.
client.
print
(now.day());
243.
client.
print
(
'/'
);
244.
client.
print
(now.year());
245.
client.
print
(F(&amp;quot;,&amp;quot;));
246.
client.
print
(now.hour());
247.
client.
print
(F(&amp;quot;:&amp;quot;));
248.
client.
print
(now.minute());
249.
client.
print
(F(&amp;quot;:&amp;quot;));
250.
client.
print
(now.second());
251.
client.
print
(F(&amp;quot;,&amp;quot;));
252.
client.
print
(voltage);
253.
client.
print
(F(&amp;quot;,&amp;quot;));
254.
client.
print
(temp);
255.
client.
print
(F(&amp;quot;,&amp;quot;));
256.
client.
print
(temperature);
257.
client.
print
(F(&amp;quot;,&amp;quot;));
258.
client.
print
(pressure);
259.
client.println(&amp;quot; HTTP
/
1.1
&amp;quot;);
260.
client.println(&amp;quot;HOST: www.levinegabriella.com&amp;quot;);
261.
262.
client.println();
263.
return
true;
264.
265.
}
266.
267.
/
/
Calculate temperature given ut.
268.
/
/
Value returned will be
in
units of
0.1
deg C
269.
short bmp085GetTemperature(unsigned int ut)
270.
{
271.
long x1, x2;
272.
273.
x1
=
(((long)ut
-
(long)ac6)
*
(long)ac5) &amp;gt;&amp;gt;
15
;
274.
x2
=
((long)mc &amp;lt;&amp;lt;
11
)
/
(x1
+
md);
275.
b5
=
x1
+
x2;
276.
277.
return
((b5
+
8
)&amp;gt;&amp;gt;
4
);
278.
}
279.
280.
/
/
Calculate pressure given up
281.
/
/
calibration values must be known
282.
/
/
b5
is
also required so bmp085GetTemperature(...) must be called first.
283.
/
/
Value returned will be pressure
in
units of Pa.
284.
long bmp085GetPressure(unsigned long up)
285.
{
286.
long x1, x2, x3, b3, b6, p;
287.
unsigned long b4, b7;
288.
289.
b6
=
b5
-
4000
;
290.
/
/
Calculate B3
291.
x1
=
(b2
*
(b6
*
b6)&amp;gt;&amp;gt;
12
)&amp;gt;&amp;gt;
11
;
292.
x2
=
(ac2
*
b6)&amp;gt;&amp;gt;
11
;
293.
x3
=
x1
+
x2;
294.
b3
=
(((((long)ac1)
*
4
+
x3)&amp;lt;&amp;lt;OSS)
+
2
)&amp;gt;&amp;gt;
2
;
295.
296.
/
/
Calculate B4
297.
x1
=
(ac3
*
b6)&amp;gt;&amp;gt;
13
;
298.
x2
=
(b1
*
((b6
*
b6)&amp;gt;&amp;gt;
12
))&amp;gt;&amp;gt;
16
;
299.
x3
=
((x1
+
x2)
+
2
)&amp;gt;&amp;gt;
2
;
300.
b4
=
(ac4
*
(unsigned long)(x3
+
32768
))&amp;gt;&amp;gt;
15
;
301.
302.
b7
=
((unsigned long)(up
-
b3)
*
(
50000
&amp;gt;&amp;gt;OSS));
303.
if
(b7 &amp;lt;
0x80000000
)
304.
p
=
(b7&amp;lt;&amp;lt;
1
)
/
b4;
305.
else
306.
p
=
(b7
/
b4)&amp;lt;&amp;lt;
1
;
307.
308.
x1
=
(p&amp;gt;&amp;gt;
8
)
*
(p&amp;gt;&amp;gt;
8
);
309.
x1
=
(x1
*
3038
)&amp;gt;&amp;gt;
16
;
310.
x2
=
(
-
7357
*
p)&amp;gt;&amp;gt;
16
;
311.
p
+
=
(x1
+
x2
+
3791
)&amp;gt;&amp;gt;
4
;
312.
313.
return
p;
314.
}
315.
316.
/
/
Read
1
byte
from
the BMP085 at
'address'
317.
char bmp085Read(unsigned char address)
318.
{
319.
unsigned char data;
320.
321.
Wire.beginTransmission(BMP085_ADDRESS);
322.
Wire.write(address);
323.
Wire.endTransmission();
324.
325.
Wire.requestFrom(BMP085_ADDRESS,
1
);
326.
while
(!Wire.available())
327.
;
328.
329.
return
Wire.read();
330.
}
331.
332.
/
/
Read
2
bytes
from
the BMP085
333.
/
/
First byte will be
from
'address'
334.
/
/
Second byte will be
from
'address'
+
1
335.
int bmp085ReadInt(unsigned char address)
336.
{
337.
unsigned char msb, lsb;
338.
339.
Wire.beginTransmission(BMP085_ADDRESS);
340.
Wire.write(address);
341.
Wire.endTransmission();
342.
343.
Wire.requestFrom(BMP085_ADDRESS,
2
);
344.
while
(Wire.available()&amp;lt;
2
)
345.
;
346.
msb
=
Wire.read();
347.
lsb
=
Wire.read();
348.
349.
return
(int) msb&amp;lt;&amp;lt;
8
| lsb;
350.
}
351.
352.
/
/
Read the uncompensated temperature value
353.
unsigned int bmp085ReadUT()
354.
{
355.
unsigned int ut;
356.
357.
/
/
Write
0x2E
into Register
0xF4
358.
/
/
This requests a temperature reading
359.
Wire.beginTransmission(BMP085_ADDRESS);
360.
Wire.write(
0xF4
);
361.
Wire.write(
0x2E
);
362.
Wire.endTransmission();
363.
364.
/
/
Wait at least
4.5ms
365.
delay(
5
);
366.
367.
/
/
Read two bytes
from
registers
0xF6
and
0xF7
368.
ut
=
bmp085ReadInt(
0xF6
);
369.
return
ut;
370.
}
371.
372.
/
/
Read the uncompensated pressure value
373.
unsigned long bmp085ReadUP()
374.
{
375.
unsigned char msb, lsb, xlsb;
376.
unsigned long up
=
0
;
377.
378.
/
/
Write
0x34
+
(OSS&amp;lt;&amp;lt;
6
) into register
0xF4
379.
/
/
Request a pressure reading w
/
oversampling setting
380.
Wire.beginTransmission(BMP085_ADDRESS);
381.
Wire.write(
0xF4
);
382.
Wire.write(
0x34
+
(OSS&amp;lt;&amp;lt;
6
));
383.
Wire.endTransmission();
384.
385.
/
/
Wait
for
conversion, delay time dependent on OSS
386.
delay(
2
+
(
3
&amp;lt;&amp;lt;OSS));
387.
388.
/
/
Read register
0xF6
(MSB),
0xF7
(LSB),
and
0xF8
(XLSB)
389.
Wire.beginTransmission(BMP085_ADDRESS);
390.
Wire.write(
0xF6
);
391.
Wire.endTransmission();
392.
Wire.requestFrom(BMP085_ADDRESS,
3
);
393.
394.
/
/
Wait
for
data to become available
395.
while
(Wire.available() &amp;lt;
3
)
396.
;
397.
msb
=
Wire.read();
398.
lsb
=
Wire.read();
399.
xlsb
=
Wire.read();
400.
401.
up
=
(((unsigned long) msb &amp;lt;&amp;lt;
16
) | ((unsigned long) lsb &amp;lt;&amp;lt;
8
) | (unsigned long) xlsb) &amp;gt;&amp;gt; (
8
-
OSS);
402.
403.
return
up;
404.
}
405.
406.
void blink(int thisPin, int howManyTimes){
407.
for
(int blinks
=
0
;blinks&amp;lt;howManyTimes;blinks
+
+
){
408.
digitalWrite(thisPin, HIGH);
409.
delay(
200
);
410.
digitalWrite(thisPin, LOW);
411.
delay(
200
);
412.
}
413.
}
414.
415.
/
/
questions: what other sensors will be good?
416.
/
/
what
is
too much data
417.
/
/
i seem to be making my request twice
418.
419.
/
/
when my request interval
is
more than
5000
i get
420.
/
/
10
/
13
/
2011
,
11
:
14
:
56
,
3.67
,
15.00
421.
/
/
10
/
13
/
2011
,
11
:
15
:
5
,
3.65
,
15.00
422.
/
/
10
/
13
/
2011
,
11
:
15
:
16
,
0.00
,
0.00
423.
/
/
10
/
13
/
2011
,
11
:
15
:
44
,
0.00
,
0.00
424.
/
/
why
425.
426.
/
/
can you go back a directory
427.
428.
/
/
try
it? overwhelm maybe... w
/
gps
429.
/
/
implem. timer
430.
<
/
code>
Logging data remotely to the web, no computer
So I switched over the client to here: http://levinegabriella.com/understanding_networks/liveApplet/
(I have to figure out why no text is appearing on the graph – i had temperature and light and date markers)
the php server is written here: http://levinegabriella.com/understanding_networks/dataLogger.php
The server reads and writes to a text file here: http://www.levinegabriella.com/understanding_networks/liveApplet/DATAcl.TXT
See code below for Arduino and php and processing
Next step: The day I left NY my dataLogging Arduino “crashed”, after working for a week . It could have been any number of things. The SD card, the server, a short?
Try Pachube, and Watch Dog to restart Arduino if it crashes.
Can i do some virtual debugging?
Also, Arduino UNO ran out of space (the functions include: data logger, ethernet connection to a server, temp sensor, light sensor, real time clock – But there are many more sensors on board (gps, humidity, accelerometer, barometric pressure) so I’ve moved everything over to the MEGA. Had to rearrange the SPI / I2C pin connections, but besides that it’s all good.
(all the code (php, processing, arduino) can also be downloaded here)
More to come…
01.
&amp;amp;amp;lt;?php
02.
// put the name and path of the text file in a variable.
03.
// this is the text file where we'll store the data:
04.
$filename
=
'liveApplet/DATAcl.TXT'
;
05.
06.
//make sure the file is not empty:
07.
if
(
file_exists
(
$filename
)) {
08.
// get the contents of the file
09.
// and put them in a variable called $fileContents:
10.
$fileContents
=
file_get_contents
(
$filename
);
11.
12.
// if there is new data from the client, it'll
13.
// be in a request parameter called &amp;amp;amp;quot;data&amp;amp;amp;quot;.
14.
if
(isset(
$_REQUEST
[
'data'
])) {
15.
// append what the client sent as 'data' to
16.
// the variable holding the file contents:
17.
$fileContents
=
$fileContents
. &amp;amp;amp;quot;\n&amp;amp;amp;quot;.
$_REQUEST
[
'data'
];
18.
// put the file contents back into the file
19.
// you're overwriting the whole file when you do this:
20.
file_put_contents
(
$filename
,
$fileContents
);
21.
}
else
{
22.
// there was no data sent in the request
23.
// so show the old stuff:
24.
// echo '&amp;amp;amp;lt;p&amp;amp;amp;gt;' . $fileContents . '&amp;amp;amp;lt;/p&amp;amp;amp;gt;';
25.
//split the string of the newLines:
26.
$strings
=
explode
(&amp;amp;amp;quot;\n&amp;amp;amp;quot;,
$fileContents
);
27.
//loop over the array of the lines, adding a break at the end of each
28.
foreach
(
$strings
as
$thisString
){
29.
echo
$thisString
. &amp;amp;amp;quot;&amp;amp;amp;lt;br/&amp;amp;amp;gt;\n&amp;amp;amp;quot;;
30.
}
31.
}
32.
}
33.
?&amp;amp;amp;gt;
Arduino:
[/python]
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
*/
#include
#include
#include
#include “RTClib.h”
#include
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
IPAddress server(69,89,31,63); // my IP server
const int requestInterval = 30000;
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
boolean requested;
const int resetLED = 13;
float temp;
float voltage;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
const int chipSelect = 4;//changed from 8
const int LOCATION_FILE_NUMBER_LSB = 0x00;
const int LOCATION_FILE_NUMBER_MSB = 0x01;
File dataFile;
RTC_DS1307 RTC;
EthernetClient client;
DateTime now;
void setup() {
// start the serial library:
Serial.begin(9600);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// A2 is the ground, A3 is the power:
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:
}
Serial.println(“card initialized.”);
Wire.begin();
RTC.begin();
delay(50);
if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
dataFile = SD.open(“data.txt”, FILE_WRITE);
delay(500);
// start the Ethernet connection:
Ethernet.begin(mac);
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// connectToServer();
// give the Ethernet shield a second to initialize:
delay(1500);
blink(resetLED, 3);
Serial.println(“connecting…”);
connectToServer();
}
void loop()
{
now = RTC.now();
if(client.connected()){
if(!requested){
requested = makeRequest();
// Serial.println(“requesting!”);
}
if(millis() – lastAttemptTime>requestInterval){
//if youre not connected and two minutes have passed, attempt to connect again
client.stop();
// Serial.println(“stopping and reconnecting!”);
// getData();
delay(1500);
//connectToServer();
}
// if there are incoming bytes available
// from the server, read them and print them:
}
if (client.available()) {
char c = client.read();
// Serial.print(c);
}
// if the server’s disconnected, stop the client:
if (!client.connected()) {
// Serial.println();
// Serial.println(“disconnecting.”);
client.stop();
delay(1500);
if(millis() – lastAttemptTime>requestInterval){
//if youre not connected and two minutes have passed, attempt to connect again
connectToServer();
//try to reconnect here…
}
}
}
void getData(){
voltage = 5 * analogRead(A0) / 1024.0;
//float temp = 5 * analogRead(A1) / 1024.0;
temp=(analogRead(A1))/10;
// Serial.print(voltage);
// Serial.print(F(“,”));
// Serial.print(temp);
// Serial.print(F(“,”));
// Serial.println(” “);
//
if (dataFile) {
DateTime now = RTC.now();
dataFile.print(now.month());
dataFile.print(‘/’);
dataFile.print(now.day());
dataFile.print(‘/’);
dataFile.print(now.year());
dataFile.print(F(“,”));
dataFile.print(now.hour());
dataFile.print(F(“:”));
dataFile.print(now.minute());
dataFile.print(F(“:”));
dataFile.print(now.second());
dataFile.print(F(“,”));
dataFile.print(voltage);
dataFile.print(F(“,”));
dataFile.print(temp);
dataFile.println();
}
dataFile.flush();
}
void connectToServer(){
// Serial.println(“connecting to server…”);
if (client.connect(server, 80)) {
requested = false;
}
lastAttemptTime = millis();
}
boolean makeRequest() {
// Serial.println(“requesting”);
getData();
// Make a HTTP request:
client.print(“GET /understanding_networks/dataLogger.php?data=”);
client.print(now.month());
client.print(‘/’);
client.print(now.day());
client.print(‘/’);
client.print(now.year());
client.print(F(“,”));
client.print(now.hour());
client.print(F(“:”));
client.print(now.minute());
client.print(F(“:”));
client.print(now.second());
client.print(F(“,”));
client.print(voltage);
client.print(F(“,”));
client.print(temp);
client.println(” HTTP/1.1 “);
client.println(“HOST: www.levinegabriella.com”);
client.println();
return true;
}
void blink(int thisPin, int howManyTimes){
for (int blinks = 0;blinks digitalWrite(thisPin, HIGH);
delay(200);
digitalWrite(thisPin, LOW);
delay(200);
}
}
//questions: what other sensors will be good?
//what is too much data
//i seem to be making my request twice
//when my request interval is more than 5000 i get
//10/13/2011,11:14:56,3.67,15.00
//10/13/2011,11:15:5,3.65,15.00
//10/13/2011,11:15:16,0.00,0.00
//10/13/2011,11:15:44,0.00,0.00
//why
//can you go back a directory
[/python]
How to get data on the internet without a computer
I did this using the Arduino ethernet shield.
Below is the script for the server that Arduino calls in its HTTP request.
The server has the following function: if there is data added to it through a GET request, it stores that data in a text file (called datalog.txt) that is also on the same server. That way my sensor data can be stored as a text file. Secondly, when the server is called with no request, it shows the text of the text file. This way, I can write a program to pull the data from that website and graph it (processing.js, perhaps… more to come later this week on that).
click here for a text file of the code in case it is not showing up correctly below
01.
&amp;amp;amp;lt;?php
02.
// put the name and path of the text file in a variable.
03.
// this is the text file where we'll store the data:
04.
$filename
=
'datalog.txt'
;
05.
06.
//make sure the file is not empty:
07.
if
(
file_exists
(
$filename
)) {
08.
// get the contents of the file
09.
// and put them in a variable called $fileContents:
10.
$fileContents
=
file_get_contents
(
$filename
);
11.
12.
// if there is new data from the client, it'll
13.
// be in a request parameter called &amp;amp;amp;quot;data&amp;amp;amp;quot;.
14.
if
(isset(
$_REQUEST
[
'data'
])) {
15.
// append what the client sent as 'data' to
16.
// the variable holding the file contents:
17.
$fileContents
=
$fileContents
. &amp;amp;amp;quot;\n&amp;amp;amp;quot;.
$_REQUEST
[
'data'
];
18.
// put the file contents back into the file
19.
// you're overwriting the whole file when you do this:
20.
file_put_contents
(
$filename
,
$fileContents
);
21.
}
else
{
22.
// there was no data sent in the request
23.
// so show the old stuff:
24.
echo
$fileContents
;
25.
}
26.
}
27.
?&amp;amp;amp;gt;
From the Arduino side, this is the code I use, for now, to append my text file. It is adapted from the example WebClient in the Ethernet library on Arduino.
I just had to enter my MAC address of the ethernet shield and the IP address of the server I am making the request to.
The next step will be to continually make requests as sensor data comes in, to have a legible CSV.
01.
/*
02.
Web client
03.
04.
This sketch connects to a website (http://www.google.com)
05.
using an Arduino Wiznet Ethernet shield.
06.
07.
Circuit:
08.
* Ethernet shield attached to pins 10, 11, 12, 13
09.
10.
created 18 Dec 2009
11.
by David A. Mellis
12.
13.
*/
14.
15.
#include &amp;amp;amp;lt;SPI.h&amp;amp;amp;gt;
16.
#include &amp;amp;amp;lt;Ethernet.h&amp;amp;amp;gt;
17.
18.
// Enter a MAC address for your controller below.
19.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
20.
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
21.
IPAddress server(69,89,31,63);
// Google
22.
23.
// Initialize the Ethernet client library
24.
// with the IP address and port of the server
25.
// that you want to connect to (port 80 is default for HTTP):
26.
EthernetClient client;
27.
28.
void
setup() {
29.
// start the serial library:
30.
Serial.begin(9600);
31.
// start the Ethernet connection:
32.
if
(Ethernet.begin(mac) == 0) {
33.
Serial.println(&amp;amp;amp;quot;Failed to configure Ethernet
using
DHCP&amp;amp;amp;quot;);
34.
// no point in carrying on, so do nothing forevermore:
35.
for
(;;)
36.
;
37.
}
38.
// give the Ethernet shield a second to initialize:
39.
delay(1000);
40.
Serial.println(&amp;amp;amp;quot;connecting...&amp;amp;amp;quot;);
41.
42.
// if you get a connection, report back via serial:
43.
if
(client.connect(server, 80)) {
44.
Serial.println(&amp;amp;amp;quot;connected&amp;amp;amp;quot;);
45.
// Make a HTTP request:
46.
client.println(&amp;amp;amp;quot;GET /understanding_networks/dataLogger.php?data=it_works_yay HTTP/1.1&amp;amp;amp;quot;);
47.
client.println(&amp;amp;amp;quot;HOST: www.levinegabriella.com&amp;amp;amp;quot;);
48.
// client.println(&amp;amp;amp;quot;GET /understanding_networks/dataLogger.php?data=1233212321232123 HTTP/1.0&amp;amp;amp;quot;);
50.
client.println();
51.
}
52.
else
{
53.
// kf you didn't get a connection to the server:
54.
Serial.println(&amp;amp;amp;quot;connection failed&amp;amp;amp;quot;);
55.
}
56.
}
57.
58.
void
loop()
59.
{
60.
// if there are incoming bytes available
61.
// from the server, read them and print them:
62.
if
(client.available()) {
63.
char
c = client.read();
64.
Serial.print(c);
65.
}
66.
67.
// if the server's disconnected, stop the client:
68.
if
(!client.connected()) {
69.
Serial.println();
70.
Serial.println(&amp;amp;amp;quot;disconnecting.&amp;amp;amp;quot;);
71.
client.stop();
72.
73.
// do nothing forevermore:
74.
for
(;;)
75.
;
76.
}
77.
}