1

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?

3
  • 2
    shouldn't this be if (%c == 1)? Commented May 8, 2012 at 8:09
  • True, though the compiling errors don't notice Javascript errors -- but you're entirely correct and I'll change it. :) Commented May 8, 2012 at 9:11
  • I've changed the question to ==. Arguably, though, since this is Javascript it would be better to be ===... Commented May 8, 2012 at 13:02

2 Answers 2

1

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");
}
Sign up to request clarification or add additional context in comments.

4 Comments

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!
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!
The compiler complains about a lack of a parenthesis before PRIu8. "error: expected ')' before 'PRIu8'" Any ideas? :)
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!
1

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.