Notice: This page contains information for the legacy Phidget21 Library. Phidget21 is out of support. Bugfixes may be considered on a case by case basis. Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21. We recommend that new projects be developed against the Phidget22 Library.
|
Use Phidgets Wirelessly with the SBC: Difference between revisions
No edit summary |
(→Code) |
||
Line 43: | Line 43: | ||
==Code== | ==Code== | ||
<div class="source"> | |||
<syntaxhighlight lang=cpp> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include "phidget21.h" | |||
int main(int argc, char* argv[]) { | |||
int result; | |||
int outputState; | |||
int i; | |||
CPhidgetInterfaceKitHandle interfaceKit = 0; | |||
CPhidgetInterfaceKit_create(&interfaceKit); | |||
CPhidget_openRemoteIP((CPhidgetHandle)interfaceKit, -1, "phidgetsbc.local", 5001, NULL); | |||
result = CPhidget_waitForAttachment((CPhidgetHandle)interfaceKit, 10000); | |||
if (result) { | |||
printf("No Device!\n"); | |||
return 0; | |||
} | |||
outputState = 1; | |||
CPhidgetInterfaceKit_setOutputState(interfaceKit, 0, outputState); | |||
for (i = 0; i < 1000; i++) { | |||
if (outputState) { outputState = 0; } | |||
else { outputState = 1; } | |||
CPhidgetInterfaceKit_setOutputState(interfaceKit, 0, outputState); | |||
} | |||
CPhidgetInterfaceKit_setOutputState(interfaceKit, 0, 0); | |||
CPhidget_close((CPhidgetHandle)interfaceKit); | |||
CPhidget_delete((CPhidgetHandle)interfaceKit); | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
</div> | |||
==Putting it All Together== | ==Putting it All Together== |
Revision as of 15:05, 19 March 2012
The project described here is a self-contained web server on the Phidget Single Board Computer (SBC). We compose and serve a complete HTTP page within C, and include Phidget data on it. We also accept data to control attached Phidgets in our request.
Practical concepts covered are (click on links to see other projects on that topic):
|
File:TBD |
As with any of our described projects, Phidgets takes care of the electrical component design. Here, we also provide in-depth instructions on installing external libraries on the SBC from source, and from an SVN trunk. We then show how to use the microhttpd.h library within C code to communicate with and control attached Phidgets.
Note: This is a complex way of doing something that the SBC can do much more simply. This page uses the simple example of using an Interface Kit over the network to demonstrate the much more complex concept of using embedded C HTTP servers. An embedded server is a powerful beast, and wanting to create one probably means you've grown needs above and beyond our simple Application Guides. The simpler guides are:
- If you just want to get Phidget data over the network, the SBC already does this naturally from the time it ships. We show an application in the Use Phidgets Wirelessly with the SBC guide.
Time: About a day
Special Needed Tools: A Phidget SBC, Ethernet and power cords (or Wireless), an LED, and your computer.
You should have already worked through the 1072 - Getting Started page before this one to have set up the network on your SBC and obtained its IP or local link address.
Introduction
When the SBC is powered on and connected to a network, it automatically handles any network requests for the Phidgets attached to it.
Phidgets
Code
#include <stdio.h>
#include <stdlib.h>
#include "phidget21.h"
int main(int argc, char* argv[]) {
int result;
int outputState;
int i;
CPhidgetInterfaceKitHandle interfaceKit = 0;
CPhidgetInterfaceKit_create(&interfaceKit);
CPhidget_openRemoteIP((CPhidgetHandle)interfaceKit, -1, "phidgetsbc.local", 5001, NULL);
result = CPhidget_waitForAttachment((CPhidgetHandle)interfaceKit, 10000);
if (result) {
printf("No Device!\n");
return 0;
}
outputState = 1;
CPhidgetInterfaceKit_setOutputState(interfaceKit, 0, outputState);
for (i = 0; i < 1000; i++) {
if (outputState) { outputState = 0; }
else { outputState = 1; }
CPhidgetInterfaceKit_setOutputState(interfaceKit, 0, outputState);
}
CPhidgetInterfaceKit_setOutputState(interfaceKit, 0, 0);
CPhidget_close((CPhidgetHandle)interfaceKit);
CPhidget_delete((CPhidgetHandle)interfaceKit);
return 0;
}