Well in my endeavour to learn Arduino Ethernet I'm still unable to get the Arduino web-client to accept commands from a server PC. Also I'm unable to get web-server to send data to a mysql database.
So I've written (probably poorly) code which I 95% understand that accepts commands from a browser.
http://192.168.1.19/?7=1 turns digital 7 on
http://192.168.1.19/?7=0 turns digital 7 off
http://192.168.1.19/?A=0 gets the value from analogue 0, prints what looks like the correct string in the web-browser but it won't write to a mysql.
#include <SPI.h>
//#include <WString.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC }; //physical mac address
byte ip[] = { 192, 168, 1, 19 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
//byte server[] = { 192,168,1,20 };
Server server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 4; // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
//#######################################
#define READING_PIN 0
double R1 = 10000.0; //resistance put in parallel
double V_IN = 5.0;
double A = 1.129148e-3;
double B = 2.34125e-4;
double C = 8.76741e-8;
double K = 9.5; // mW/dec C – dissipation factor
double SteinhartHart(double R)
{
// calculate temperature
double logR = log(R);
double logR3 = logR * logR * logR;
return 1.0 / (A + B * logR + C * logR3 );
}
int celsius;//Global declare celsius
//#######################################
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//pinMode;
pinMode(7,OUTPUT); digitalWrite(7,LOW); // I use this pin as GND for the LED.
pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
//enable serial datada print
Serial.begin(9600);
}
void loop(){
readString="";//Clear readString before filling
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 30) {
//store characters to string
readString.concat(c);
Serial.println(readString);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//lets check if LED should be lighted
Serial.println(readString.substring(6,9));//for test to allow readString to be viewed
if(readString.substring(6,9)=="7=1") {//read the last 3 information characters from URL
// Serial.println(readString);
//set digital 7 HIGH
Serial.println("HIGH");
digitalWrite(7, HIGH); // set the LED on
readString="";//clear readString
}
if(readString.substring(6,9)=="7=0") {
//set digital 7 LOW
Serial.println("LOW");
digitalWrite(7, LOW); // set the LED OFF
readString="";//clear readString
}
if(readString.substring(6,9)=="A=0") {
//readAnal 0 temp
ReadTemp();
Serial.println("for test");
client.print("GET http://192.168.1.20/update_db.php?celsius=");
client.print(celsius);
client.println(" HTTP/1.0");
client.println("Host: http://192.168.1.20");
client.println();
readString="";//clear readString
}
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}
//########################################################################
void ReadTemp() {
//Serial.print("I'm at ReadTemp ");
double adc_raw = analogRead(READING_PIN);
double V = adc_raw / 1024 * V_IN;//Serial.println(adc_raw);
double R_th = (R1 * V ) / (V_IN - V);//calculate resistance
double kelvin = SteinhartHart(R_th) - V*V/(K * R_th);
celsius = kelvin - 273.15;//was double but changed to int to remove decimal places
Serial.println();
delay(1000);
return;
}
//#######################################################################
Can anyone see and explain what's wrong as I've spent all day on this with no answer.
All I want is an Arduino with Ethernet Shield that can accept commands from a web-server PC and send values to a mysql database on the same to the web-server PC.
Thanks for any help.
Tony