Last active
December 14, 2015 05:29
-
-
Save rockos/de0420ea0056b8ca090e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#include <WebSocketClient.h> | |
#include <SPI.h> | |
#include <Ethernet.h> | |
// Enter a MAC address and IP address for your controller below. | |
// The IP address will be dependent on your local network: | |
byte mac[] = { | |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; /* Edit here */ | |
IPAddress ip(192,168,0, 1); /* Edit here */ | |
/* socket.io server */ | |
char SERVER[] = "192.168.0.10"; /* Edit here */ | |
int PORT = 3013; | |
WebSocketClient client; | |
/** | |
* | |
* | |
*/ | |
int connectSrv(char host[],int& port) { | |
if(client.connect(host, "/socket.io/1/websocket/", port)){ | |
client.setDataArrivedDelegate(dataArrived); | |
delay(1000); | |
Serial.print("Debug: Connected"); | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
/** | |
* Initialize the Ethernet server library | |
* with the IP address and port you want to use | |
*/ | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for Leonardo only | |
} | |
// start the Ethernet connection and the server: | |
Ethernet.begin(mac, ip); | |
Serial.print("Debug: Setup"); | |
delay(1000); | |
int rc = connectSrv(SERVER, PORT); | |
} | |
void dataArrived(WebSocketClient client, String data) { | |
Serial.println("Data Arrived: "+data); | |
} | |
float aval2lx(int aval) { | |
float voltage = ((long)aval * 5000) / 1024; | |
float microamp = (voltage * 1000) / 1000; | |
return (microamp / (290 / 100)); | |
} | |
/* | |
* main loop | |
*/ | |
void loop() { | |
int rc = false; | |
if (!client.connected()) { | |
client.disconnect(); | |
while(!rc) { | |
rc = connectSrv(SERVER, PORT); | |
if (!rc) delay(1000); | |
} | |
} | |
client.monitor(); | |
int sensorReading = analogRead(0); | |
/* calculator for lux */ | |
float lx = aval2lx(sensorReading); | |
/* send a message */ | |
String message = "{"; | |
message += "\"CDS_A0\":"; | |
message += (int)lx; | |
message += "}"; | |
client.send(message); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment