Skip to content

Instantly share code, notes, and snippets.

@davidkaste
Last active August 29, 2015 14:02
Show Gist options
  • Save davidkaste/fc11d179e00da1948250 to your computer and use it in GitHub Desktop.
Save davidkaste/fc11d179e00da1948250 to your computer and use it in GitHub Desktop.
Web server to get temperature and id with Arduino
#include <SerialGraphicLCD.h>//inculde the Serial Graphic LCD library
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x7F, 0xC2 };
IPAddress ip(192,168,1,177);
EthernetServer server(8080);
String HTTP_req;
#define maxX 127//159
#define maxY 63 //127
float tempMax = 30;
float tempMin = 25;
LCD lcd;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
lcd.setHome();
lcd.clearScreen();
}
void loop() {
String temp;
EthernetClient client = server.available();
if(client) {
Serial.println("New client");
boolean currentLineIsBlank = true;
while(client.connected()) {
if(client.available()) {
char c = client.read();
HTTP_req += c;
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("</html>");
Serial.println(HTTP_req);
temp = getValue(HTTP_req, ' ', 1);
Serial.println(temp);
printTempToLCD(temp);
HTTP_req = "";
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
break;
}
}
}
delay(1);
client.stop();
}
}
void printTempToLCD(String temp){
String client = getValue(temp, '/', 1);
String temperatura = getValue(temp, '/', 2);
char charBufClient[client.length()+1];
client.toCharArray(charBufClient, client.length()+1);
char charBufTemperatura[temperatura.length()+1];
temperatura.toCharArray(charBufTemperatura, temperatura.length()+1);
float tempf = atof(charBufTemperatura);
lcd.setHome();
lcd.clearScreen();
lcd.printStr("TEMPERATURA CORPORAL");
lcd.drawLine(0,maxY-8,maxX,maxY-8,2);
lcd.setX(0);
lcd.setY(maxY-12);
lcd.printStr("Pacient id: ");
lcd.printStr(charBufClient);
lcd.setX(0);
lcd.setY(maxY-20);
lcd.printStr(charBufTemperatura);
lcd.printStr(" graus");
lcd.setX(0);
lcd.setY(maxY-28);
if(tempf > tempMax) {
lcd.printStr("ALERTA!");
lcd.setX(0);
lcd.setY(maxY-36);
lcd.printStr("Superior al maxim");
}
if(tempf < tempMin) {
lcd.printStr("ALERTA!");
lcd.setX(0);
lcd.setY(maxY-36);
lcd.printStr("Inferior al minim");
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i = 0; i <= maxIndex && found <= index; i++) {
if(data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment