Skip to content

Instantly share code, notes, and snippets.

@EvanGertis
Last active June 18, 2018 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvanGertis/0acf7359f419193ceeea0b77af69a311 to your computer and use it in GitHub Desktop.
Save EvanGertis/0acf7359f419193ceeea0b77af69a311 to your computer and use it in GitHub Desktop.
Solution for EMONCMS
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,0, 13);
EthernetClient client;
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 60000;
const String APIKEY="db2eb7befe02a72d49dffc1cc4365fdc";
const char server[] ="www.emoncms.org";
const int PORT=80;
int photoSensorPin = 1;
volatile int photoSensorValue = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
String str = "GET /input/post?node=emontx&fulljson=";
str += "{\"lum\":";
photoSensorValue = analogRead(photoSensorPin);
str += String(photoSensorValue);
str += "}&apikey=";
str += APIKEY;
str+= " HTTP/1.1";
Serial.println(str);
client.println(str);
client.println("Host: www.emoncms.org");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
readData();
//setup();
}
void readData() {
// 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(1000);
setup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment