Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm programming a simple web server on an Arduino. I basically send HTML, CSS and Javascript to a client through a method in C code. Now I've come to need a C variable in a Javascript if-case, and I need to do it without using ASP.NET.

client.println("if (%c == 1) {",stat[0]);

I tried changing the data type, I tried using a Java-style with plus signs, I even tried storing it in a string first and then sending it in; nothing works.

stat[0] is declared and changes over time.

The error I get from the compiler:

Webserver4.cpp:217:40: error: call of overloaded 'println(const char [15], byte&)' is ambiguous C:\mpide-0023-windows-20111221\hardware\pic32\cores\pic32/Print.h:66:7: note: candidates are: void Print::println(char, int) (...)

Any ideas?

share|improve this question
2  
shouldn't this be if (%c == 1)? –  thumbmunkeys May 8 '12 at 8:09
    
True, though the compiling errors don't notice Javascript errors -- but you're entirely correct and I'll change it. :) –  Michael Kaesy May 8 '12 at 9:11
    
I've changed the question to ==. Arguably, though, since this is Javascript it would be better to be ===... –  Matthew Murdoch May 8 '12 at 13:02

2 Answers 2

up vote 1 down vote accepted

The compilation problem is due to the printXxx() functions available on the client object not supporting printf format specifiers. Whilst it's not obvious what the type of client is, it evidently subclasses the built-in Arduino Print class.

To use the format specifiers you have to do it in two steps, for example (note that a byte in Arduino is actually a uint8_t and PRIu8 is the format specifier for this type):

#define __STDC_LIMIT_MACROS 1
#include <inttypes.h>

// ...

char ifStatement[13];
sprintf(ifStatement, "if (%" PRIu8 " == 1) {", stat[0]);
client.println(ifStatement);

client.println("// JavaScript code to execute if stat[0] == 1");

client.println("}");

Also, note that this assumes that stat[0] is always a single digit. If it has two or more digits the ifStatement buffer will overflow, corrupting memory.

However, since (as unwind points out) the JavaScript if statement is essentially constant from the perspective of the browser, it would perhaps be simpler to do this in the Arduino code:

if (stat[0] == 1) {
    client.println("// JavaScript code to execute if stat[0] == 1");
}
share|improve this answer
    
Stat[0] is always either a 1 or a 0. I essentially want different parts of the Javascript code to run depending on what stat[0] is; and what it is will change from time to time. If I use your second suggestion, it means that any change in stat[0] won't be registered when the site is refreshed, correct? It shouldn't remain constant since I can print: "client.println(stat[0]);" directly onto the webpage. All of this is encased in a method that creates a client connection. So I'll try your first idea out later, thanks for the tip! –  Michael Kaesy May 9 '12 at 1:00
    
I had assumed that refreshing the site would cause the Arduion code to regenerate the JavaScript code, in which case changes to stat[0] will be registered. If that's not true I'll update the answer. Thanks! –  Matthew Murdoch May 9 '12 at 11:05
    
The compiler complains about a lack of a parenthesis before PRIu8. "error: expected ')' before 'PRIu8'" Any ideas? :) –  Michael Kaesy May 10 '12 at 6:30
    
Ah, in the documentation (nongnu.org/avr-libc/user-manual/group__avr__inttypes.html) it says that you must #define __STDC_LIMIT_MACROS before #include <inttypes.h>. I've updated the answer with this. Thanks! –  Matthew Murdoch May 10 '12 at 8:16

As a commenter has noted, it should probably be if (%c == 1) in the emitted Javascript code.

Also, note that the expression you're emitting is constant, so you could just as well re-factor your C code to emit the proper code (the code that is taken if the if evaluates to true) directly, and reduce the complexity of the emitted code.

You're not showing the C declaration of stat, which makes it hard to be sure you're doing it right. For the %c formatting code, and the array indexing, it should be an array of characters, which is a bit odd since the code you're emitting is comparing against an integer.

share|improve this answer
    
There's a bit of an issue with using If-cases directly with C-code, if that's what you mean. Stat is an array declared as a byte (I tried %b), and stat[0] is either 0 or 1. –  Michael Kaesy May 8 '12 at 9:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.