Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Pulse an arduino PWM pin by hitting a URL over the network
/**
Generic networked pulser
Sets up a web page and every time a url is hit then it conducts and action
Author: Andrew Fisher
Date: 29 September 2011
Version 0.2
Adapted from generic web server example as art of IDE created by David Mellis and Tom Igoe.
Circuit: Ethernet shield or other ethenet enabled Arduino on pins 10,11,12,13
An LED or some other device connected to a PWM PIN... which you map below.
Channels are as many PWM PINS as you have available - on a standard Arduino that's 4 so that's
how many channels you have available.
License:
Copyright (c) Andrew Fisher and individual contributors.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of generic_network_pulser nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
#include <SPI.h>
#include <Ethernet.h>
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINTDEC(x) Serial.print (x, DEC)
#define DEBUG_PRINTLN(x) Serial.println (x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTDEC(x)
#define DEBUG_PRINTLN(x)
#endif
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAE };
byte ip[] = { <YOUR IP ADDRESS> };
byte gateway[] = { <YOUR GATEWAY >};
byte subnet[] = { <YOUR SUBNET >};
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
#define BUFFERLENGTH 255
// define how many channles you want to use. Usually mapped to no. of PWM outputs you have
#define MAX_CHANNELS 4
// number of milliseconds to wait between each increment of fade.
#define FADE_TIME 6
int channels[MAX_CHANNELS]; // This is a mapping of channel numbers to pin numbers
double channel_timers[MAX_CHANNELS];
double last_time;
void setup() {
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
for (int i=0; i< MAX_CHANNELS; i++){
channel_timers[i]=0;
}
// set up each of your channels here as
channel[0] = 3; // etc this will point channel 0 to Digital PWM pin 3
// set up each of your channels as an output.
for (int i=0; i<MAX_CHANNELS; i++) {
if (channels[i] > 0) pinMode(channels[i], OUTPUT);
}
#ifdef DEBUG
Serial.begin(9600);
#endif
DEBUG_PRINTLN("Awaiting connection");
}
void loop() {
char buffer[BUFFERLENGTH];
int index = 0;
// Listen
Client client = server.available();
if (client) {
DEBUG_PRINTLN("Got a client");
// reset the input buffer
index = 0;
while (client.connected()) {
if (client.available()){
char c = client.read();
// if it's not a new line then add it to the buffer
if (c != '\n' && c != '\r') {
buffer[index] = c;
index++;
if (index > BUFFERLENGTH) index = BUFFERLENGTH -1;
continue;
}
// get the url string for processing
String urlstr = String(buffer);
// get just the url
urlstr = urlstr.substring(urlstr.indexOf('/'), urlstr.indexOf(' ', urlstr.indexOf('/')));
// rebuild the buffer with just the URL
urlstr.toCharArray(buffer, BUFFERLENGTH);
// now we get the parameters channel to pulse and time to pulse it
char *channel = strtok(buffer, "/");
char *time = strtok(NULL, "/");
int selectedChannel = channel[0] - '0'; // convert to int
if (channel == NULL || time == NULL || selectedChannel >= MAX_CHANNELS) {
// return error
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
DEBUG_PRINTLN("Error 404 not found");
} else {
// we have both bits of data so we now attempt to do something with them.
double selectedTime = atol(time);
pulser(selectedChannel, selectedTime);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
DEBUG_PRINTLN("200 OK");
}
break;
}
}
delay(10); // give client time to send the data back
DEBUG_PRINTLN("Stopping client");
client.stop();
}
// check the situation with the channels and fade out if the timer has exhausted
boolean active_channels = false;
for (int i=0; i<MAX_CHANNELS; i++) {
if (channels[i] != 0 && channel_timers[i]> 0) {
DEBUG_PRINT("Timer CD");
DEBUG_PRINT(i);
DEBUG_PRINT(": ");
DEBUG_PRINTLN(channel_timers[i]);
channel_timers[i] -= (millis() - last_time);
last_time = millis();
if (channel_timers[i]<= 0) {
channel_timers[i] = 0;
fadeout(i);
} else {
active_channels = true;
}
}
}
if (!active_channels) last_time = 0; // reset the master timer to zero if nothing's using it.
delay(5);
}
void pulser(int channel, double time) {
// Set up the pulser as needed given the channel
// note that adding a pulse to an existing pulse will increment the pulse by the new time
// ie: if remaining pulse < 100s then remaining pulse += new pulse amount
DEBUG_PRINT("time: ");
DEBUG_PRINTLN(time);
if (channel_timers[channel] > 0) {
// basically we already have a pulse in motion so just add to it
DEBUG_PRINTLN("Already pulsing");
if (channel_timers[channel] < 100000) channel_timers[channel] += time; // put upper limit of 100 seconds on additions
} else {
fadein(channel);
channel_timers[channel] = time;
if (last_time == 0) last_time = millis(); // just a catch all
}
}
void fadein(int channel) {
// this method fades up the channel
DEBUG_PRINTLN("Fade in");
for (int i = 255; i> 0; i--) {
analogWrite(channels[channel], i);
delay(FADE_TIME);
}
}
void fadeout(int channel) {
// this method fades out the channel
DEBUG_PRINTLN("Fade out");
for (int i = 0; i<255; i++) {
analogWrite(channels[channel], i);
delay(FADE_TIME);
}
digitalWrite(channels[channel], HIGH);
}
Owner

ajfisher commented Oct 16, 2011

This will pulse a PWM pin after you hit a url. Loosely inspired by restduino and driven by a need I had to have an arduino displaying a light in one location but the processing happen somewhere else.

Once up and running (look for the fill in IP and channel info at the top) to use it you simply go:

http://[ARDUINO IP]/[CHANNEL NUMBER]/[DURATION in msec]

Eg http://192.168.0.2/0/10000 would pulse channel 0 (with whatever PWM pin this has been mapped to) for 10 seconds.

Program will return a 200 OK header if all is fine, will return 404 if the URL doesn't map correctly and a generic 500 error for any other problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment