Last active
August 29, 2015 14:12
-
-
Save igorblumberg/3fba72c56b072133bb58 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
/* | |
WiFi Web Server | |
A simple web server that shows the value of the analog input pins. | |
using a WiFi shield. | |
This example is written for a network using WPA encryption. For | |
WEP or WPA, change the Wifi.begin() call accordingly. | |
Circuit: | |
* WiFi shield attached | |
* Analog inputs attached to pins A0 through A5 (optional) | |
created 13 July 2010 | |
by dlf (Metodo2 srl) | |
modified 31 May 2012 | |
by Tom Igoe | |
modified 29 Dec 2014 | |
by Igor Blumberg | |
*/ | |
#include <SPI.h> | |
#include <WiFi.h> | |
char ssid[] = "suarede"; // your network SSID (name) | |
char pass[] = "suasunha"; // your network password | |
int keyIndex = 0; // your network key Index number (needed only for WEP) | |
int status = WL_IDLE_STATUS; | |
WiFiServer server(80); | |
void setup() { | |
//Initialize serial and wait for port to open: | |
pinMode(6, OUTPUT); | |
pinMode(8, OUTPUT); | |
pinMode(5, OUTPUT); | |
digitalWrite(5, HIGH); //relay is off on high | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for Leonardo only | |
} | |
// check for the presence of the shield: | |
if (WiFi.status() == WL_NO_SHIELD) { | |
Serial.println("WiFi shield not present"); | |
// don't continue: | |
while(true); | |
} | |
// attempt to connect to Wifi network: | |
while ( status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | |
status = WiFi.begin(ssid, pass); | |
// wait 10 seconds for connection: | |
delay(10000); | |
} | |
server.begin(); | |
// you're connected now, so print out the status: | |
printWifiStatus(); | |
} | |
void loop() { | |
float voltage,degreesC; | |
int stringPos = 0; | |
voltage = getVoltage(0); | |
degreesC = (voltage - 0.5) * 100.0; | |
// listen for incoming clients | |
WiFiClient client = server.available(); | |
if (client) { | |
String readString; | |
int firstParameterPos, secParameterPos; | |
String firstParameter, secParameter; | |
Serial.println("new client"); | |
// an http request ends with a blank line | |
boolean currentLineIsBlank = true; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
if (readString.length() < 100) { | |
//store characters to string | |
readString += c; | |
} | |
if (c == '\n' && currentLineIsBlank) { | |
// send a standard http response header | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println("Connection: close"); // the connection will be closed after completion of the response | |
// client.println("Refresh: 5"); // refresh the page automatically every 5 sec | |
client.println(); | |
/***** HTML !1 ****** | |
client.println("<!DOCTYPE HTML>"); | |
client.println("<html>"); | |
// output the value of each analog input pin | |
for (int analogChannel = 0; analogChannel < 6; analogChannel++) { | |
int sensorReading = analogRead(analogChannel); | |
client.print("analog input "); | |
client.print(analogChannel); | |
client.print(" is "); | |
client.print(sensorReading); | |
client.println("<br />"); | |
} | |
client.println("A temperatura e: "); | |
client.print(degreesC); | |
client.println("O led1 e: "); | |
client.print(led1); | |
client.println("O led2 e: "); | |
client.print(led2); | |
client.println("</html>"); | |
/*********** HTML1 ***********/ | |
client.println("<!DOCTYPE html>"); | |
client.println("<html>"); | |
client.println("<body>"); | |
client.println("<h1>Igor's room control</h1>"); | |
client.println("<hr>"); | |
client.println("<form name=\"input\" action=\"\" method=\"get\">"); | |
client.println(" Display Number: <input type=\"text\" name=\"1\">"); | |
client.println(" <select name=\"2\">"); | |
client.println(" <option value=\"r\">red</option>"); | |
client.println(" <option value=\"y\">yellow</option>"); | |
client.println(" <option value=\"e\">green</option>"); | |
client.println(" </select>"); | |
client.println(" <input type=\"submit\" value=\"Submit\">"); | |
client.println("</form>"); | |
client.println("<hr>"); | |
client.println("</body>"); | |
client.println("</html>"); | |
delay(10); | |
Serial.print("readString = "); | |
Serial.println(readString); //print to serial monitor for debugging | |
firstParameterPos = readString.indexOf('='); | |
secParameterPos = readString.indexOf('=',firstParameterPos + 1); | |
firstParameter = readString.substring(firstParameterPos + 1 ,firstParameterPos + 2); | |
secParameter = readString.substring(secParameterPos + 1 ,secParameterPos + 2); | |
Serial.print("firstParam = "); | |
Serial.println(firstParameter); //print to serial monitor for debugging | |
Serial.print("secParam = "); | |
Serial.println(secParameter); //print to serial monitor for debugging | |
Serial.print("temp = "); | |
Serial.println(degreesC); //print to serial monitor for debugging | |
if(secParameter == "r") | |
{ | |
digitalWrite(6, HIGH); | |
digitalWrite(8, LOW); | |
digitalWrite(5, HIGH); //relay is off on high | |
} | |
else if(secParameter == "y") | |
{ | |
digitalWrite(6, LOW); | |
digitalWrite(5, HIGH); //relay is off on high | |
digitalWrite(8, HIGH); | |
} | |
else if(secParameter == "e") | |
{ | |
digitalWrite(6, LOW); | |
digitalWrite(8, LOW); | |
digitalWrite(5, LOW);//relay is ON on high | |
} | |
break; | |
} | |
if (c == '\n') { | |
// you're starting a new line | |
currentLineIsBlank = true; | |
} | |
else if (c != '\r') { | |
// you've gotten a character on the current line | |
currentLineIsBlank = false; | |
} | |
} | |
} | |
// give the web browser time to receive the data | |
delay(10); | |
// close the connection: | |
client.stop(); | |
Serial.println("client disconnected"); | |
} | |
} | |
void printWifiStatus() { | |
// print the SSID of the network you're attached to: | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
// print your WiFi shield's IP address: | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
// print the received signal strength: | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI):"); | |
Serial.print(rssi); | |
Serial.println(" dBm"); | |
} | |
float getVoltage(int pin) | |
{ | |
// This function has one input parameter, the analog pin number | |
// to read. You might notice that this function does not have | |
// "void" in front of it; this is because it returns a floating- | |
// point value, which is the true voltage on that pin (0 to 5V). | |
// You can write your own functions that take in parameters | |
// and return values. Here's how: | |
// To take in parameters, put their type and name in the | |
// parenthesis after the function name (see above). You can | |
// have multiple parameters, separated with commas. | |
// To return a value, put the type BEFORE the function name | |
// (see "float", above), and use a return() statement in your code | |
// to actually return the value (see below). | |
// If you don't need to get any parameters, you can just put | |
// "()" after the function name. | |
// If you don't need to return a value, just write "void" before | |
// the function name. | |
// Here's the return statement for this function. We're doing | |
// all the math we need to do within this statement: | |
return (analogRead(pin) * 0.004882814); | |
// This equation converts the 0 to 1023 value that analogRead() | |
// returns, into a 0.0 to 5.0 value that is the true voltage | |
// being read at that pin. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment