Hello!
I'm using an arduino as a webserver and webclient at the same time using ethernet shield. I need it to send data to mysql and also receive commands.
For now i only control two relays, one led and receive data from dht22 sensor, but i need to put few more sensors and the memory it's almost full.
Sketch uses 19,340 bytes (59%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,527 bytes (74%) of dynamic memory, leaving 521 bytes for local variables. Maximum is 2,048 bytes.
How can i improve this? I already bought esp8266 to replace ethernet shield.
What is the best solution? i know that i can write code on that board. what is the memory? (dinamic memory and storage space) Can i make esp8266 to run the webserver and webclient while i can read data from sensors and control relays,led using arduino uno/micro/ etc?
Can i split webserver and webclient on two arduino and make them to comunicate eachother?
It is possible to receive commands from browser without having to create a webclient?
How can i show data directly from arduino on a different web server?
My code:
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Adresa MAC
IPAddress ip(192, 168, 0, 123); // ip in LAN
IPAddress myDns(8, 8, 8, 8); // server DNS
//IPAddress myserver(192,168,0,114); // ip webhost apache
char myserver[] = "deseuridolj.ro"; // domeniu webhost apache
EthernetServer server(84); // portului webserver-ului arduino
EthernetClient client;
String readString;
#define rel1 6 // definirea numelor pentru pinii folositi in a controla releele
#define rel2 7
////////////////////// inceput Termistor/umiditate
#define DHTPIN 2 // Digital Pin 2
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
//////////// sfarsit Termistor
// Variabile
unsigned long previousMillis = 0; // will store last time LED was updated
// constante
const long interval = 60000; // intervalul pentru webclient (milliseconds)
void setup() { // vor fi rulate o singura data cand porneste microcontrollerul
Ethernet.begin(mac, ip, myDns);
server.begin();
Serial.begin(9600);
Serial.println("ok"); // Mesaj fisat in consola pentru debug
pinMode(rel1, OUTPUT); // pin ales pentru a controla releul 1 este setat pentru semnal de iesire
pinMode(rel2, OUTPUT); // pin ales pentru a controla releul 1
pinMode(4, OUTPUT); // pin ales pentru a controla led ul de pe pin-ul 4
dht.begin();
}
void loop(){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Fail read temp");
return;
}
// here is where you'd put code that needs to be running all the time.
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n')
{
///////////////
Serial.println(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println("HTTP/1.1 204 OK");
client.println();
client.println();
}
else {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Hello</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<b>Led Control:</b>
");
if( digitalRead(4)){
client.println("<a href=\"/?ledoff\" target=\"inlineframe\">Turn the Led OFF</a>
");
}
else {
client.println("<a href=\"/?ledon\" target=\"inlineframe\">Turn the Led ON</a>
");
}
// conditii relee afiseaza comanda disponibila
client.println("<b>Relay Control:</b>
");
if( !digitalRead(rel1)){
client.println("<a href=\"/?rel1off\" target=\"inlineframe\">Relay1 is opened. Close it!</a> -");
}
else {
client.println("<a href=\"/?rel1on\" target=\"inlineframe\">Relay1 is closed. Open it!</a> -");
}
if( !digitalRead(rel2)){
client.println("<a href=\"/?rel2off\" target=\"inlineframe\">Relay2 is opened. Close it!</a>
");
}
else {
client.println("<a href=\"/?rel2on\" target=\"inlineframe\">Relay1 is closed. Open it!</a>
");
}
client.println("
Temperatura=");
if(t >=20) {
client.println("<font color='red'>");
client.println(t);
client.println("*C</font>");
}
else {
client.println("<font color='blue'>");
client.println(t);
client.println("</font>");
}
client.println("
Umiditatea=");
if(h == 70) {
client.println("<font color='green'>");
client.println(h);
client.println("%</font>");
}
else {
client.println("<font color='red'>");
client.println(h);
client.println("</font>");
}
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("ledon") >0)//checks for on
{
digitalWrite(4, HIGH); // set pin 4 high
}
if(readString.indexOf("ledoff") >0)//checks for off
{
digitalWrite(4, LOW); // set pin 4 low
}
//clearing string for next read
//control relay 1
if(readString.indexOf("rel1on") >0)//checks for on
{
digitalWrite(rel1,0); // set pin 4 high
}
if(readString.indexOf("rel1off") >0)//checks for off
{
digitalWrite(rel1,1); // set pin 4 low
}
//control relay 2
if(readString.indexOf("rel2on") >0)//checks for on
{
digitalWrite(rel2,0); // set pin 4 high
}
if(readString.indexOf("rel2off") >0)//checks for off
{
digitalWrite(rel2,1); // set pin 4 low
}
readString="";
}
}
}
}
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED - ultima oara cand a accesat link-ul
previousMillis = currentMillis;
// daca a trecut timpul acceseaza din nou link-ul
sendGET(); // acceseaza link-ul (webclient)
}
}
void sendGET() // functia webclient - send/receie GET request data.
{
// temperatura
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
client.stop();
if (client.connect(myserver, 80))
{
Serial.println("connected");
client.print("GET /i.php?t=");
client.print(t);
client.print("&h=");
client.print(h);
client.println(" HTTP/1.1");
client.println("Host: deseuridolj.ro");
client.println("Connection: close");
client.println();
}
else {
Serial.println("confail");
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println("Decon");
Serial.println();
client.stop();
}