I'm trying to connect to localhost xampp server on a computer connected to a switch router, I do IPconfig and see there isnt any default network IP and I remember hearing that switch's use mac address to communicate between other computers.
Im getting failed to connnect from my serialconnect.
PS I also cant seem to map my analog signal, I tried a x = map(x 0, 1023, 0, 255) but the variable when called from serial and the server does return anything, Ive been just using the raw inout value for testing purposes.
#include <WString.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,9); // ip in lan
IPAddress gateway(192,168,1,1); // internet access via router
IPAddress subnet(255,255,255,0); //subnet mask
IPAddress myserver(192,168,1,4); // zoomkat web page
EthernetServer server(80); //server port
EthernetClient client;
String readString = String(30);
int chlorinelvl = A0;
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100000; // interval at which to blink (milliseconds)
boolean data= LOW;
void setup(){
Serial.begin(9600);
Ethernet.begin(mac, ip, subnet, gateway);
pinMode(3, OUTPUT); digitalWrite(3, LOW); // led 1
pinMode(4, OUTPUT); digitalWrite(4, LOW); // used as a ground
pinMode(8, OUTPUT); digitalWrite(8, LOW); // led 2
pinMode(9, OUTPUT); digitalWrite(9, LOW); // used as a ground
pinMode(chlorinelvl, INPUT);
}
void loop(){
EthernetClient client = server.available();
int chlorinedata = 0;
chlorinedata = analogRead(chlorinelvl);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
Serial.print("hi");
data= !data;
Serial.print(digitalRead(data));
// set the LED with the ledState of the variable:
if (data==HIGH){
Serial.print("data working") ;
sendGET();
Serial.print(analogRead(chlorinelvl));
if(chlorinedata>=0){
sendHELP(); }
}
}
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100){
readString.concat(c);
}
if (c == '\n') {
if(readString.indexOf("led8") >=0) {
toggle(8);
}
if(readString.indexOf("led3") >=0) {
toggle(3);
}
client.println("<html><center>");
client.println("Light 3
");
if (digitalRead(3) == LOW){
client.println("Light 3 OFF");
} else if (digitalRead(3) == HIGH){
client.println("Light 3 ON");
}
client.println("<form method='get'><input type=submit name=led3 value=toggle></form>");
client.println("Light 8
");
if (digitalRead(8) == LOW){
Serial.println("off LED");
client.println("Light off 8
");
}
else if (digitalRead(8) == HIGH){
client.println("Light ON 8
");
}
client.println("<form method='get'><input type=submit name=led8 value=toggle></form>");
client.println("</center></html>");
readString="";
client.stop();
}
}
}
}
}
int toggle(int pin){
if(digitalRead(pin) == LOW){
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
}
int chlorinedata=analogRead(chlorinelvl);
void sendGET() //client function to send/receie GET request data.
{
if (client.connect(myserver, 80)) {
Serial.println("connected");
client.print("GET http://localhost/handler.php?watt=");
client.print(analogRead(chlorinelvl));
client.println(" HTTP/1.1");
client.println("Host: 192.168.0.126");
client.println("Connection: close");
client.println();
Serial.print(chlorinedata);
}
else {
Serial.println("connection failed");
Serial.println();
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop();
}
void sendHELP() //client function to send/receie GET request data.
{
if (client.connect(myserver, 80)) {
Serial.println("connected");
client.print("GET http://localhost/email.php?watt=");
client.print(chlorinedata);
client.println(" HTTP/1.1");
client.println("Host: 192.168.0.126");
client.println("Connection: close");
client.println();
Serial.print(chlorinedata);
}
else {
Serial.println("connection failed");
Serial.println();
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop();
}