issues: so it work reading data from webpage to get data, but it only will get once after that it wont update the data even though it's in the loop
#include <math.h>
#include <Wire.h>
#include "rgb_lcd.h"
#include <Ethernet.h>
#include <PubSubClient.h>
//server
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x2E, 0x03 }; //pls change to your arduino MAC, refer to the sticker on the side
IPAddress server(192, 168, 10, 122); //change to the php server ip
EthernetClient client;
//preset if statement
int reading = LOW;
int state = HIGH;
int previous = LOW;
int buttonPin = 6;
int stateTime = 0;
int debounce = 100;
//lcd colour
rgb_lcd lcd;
const int colorR = 255;
const int colorG = 255;
const int colorB = 255;
//define
int buzz=4;
int light=3;
String coolant;
String temperature;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buzz, OUTPUT);
pinMode(light, OUTPUT);
Serial.begin(9600);
Serial.println("Read data from database");
Ethernet.begin(mac);
delay(1500); //wait for connection
//print connection detail
Serial.print("Arduino IP Address : "); //ip
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
}
void loop() {
read_arduinodata();
// read the pushbutton input pin
reading = digitalRead(buttonPin);
// determine if the button is pressed with debounce
if (reading == HIGH && previous == LOW && (millis() - stateTime) > debounce)
{
Serial.println("Button PUSHED");
if (state == HIGH)
{
state = LOW;
Serial.println("Status changed to LOW");
lcd.begin(16, 2);
lcd.print("ON");
delay(2000);
}
else {
state = HIGH;
Serial.println("Status changed to HIGH");
lcd.begin(16, 2);
lcd.print("OFF");
delay(2000);
lcd.noDisplay();
delay(2000);
lcd.setRGB(colorR, colorG, colorB);
}
stateTime = millis();
}
if (state == LOW & client.connect(server, 80))
{
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
if (coolant == "1" )
{
digitalWrite(buzz,HIGH);
lcd.setCursor(0, 0);
lcd.print("coolant:");
lcd.print("ABNORM");
}
if (temperature == "1")
{
digitalWrite(light,HIGH);
lcd.setCursor(0, 1);
lcd.print("temp:");
lcd.print("ABNORM" );
}
else
{
digitalWrite(buzz,LOW);
digitalWrite(light,LOW);
//lcd
lcd.setCursor(0, 0);
lcd.print("coolant:");
lcd.print("NORM");
lcd.setCursor(0, 1);
lcd.print("temp:");
lcd.print("NORM" );
delay(1000);
}
}
// update previous button state reading
previous = reading;
}
void read_arduinodata(){
if (client.connect(server, 80))
{
Serial.println("-> Connected");
Serial.println("Reading");
client.print("GET http://localhost/test.php"); //change based on the server ip
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println( "Connection: close" );
client.println();
String receive = client.readString();
//Serial.println(receive); //activate this line to see the actual text you receive
//Serial.println(receive.length()); //activate this line to see the length of text you receive
coolant=receive.substring(receive.length()-93,receive.length()-94);
temperature=receive.substring(receive.length()-36,receive.length()-37);
Serial.print("coolant:");
Serial.println(coolant);
Serial.print("temperature:");
Serial.print(temperature);
client.println();
}
}