#include ////////////////////// // WiFi Definitions // ////////////////////// const char WiFiAPPSK[] = "thenoseknows"; ///////////////////// // Pin Definitions // ///////////////////// const int LED_PIN = 5; // Thing's onboard, green LED const int ANALOG_PIN = A0; // The only analog pin on the Thing const int DIGITAL_PIN = 12; // Digital pin to be read WiFiServer server(80); //used to convert temperatureF to an integer int temp_int; float voltage_original; int i; void setup() { initHardware(); //Switch vaiable to record orginal voltage i = 0; setupWiFi(); server.begin(); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int val = -1; // We'll use 'val' to keep track of both the // request type (read/set) and value if set. if (req.indexOf("/led/0") != -1) val = 0; // Will write LED low else if (req.indexOf("/led/1") != -1) val = 1; // Will write LED high else if (req.indexOf("/diag") != -1) val = -2; // Will print pin reads // Otherwise request will be invalid. We'll say as much in HTML else if (req.indexOf("/temp") != -1) val = -3; // Will print temp page //Set GPIO5 according to the request if (val >= 0) digitalWrite(LED_PIN, val); //getting the voltage reading from the temperature sensor int ADCreading = analogRead(ANALOG_PIN); byte ledStatus = LOW; //Scale to voltage float voltage = ADCreading * 3.2; //set voltage_orignal if (i == 0) { voltage_original = voltage; i = 1; } //Steinhart–Hart voltage to temp conversion float Temp = log(((10240000/ADCreading) - 10000)); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); float temperatureC = Temp - 273.15; // Convert Kelvin to Celsius float temperatureF = (temperatureC * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius //Clear out previous print client.flush(); // Prepare the response. Start with the common header: String s = "HTTP/1.1 200 OK\r\n"; s += "Content-Type: text/html\r\n\r\n"; s += "\r\n\r\n"; // If we're setting the LED, print out a message saying we did if (val >= 0) { s += "LED is now "; s += (val)?"on":"off"; s += "
\r\n"; s += "\r\n"; s += "
"; } if (val == -2) { // If we're reading pins, print out those values: s += "\r\n"; s += "\r\n"; s += ""; s += """\r\n"; s += ""; s += "\r\n"; s += ""; s += "Temp Sensor Diag"; s += ""; s += "
"; // Go to the next line. s += ""; s += "
"; // Go to the next line. s += "

"; s += "Analog Pin = "; s += String(analogRead(ANALOG_PIN)); s += "
"; // Go to the next line. s += "Digital Pin 12 = "; s += String(digitalRead(DIGITAL_PIN)); s += "
"; // Go to the next line. s += "Voltage = "; s += String(voltage); s += "
"; // Go to the next line. s += "Voltage_Org = "; s += String(voltage_original); s += "
"; // Go to the next line. s += "Temperature Celcius = "; s += String(temperatureC); s += "
"; // Go to the next line. s += "Temperature Fahrenheit = "; s += String(temperatureF); s += "
"; // Go to the next line. s += "

"; s += "
\r\n"; s += "\r\n"; s += "
"; s += ""; s += "\n"; } if ( val == -3 ) { s += "\r\n"; s += "\r\n"; s += "
"; // Go to the next line. s += ""; s += ""; s += "\r\n"; s += ""; s += "Beau's Crate Temp"; s += ""; s += "
"; // Go to the next line. s += ""; s += "
"; // Go to the next line. s += "\r\n"; float temperature = round(temperatureF*10)/10; temp_int = (int) temperature; String Temp_str = String(temp_int); //Change background based on temp if ( temperatureF <= 60 ) { s += "\r\n"; s += ""; } else if (( temperatureF > 60 ) && (temperatureF <= 75)) { s += "\r\n"; s += ""; } else if (( temperatureF > 75 ) && (temperatureF <= 90)) { s += "\r\n"; s += ""; } else if ( temperatureF > 90 ) { s += "\r\n"; s += ""; } //White letters do not show well with yellow circle if (( temperatureF > 60 ) && (temperatureF <= 75)) s += "" + Temp_str + """\r\n"; else s += "" + String (temperatureF) + """\r\n"; s += ""; s += "
\r\n"; s += "\r\n"; s += "
"; s += "
"; s += "
\r\n"; s += "\r\n"; s += "
"; s += "
"; s += "
\r\n"; s += "\r\n"; s += "
"; s += ""; s += ""; } else if (val >= 2) { s += "Invalid Request.
Try /led/1, /led/0, /read or /svg."; } s += "\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disonnected"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed } void setupWiFi() { WiFi.mode(WIFI_AP); String AP_NameString = "SCSARDA Crate Temp"; char AP_NameChar[AP_NameString.length() + 1]; memset(AP_NameChar, 0, AP_NameString.length() + 1); for (int i=0; i