I just got an Arduino wifi shield.
I am using my Arduino to monitor temps for a brew mashing setup and using the wifishield to transmit the temps and post to a web page.
When I view the serial monitor, the arduino connects to the wireless network and then posts the temp. However, when I do not connect via serial monitor, it drops the connection and does not post the temperatures.
I used the wifi web server example code -I noticed that it didn't work without some modification. I need to clean up some of the stuff that has been commented out.
Here is my code.
#include <SPI.h>
#include <WiFi.h>
#include <OneWire.h>
char ssid[] = "network name"; // your network SSID (name)
char pass[] = "password"; // your network password
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
int deltaTemp = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
OneWire ds(DS18S20_Pin);
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(9600);
// 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:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
void loop()
{
// listen for incoming clients
WiFiClient client = server.available();
if (client)
{
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();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
//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();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");
client.println("Welcome to Yoyodyne Propulsion Systems brew temp monitor");
client.println("<br />");
client.print("Current Temp ");
float temperature = getTemp();
float tempF = temperature * 1.8;
tempF += 32;
client.print(" is ");
client.print(tempF);
Serial.println(tempF);
client.println("<br />");
//}
client.println("</body>");
client.println("</html>");
break;
// curly brace here?
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(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
} // where is matching?
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
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");
}