-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTempTrack.ino
149 lines (115 loc) · 3.41 KB
/
TempTrack.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
Channel ID: ******
Write APIKey: *********************
#include <SPI.h>
#include <Ethernet.h>
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
// Local Network Settings
byte mac[] = { *x**, *x**, *x**, *x**, *x**, *x** }; // Must be unique on local network
byte ip[] = { ***,***,*,*** }; // Must be unique on local network
byte gateway[] = { ***,***,*,*};
byte subnet[] = {***, ***, ***, ***};
// ThingSpeak Settings
char thingSpeakAddress[] = “api.thingspeak.com”;
String writeAPIKey = “**************”; // Write API Key for a ThingSpeak Channel
const int updateInterval = 10000; // Time interval in milliseconds to update ThingSpeak
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.print(“ETHERNET SHIELD ip is : “);
Serial.println(Ethernet.localIP());
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
tempc = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
String analogPin0 = String(tempc);
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println(“…disconnected.”);
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() – lastConnectionTime > updateInterval))
{
updateThingSpeak(“field1=”+analogPin0);
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print(“POST /update HTTP/1.1\n”);
client.print(“Host: api.thingspeak.com\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: “+writeAPIKey+”\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);
client.print(tsData.length());
client.print(“\n\n”);
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println(“Connecting to ThingSpeak…”);
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println(“Connection to ThingSpeak failed (“+String(failedCounter, DEC)+”)”);
Serial.println();
}
}
else
{
failedCounter++;
Serial.println(“Connection to ThingSpeak Failed (“+String(failedCounter, DEC)+”)”);
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println(“Connecting Arduino to network…”);
Serial.println();
delay(1000);
// Connect to network and obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println(“DHCP Failed, reset Arduino to try again”);
Serial.println();
}
else
{
Serial.println(“Arduino connected to network using DHCP”);
Serial.println();
Serial.println(“Data being uploaded to THINGSPEAK Server…….”);
Serial.println();
}
delay(1000);
}