-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simple_emon_arduino.ino
165 lines (111 loc) · 4.15 KB
/
Simple_emon_arduino.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
/*
Arduino & OpenEnergyMonitor
This sketch connects to an emoncms server and send data
Author: Mirco Piccin aka pitusso
Modified and simplified by J-F Payeur
Tested on Arduino Ethernet
Tested on Arduino Uno + ethernet sheild
Tested on Arduino2560 + ethernet sheild
based on http://arduino.cc/en/Tutorial/WebClientRepeating
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x04, 0x69, 0xD5};
// fill in an available IP address on your network here, if dhcp auto config fail
IPAddress ip(192, 168, 1, 82);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(8, 8, 8, 8);
IPAddress gw(192, 168, 1, 1);
EthernetClient client;
// Sensor pins
const int lightSensorPin = A0;
int lightValue =0 ;
//Emoncms configurations
//char server[] = "emoncms.org"; // name address for emoncms.org
// OR
IPAddress server(192, 168, 81, 145); // numeric IP for emoncms.org (no DNS)
String apikey = "0fc3340c4434a7d42986e83fe2745e01"; //api key emoncms.org
int node = 1; //if 0, not used
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 5*1000; // delay between updates, in milliseconds
void setup() {
Serial.begin(9600);
Serial.println("Emoncms client starting...");
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("Failed to get an IP address using DHCP, forcing manually");
Ethernet.begin(mac, ip, dns, gw, subnet);
}
pinMode(lightSensorPin, INPUT); // set pin to input
digitalWrite(lightSensorPin, HIGH); // turn on pullup resistors
printStatus();
}
void loop() {
// if there's incoming data from the net connection.send it out the serial port. This is for debugging purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("Disconnecting...");
client.stop();
}
// if you're not connected, and at least <postingInterval> milliseconds have passed sinceyour last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
//read sensors
lightValue = analogRead(lightSensorPin);
lightValue = map(lightValue, 50, 900, 100, 1); //100 a 800
lightValue = constrain(lightValue, 0, 100);
//Print values (debug)
Serial.println();
Serial.print(" ; Light : "); Serial.print(lightValue);
//send values
sendData();
}
// store the state of the connection for next time through the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData() {
Serial.println("toti");
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("Connecting...");
// send the HTTP GET request:
client.print("GET /emoncms/input/post?apikey=");
client.print(apikey);
if (node > 0) {
client.print("&node=");
client.print(node);
}
client.print("&json={");
client.print("light:");
client.print(lightValue);
client.println("} HTTP/1.1");
client.println("Host:emoncms.org");
client.println("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting...");
client.stop();
}
}
void printStatus() {
// print your local IP address:
Serial.print("IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}