Greetings everyone!
I am using an Arduino Mega 2560 with Arduino.org Ethernet Shield ver. 2 and a 16x2 1602A lcd.
I have an analog temp sensor and a DHT11 temp/humidity sensor and am hosting a webpage from the SD card and updating values from the sensors. The webpage portion has been working great. I decided to add an LCD so that I can display temp and humidity locally.
My LCD will display anything that I send to it in the setup loop, but has not displayed anything that I send to it during the main loop.
I have tried delays, printing to either line, initializing the LCD pins in the main loop, specifically defining the pins as OUTPUTs, nothing seems to make a difference. With the current code posted below, the two lines in setup display perfectly, then the display is cleared in the main loop. That is all. The rest of the code (webpage and XML) function normally.
I'm quite new at this, I know the code is sloppy still and there are some pieces that I don't intend to keep, but not sure what would be keeping it from printing to the LCD in the main loop (currently the word "Nova").
#include <SPI.h>
#include <Ethernet2.h>
#include <SD.h>
#include <dht11.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(52, 53, 29, 27, 25, 23); //(RS, EN, DB4, DB5, DB6, DB7)
// Math section for Steinhart-Hart temperature equation
double Thermistor(int RawADC) {
double Temp;
Temp = log(10000.0/(1024.0/RawADC-1)); // =log(10000.0/(1024.0/RawADC-1)) <-- for pull-up configuration/for other configuration --> =log(10000.0*((1024.0/RawADC-1)))
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
// Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
// Math section for dewpoint (fast) calculation
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 50
#define DHT11PIN 2
dht11 DHT11;
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x21, 0xF2 };
IPAddress ip(192, 168, 50, 148); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(23, OUTPUT);
pinMode(25, OUTPUT);
pinMode(27, OUTPUT);
pinMode(29, OUTPUT);
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("NIS 1151 Inkjet");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{
int chk = DHT11.read(DHT11PIN);
//lcd.begin(16, 2);
//lcd.clear();
lcd.setCursor(0, 0);
delay(500);
lcd.print("Nova");
delay(500);
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
// send XML file containing input states
XML_response(client);
}
else { // web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open("index.htm");
if (webFile) {
while(webFile.available()) {
int num_bytes_read;
char byte_buffer[64];
num_bytes_read = webFile.read(byte_buffer, 64);
client.write(byte_buffer, num_bytes_read); // send web page to client
}
webFile.close();
}
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// send the XML file containing analog value
void XML_response(EthernetClient cl)
{
int analog_val_1 = 0;
int analog_val_2 = 0;
int analog_val_3 = 0;
int analog_val_4 = 0;
char sample;
// get the sum of 10 samples from analog inputs 2 and 3
for (sample = 0; sample < 10; sample++) {
analog_val_1 += analogRead(2);
delay(2);
analog_val_2 += analogRead(3);
delay(2);
}
// calculate the average of the 10 samples
analog_val_1 /= 10;
analog_val_2 /= 10;
cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
// read analog pin A2
// analog_val = analogRead(2);
cl.print("<analog>");
cl.print(int(Thermistor(analogRead(2)))); //analog_val_1
cl.print("</analog>");
// analog_val = analogRead(3);
cl.print("<analog>");
cl.print((float)DHT11.temperature, 2);
cl.print("</analog>");
cl.print("<analog>");
cl.print((float)DHT11.humidity, 2);
cl.print("</analog>");
cl.print("<analog>");
cl.print(dewPointFast(DHT11.temperature, DHT11.humidity));
cl.print("</analog>");
cl.print("</inputs>");
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}