I need to print temperature data on a local browser. and get a string from the browser (which will be typed in a textbox on the browser page) as input to a char in arduino. But I am not able to get the client, I checked by echo but the
if(client)
loop is not being executed. Here are all of my codes and custom libraries. some codes have been added in the attachment for character limits.
testlib.ino
#include "testlib.h"
#include "textboxfunc.h"
#include "textboxreceive.h"
#include "Wire.h"
#include <inttypes.h>
#include <Wire.h>
#include <Arduino.h>
#include <SHT2x.h>
#include <Ethernet.h>
#include <SPI.h>
testlib testcase;
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print("a");
testcase.setup_system();
}
void loop() {
testcase.looping_system();
Serial.print("b");
delay (2000);
}
testlib.cpp
#include <inttypes.h>
#include <Wire.h>
#include <Arduino.h>
#include <SHT2x.h>
#include <Ethernet.h>
#include <SPI.h>
#include "testlib.h"
#include "textboxfunc.h"
#include "textboxreceive.h"
void testlib::setup_system()
{
IPAddress ip(192, 168, 1, 129);
EthernetClient client;
EthernetServer server(80);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Setting MAC Address
if (Ethernet.begin(mac) == 0)
{
}
else
{
Ethernet.begin(mac, ip);
server.begin();
}
// delay(1000);
}
void testlib::looping_system()
{
float humidityData;
float temperatureData;
IPAddress ip(192, 168, 1, 129);
EthernetServer server(80);
char newip;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Setting MAC Address
textboxreceive textboxreceiveTRY;
Serial.print("ff");
newip= textboxreceiveTRY.textboxdata();
Serial.print(newip);
if (newip="")
{
Serial.print("dd");
IPAddress ip(192, 168, 1, 129);
Serial.print(ip);
}
else
{
// Serial.print(ip);
IPAddress ip(newip);
}
if (Ethernet.begin(mac) == 0)
{
}
else
{
Ethernet.begin(mac, ip);
Serial.print("ip Connected");
server.begin();
}
EthernetClient client = server.available();
Serial.print("hh");
humidityData = (SHT2x.GetHumidity());
temperatureData = (SHT2x.GetTemperature());
// listen for incoming clients
if (client) {
Serial.print("kk");
// 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>");
// output the value of sensor data
client.print("temperature value is: ");
client.print(temperatureData);
client.println("humidity value is: ");
client.print(humidityData);
client.println("
");
client.println("</html>");
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(1);
// close the connection:
client.stop();
}
}
textboxreceive.cpp
#include <inttypes.h>
#include <Wire.h>
#include <Arduino.h>
#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>
#include "textboxfunc.h"
#include "textboxreceive.h"
char textboxreceive::textboxdata()
{
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 90
// size of buffer that stores the incoming string
#define TXT_BUF_SZ 50
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 129); // 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
char txt_buf[TXT_BUF_SZ] = {0}; // buffer to save text to
textboxfunc textboxfuncTRY;
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
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);
server.begin();
Serial.print("c");
EthernetClient client = server.available(); // try to get client
Serial.print("c1");
if (client) { // got client?
boolean currentLineIsBlank = true;
Serial.print("d");
while (client.connected()) {
Serial.print("e");
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c;
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
if (textboxfuncTRY.StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
if (textboxfuncTRY.GetText(txt_buf, TXT_BUF_SZ)) {
Serial.println("\r\nReceived Text:");
Serial.println(txt_buf);
}
}
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()) {
client.write(webFile.read());
}
webFile.close();
}
}
req_index = 0;
textboxfuncTRY.StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
return txt_buf;
}
can you please bail me out?
testlib.zip (5.47 KB)