Skip to content

Instantly share code, notes, and snippets.

@mbparks
Last active December 16, 2015 11:38
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 mbparks/5428506 to your computer and use it in GitHub Desktop.
Save mbparks/5428506 to your computer and use it in GitHub Desktop.
Supports system to provide web-viewable datalogging of sensor telemetry data from an Arduino. WebPHPLightSensor.ino handles the sensor interface and communication with web server.
#include <Ethernet.h>
#include <SPI.h>
const int sensorPin = 5;
int sensorReading = 0;
byte mac[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Edit with your Arduino Ethernet shield's MAC address
byte ip[] = {
192, 168, 1, 200 }; // Edit with your static IP address on network
byte server[] = {
99, 99, 248, 1}; // Address for your server where PHP files will sit
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
sensorReading = analogRead(sensorPin);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.print("GET /php/LightSensorWriteToFile.php?brightness=");
client.print(sensorReading, DEC);
client.println(" HTTP/1.1");
client.println("Host: your-url.net"); // Make sure to edit with URL for your website
client.println();
}
else {
Serial.println("connection failed");
}
}
void loop()
{
char recvChar;
if (Serial.available() > 0) {
// read the incoming byte:
recvChar = Serial.read();
if (recvChar == 'R') {
Serial.println("R Received");
sensorReading = analogRead(sensorPin);
if (client.connect(server, 80)) {
Serial.println("connected");
client.print("GET /php/LightSensorWriteToFile.php?brightness=");
client.print(sensorReading, DEC);
client.println(" HTTP/1.1");
client.println("Host: your-url.net"); // Make sure to edit with URL for your website
client.println();
}
else {
Serial.println("connection failed");
}
}
}
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
//Serial.println();
//Serial.println("disconnecting.");
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment