Printing with Wifly and PHP

Hello,

I am having problems printing the results of a PHP page with Wifly.

I'm trying to use a potentiometer to select which PHP to reference, and then a push button to print the results from a thermal receipt printer. When I run the sketch, I can see it connects in the Serial Monitor, but seems to get stuck after it prints "connected". If the push button is pressed again, it goes through the connection process again, but still does not output anything from PHP?

Any help would be really appreciated.

Thanks

#include <NewSoftSerial.h>


// (Based on Ethernet's WebClient Example)

#include "WiFly.h"


#include "Credentials.h"

#define rxPin 6
#define txPin 7

NewSoftSerial printer = NewSoftSerial(rxPin, txPin);

const byte command = 0x1B;
const byte fullcut = 0x69;

byte server[] = {192,168,0,1 }; // Google

//Client client(server, 80);

Client client("192.168.0.1", 80);


int pot = A0;
int searchButton = 13;
int potVal = 0;
int buttonVal = 0;
String web;

void setup() {
  
  pinMode(pot, INPUT);
  pinMode(searchButton, INPUT);
  
  printer.begin(9600);
  
  Serial.begin(9600);

  WiFly.begin();
  
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }  
}


void printOnPrinter(char text[]) {
  printer.print(text);
}


void feed() {
  printer.println("");
  printer.println("");
  printer.println("");
}


void cut() {
  printer.print(command, BYTE);
  printer.print(fullcut, BYTE);
}


void loop() {
  buttonVal = digitalRead(searchButton);
  if(buttonVal == 1){
    
    int potVal = analogRead(pot);
potVal = map(potVal, 0, 1023, 1, 5);
Serial.println(potVal);

if(potVal==1) {
  web = "GET /~paddy/nameTest1.php HTTP/1.0";
}

if(potVal==2) {
  web = "GET /~paddy/nameTest2.php HTTP/1.0";
}

if(potVal==3) {
  web = "GET /~paddy/nameTest3.php HTTP/1.0";
}

if(potVal==4) {
  web = "GET /~paddy/nameTest4.php HTTP/1.0";
}

if(potVal==5) {
  web = "GET /~paddy/nameTest5.php HTTP/1.0";
  }
    
    
    Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println(web);
    client.println();
  } else {
    Serial.println("connection failed");
  }
  
  while(client.available()){
  do{
    char c = client.read();
    Serial.print(c);
  }
  
  while(client.available());
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    feed();
    feed();
    cut();
    feed();
    feed();
    for(;;); 
  }
 }
}

Have you checked which readings come from the potvalue?
It looks strange to me to have values between 1 and 5 when the pot will give.

Also, you should have another look on how to re-program this bit

while(client.available()){
  do{
    char c = client.read();
    Serial.print(c);
  }
  
  while(client.available());
  }

to make it more readable, and probably solve your problem.

If the push button is pressed again, it goes through the connection process again, but still does not output anything from PHP?

I would be looking at the PHP scripts, then, to verify that they actually return something.

I second the need to remove that do/while loop from inside the while loop.

Sorry, the outer "while loop" was meant to be commented out. I had originally tried it with just a while loop which made no difference, so tried the do/while loop.

The PHP script seams ok. The PHP prints the results when I have code printing it automatically from start up, but when I introduce this button it does not want to work.

Could it be something to do with where the (client.connect()); is?

Could it be something to do with where the (client.connect()); is?

Depends. Do you have anything in the PHP script that PROVES that it was invoked, like writing a timestamp to a file on the PC?

If the PHP script isn't being executed, it won't generate any output. If it does get executed, the client.connect is OK where it is.

Note that nameTest5.php will hardly ever get called. If you want a more uniform distribution, change the from range to 0, 1024.

Interesting point about increasing the range to 0-1024, what effect does this have? Regarding the PHP, when it runs without the use of a push button it outputs (prints from the receipt printer) fine and does have a timestamp which it prints, but with the push button it only outputs a strange Y with two dots over it?!

Just seams weird why the button makes such a difference - you would think it would be something quite straight forward to do.

I've tried a new approach which is getting me closer, but tantalisingly not their yet!

I have now tried sending a '$' at the end of my PHP script, and adjusted the arduino code to collect and print the PHP result until it gets the '$'. This works ok, but even after it get the '$' it keeps printing a strange ÿ character?!

So close, please help to kill this off.

The new code is as follows:

#include <NewSoftSerial.h>


// (Based on Ethernet's WebClient Example)

#include "WiFly.h"


#include "Credentials.h"

#define rxPin 6
#define txPin 7

NewSoftSerial printer = NewSoftSerial(rxPin, txPin);

const byte command = 0x1B;
const byte fullcut = 0x69;

byte server[] = {192,168,0,1 }; // Google

//Client client(server, 80);

Client client("192.168.0.1", 80);


int pot = A0;
int searchButton = 13;
int potVal = 0;
int buttonVal = 0;
String web;
char c;

void setup() {
  
  pinMode(pot, INPUT);
  pinMode(searchButton, INPUT);
  
  printer.begin(9600);
  
  Serial.begin(9600);

  WiFly.begin();
  
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }  
}


void printOnPrinter(char text[]) {
  printer.print(text);
}


void feed() {
  printer.println("");
  printer.println("");
  printer.println("");
}


void cut() {
  printer.print(command, BYTE);
  printer.print(fullcut, BYTE);
}


void loop() {
  buttonVal = digitalRead(searchButton);
  if(buttonVal == 1){
    
    int potVal = analogRead(pot);
potVal = map(potVal, 0, 1020, 1, 5);
Serial.println(potVal);

if(potVal==1) {
  web = "GET /~paddy/nameTest1.php HTTP/1.0";
}

if(potVal==2) {
  web = "GET /~paddy/nameTest2.php HTTP/1.0";
}

if(potVal==3) {
  web = "GET /~paddy/nameTest3.php HTTP/1.0";
}

if(potVal==4) {
  web = "GET /~paddy/nameTest4.php HTTP/1.0";
}

if(potVal==5) {
  web = "GET /~paddy/nameTest5.php HTTP/1.0";
  }
    
    
    Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println(web);
    client.println();
  } else {
    Serial.println("connection failed");
  }
  
  
  do{
    char c = client.read();
//    if(c == '

){
//    client.stop();
//    }
    Serial.print(c);
  }
 
  while(c != '


);

  
  if (c == '

) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    feed();
    feed();
    cut();
    feed();
    feed();
    for(;;);
  }
}
}

Typically, the y with the two dots over it is what is printed when a -1 is printed as a character. This happens when a serial read is performed when there is nothing to read.

Test your code, calling each of the PHP scripts individually, ignoring the switch/potentiometer. Verify that each produces valid output.

Add a Serial.print() of the value in web, to make sure that the correct GET request is actually set up.

Interesting point about increasing the range to 0-1024, what effect does this have?

The map function deals in integers. It returns a value that is in the to range, if the input value is in the from range. With a from range of 0 to 1023, only one value in that range (1023) will result in the upper limit of the to range being returned. Since 1024 is not a possible output of the analogRead function, there is no danger of returning a value that is not in the to range.

Use client.available() to see if there are bytes available before reading, and client.connected() to see if the connection has been closed.