Show Posts
Pages: [1] 2 3
1  Topics / Home Automation and Networked Objects / Re: Arduino Jquery retain sliders last value on: August 11, 2014, 04:30:15 pm
I’m currently working on a Arduino based pre amp that hosts a rest api for remote controlling:



http://tinypic.com/r/15yvj2g/8
http://tinypic.com/r/11vmhld/8
http://tinypic.com/r/jrzfk5/8

I'm using a modified version of the Webduino library that can be found from here https://github.com/lasselukkari/aWOT , but you should be able to use the same idea with standard webduino as well.

Here is the the full scetch. The parts you are interested are on the top. The end result looks like this:



Code:
#include <SPI.h>
#include <Ethernet.h>
#include <HTTPServer.h>
#include <ServerInterface.h>
#include <StreamServerAdapter.h>
#include <EthernetServerAdapter.h>
#include <JsonParser.h>
#include <pga23XX.h>
#include <Encoder.h>

#define encoderPin1 2
#define encoderPin2 3
#define channelOnePin 5
#define channelOnePin 5
#define channelTwoPin 6
#define channelThreePin 7
#define encoderSwitchPin 9
#define pgaMutePin 14
#define  pgaSckPin 15
#define  pgaCsPin 16
#define  pgaSdataPin 17

PGA23XX pga2311;

byte volume = 0;
byte input = 0;
byte minVolume  = 0;
byte maxVolume  = 192;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);
EthernetServerAdapter ethernetServerAdapter(&server);
HTTPServer httpServer("");
Encoder volumeKnob(encoderPin1, encoderPin2);

bool previousButtonState;
bool buttonPressed = false;
long lastBounceTime = 0;         // the last time the output pin was toggled
byte debounceHoldTime = 50;   // the debounce time, increase if the output flickers

void indexCmd(HTTPServer &server, HTTPServer::MethodType type, char *, bool) {
  server.httpSuccess();
  P(helloMsg) =
    "<!DOCTYPE html>\n"
    "<html lang=\"en\">\n"
    "\n"
    "<head>\n"
    "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
    "<title>aPreAmp</title>\n"
    "<link href=\"http://www.cs.helsinki.fi/u/ljlukkar/apreamp/css/main.css\" rel=\"stylesheet\">\n"
    "</head>\n"
    "\n"
    "<body>\n"
    "\n"
    "<div class=\"buttons\">\n"
    "<input type=\"radio\" id=\"input0\" name=\"inputSelect\" value=\"0\">\n"
    "<label for=\"input0\" class=\"button\">input 1</label>\n"
    "<input type=\"radio\" id=\"input1\" name=\"inputSelect\" value=\"1\">\n"
    "<label for=\"input1\" class=\"button\">input 2</label>\n"
    "<input type=\"radio\" id=\"input2\" name=\"inputSelect\" value=\"2\">\n"
    "<label for=\"input2\" class=\"button\">input 3</label>\n"
    "</div>\n"
    "\n"
    "<div id=\"volumeSlider\" class=\"center-block\"></div>\n"
    "\n"
    "<script src=\"http://code.jquery.com/jquery-1.10.2.js\"></script>\n"
    "<script src=\"http://cdnjs.cloudflare.com/ajax/libs/noUiSlider/6.2.0/jquery.nouislider.min.js\"></script>\n"
    "\n"
    "<script>\n"
    "\n"
    "var state = {\n"
    "v: 0,\n"
    "i: 0\n"
    "};\n"
    "\n"
    "var busy = false;\n"
    "\n"
    "var inputSelect = $(\'input[name=\"inputSelect\"]:radio\');\n"
    "var volumeSlider = $(\"#volumeSlider\");\n"
    "\n"
    "volumeSlider.noUiSlider({\n"
    "start: 0,\n"
    "orientation: \'vertical\',\n"
    "direction: \"rtl\",\n"
    "range: {\n"
    "\'min\': 0,\n"
    "\'max\': 192\n"
    "}\n"
    "});\n"
    "\n"
    "volumeSlider.on({\n"
    "slide: function () {\n"
    "busy = true;\n"
    "},\n"
    "set: function (event, value) {\n"
    "state.v = parseInt(value);\n"
    "busy = false;\n"
    "saveState(state);\n"
    "}\n"
    "});\n"
    "\n"
    "\n"
    "inputSelect.change(function () {\n"
    "state.i = Number(this.value);\n"
    "saveState(state);\n"
    "});\n"
    "\n"
    "\n"
    "function fetchState() {\n"
    "\n"
    "if (busy) return;\n"
    "\n"
    "busy = true;\n"
    "\n"
    "$.getJSON(\"/state\", function (serverState) {\n"
    "state.i = serverState.i;\n"
    "state.v = serverState.v;\n"
    "volumeSlider.val(state.v);\n"
    "$(\"input[name=inputSelect][value=\" + state.i + \"]\").prop(\'checked\', true);\n"
    "busy = false;\n"
    "});\n"
    "}\n"
    "\n"
    "function saveState(newState) {\n"
    "$.ajax({\n"
    "type: \"PUT\",\n"
    "url: \'/state\',\n"
    "contentType: \'application/json\',\n"
    "data: JSON.stringify(newState)\n"
    "});\n"
    "}\n"
    "\n"
    "function resizeControls() {\n"
    "\n"
    "var height = $(window).height();\n"
    "var width = $(window).width();\n"
    "\n"
    "var reference = (height / width < 1.30) ? (height * 0.45) : (width * 0.60);\n"
    "\n"
    "$(\"#volumeSlider\").width(reference).height(1.49 * reference);\n"
    "$(\".noUi-handle\").width(0.41 * reference).height(0.41 * reference);\n"
    "$(\".buttons\").width(1.56 * reference);\n"
    "$(\".button\").width(0.51 * reference).height(0.23 * reference);\n"
    "\n"
    "}\n"
    "\n"
    "$(window).resize(function () {\n"
    "\n"
    "resizeControls();\n"
    "\n"
    "});\n"
    "\n"
    "$(function () {\n"
    "\n"
    "resizeControls();\n"
    "setInterval(fetchState, 500);\n"
    "\n"
    "});\n"
    "\n"
    "</script>\n"
    "\n"
    "</body>\n"
    "</html>\n"
    ;
  server.printP(helloMsg);
}

void sendJSONresponse(HTTPServer &server){

  char buffer [4];

  server.httpSuccess("application/json");

  server.write("{\"v\":");
  itoa (volume,buffer,10);
  server.write(buffer);
  server.write(",\"i\":");
  itoa (input,buffer,10);
  server.write(buffer);
  server.write("}");

}

void getState(HTTPServer &server, HTTPServer::MethodType type, char ** routeParams, char *, bool) {

  sendJSONresponse(server);

}

void updateState(HTTPServer &server, HTTPServer::MethodType type, char ** routeParams, char *, bool) {

  char buffer[20];
  byte i = 0;

  while (server.available() && i < 19){
    buffer[i] = (char)server.read();
    i++;
  }

  buffer[i] = '\0';

  JsonParser<5> parser;

  JsonHashTable hashTable = parser.parseHashTable(buffer);

  if (!hashTable.success()){

    server.httpServerError();

  }
  else {

    volume = hashTable.getLong("v");
    input = hashTable.getLong("i");

    sendJSONresponse(server);
    server.close();

    volumeKnob.write(volume);
    selectInput(input);
    pga2311.setVolume(volume, volume);

  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("starting");
  pga2311.begin(pgaCsPin, pgaSdataPin, pgaSckPin, pgaMutePin);

  pinMode(channelOnePin, OUTPUT);
  pinMode(channelTwoPin, OUTPUT);
  pinMode(channelThreePin, OUTPUT);
  pinMode(encoderSwitchPin, INPUT);
  digitalWrite(encoderSwitchPin, HIGH);

  selectInput(input);

  if (Ethernet.begin(mac)) {
    Serial.print("My IP address: ");
    for (byte thisByte = 0; thisByte < 4; thisByte++) {

      Serial.print(Ethernet.localIP()[thisByte], DEC);
      Serial.print(".");
    }
    Serial.println();
  }

  httpServer.setDefaultCommand(&indexCmd);
  httpServer.onGET("state", &getState);
  httpServer.onPUT("state", &updateState);

}

void selectInput(byte input) {

  if (input >= 0 && input < 3) {

    digitalWrite(channelOnePin, LOW);
    digitalWrite(channelTwoPin, LOW);
    digitalWrite(channelThreePin, LOW);
    digitalWrite(input + 5, HIGH);

  }

}

void processVolumeKnob() {

  byte reading = volumeKnob.read();

  if (reading != volume && reading >= minVolume && reading <= maxVolume) {

    volume = reading;
    pga2311.setVolume(volume, volume);

  }

  volumeKnob.write(volume);

}

void processInputButton() {

  byte reading = volumeKnob.read();

  reading = digitalRead(encoderSwitchPin);

  if (reading != previousButtonState) {
    lastBounceTime = millis();
  }

  if (millis() - lastBounceTime > debounceHoldTime) {

    if (reading == LOW && !buttonPressed){

      buttonPressed = true;

      if (input<2) {

        input++;

      }  
      else {

        input = 0;

      }

      selectInput(input);

    }
    else if (reading == HIGH){

      buttonPressed = false;

    }

  }

  previousButtonState = reading;

}


void loop() {

  httpServer.processConnection(&ethernetServerAdapter);

  pga2311.run(50);

  processInputButton();

  processVolumeKnob();

}


The HTML is kinda annoying to read because of the escaping. Here is the HTML content alone:
http://pastebin.com/w0Yk5sRH
2  Topics / Home Automation and Networked Objects / Re: HTML/Arduino coding for single ON/OFF button on: July 10, 2014, 09:56:17 am
Take a look at this page:
https://www.cs.helsinki.fi/u/ljlukkar/wot/

You could combine the code from the two examples and get the functionality you are after.

The code listings on that page are outdated but updated versions can be found from github and are also available in the examples if you add the aWOT library.
https://github.com/lasselukkari/aWOT
3  Topics / Home Automation and Networked Objects / Re: looking for Arduino MQTT library w/ encryption (SSL or TLS?) on: June 02, 2014, 02:48:16 am
Atleast the 8-bit Arduinos don't have enough resources for SSL or TLS implementation.
4  Topics / Home Automation and Networked Objects / Re: HELP! Greenhouse contol concept problems PLEASE READ! on: April 18, 2014, 01:44:13 am
Ask me anything but not the source code as I don't have it anymore:
http://www.cs.helsinki.fi/u/ljlukkar/hydrobot/
5  Topics / Home Automation and Networked Objects / Re: Web based Temperature Logging on: April 15, 2014, 08:22:42 am
Here is how I did it:
https://github.com/lasselukkari/ArduinoWoT/blob/master/examples/WeatherStation/WeatherStation.ino

The result looks like this:
https://www.cs.helsinki.fi/u/ljlukkar/weatherstation/
6  Topics / Home Automation and Networked Objects / Apple TV web interface on: April 14, 2014, 02:30:38 am
Here is something I built during the weekend. I saw this post last Friday on Hackaday and remembered a project idea that I had been thinking about previously. Apple Remote Arduino shield has been featured in Hackaday also in 2009.

Searching anything with the original Apple TV IR-remote is a pain in the ass. You are forced to navigate your way in a grid of letters where the letters are organized alphabetically instead of forming a qwerty keyboard. I barely remember the correct order of the alphabets. Also many of the menu views would work better with swipe gestures instead of the four navigation buttons.




More details can be found from here:
https://www.cs.helsinki.fi/u/ljlukkar/appletv/

Source code on GitHub
7  Topics / Home Automation and Networked Objects / Re: arduino - android communication on: April 04, 2014, 06:18:47 am
I do it like this:
https://www.cs.helsinki.fi/u/ljlukkar/wot/

It's a webserver running on arduino but you get to choose the connection method from serial (bluetooth), ethernet and wifi and they all can be used with an Android phone.
8  Topics / Home Automation and Networked Objects / Re: Arduino HTTP REST API over BlueTooth with Android on: March 31, 2014, 10:06:00 am
Due to popular demand (none really) I made another example. A remote controller for the Blink server demonstrated in the first example.

Together with the first example they can be used for example like this

The source code for the Blink remote client is even simpler than the Blink server
Code:
#include <aJSON.h>
#include <ClientInterface.h>
#include <SerialClientAdapter.h>
#include <aJsonStore.h>
#include <HTTPStore.h>

// EthernetClient client;
// EthernetClientAdapter ethernetClientAdapter(client);
// byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// byte blinkServer[] = { 192, 168, 0, 11 };
// HTTPStore httpStorage(&ethernetClientAdapter);

SerialClientAdapter serialClientAdapter(&Serial);
HTTPStore httpStorage(&serialClientAdapter);
aJsonObject* ledModel  = aJson.parse("{\"id\":\"led\", \"on\":true}");

int buttonPin = 2;
bool lastState = 0;

void setup() {
  //Ethernet.begin(mac);
  //httpStorage.setServer(server, 80);
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop(){
  bool buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH && buttonState != lastState) {
    bool oldState = aJson.getObjectItem(ledModel, "on")->type;
    aJson.getObjectItem(ledModel, "on")->type = !oldState;
    httpStorage.saveModel("led", ledModel);
  }
  lastState = buttonState;
}

Everything needed for the setup including the HTML user interface is done with around 100 lines of C++ and JavaScript in total.
9  Topics / Home Automation and Networked Objects / Arduino HTTP REST API over BlueTooth with Android on: March 28, 2014, 02:40:03 am
Hello everyone!

I have been playing around with Arduino to build REST backends to support HTML user interfaces.

For the web server part I have been using the Webduino library. Webduino only works with Wiznet ethernet cards so I decided to abstract the transport stack behind an interface. This way it's possible to use for example serial connections and the CC3000 wifi chip with the library. The ability to use serial has many use cases. For example a serial to bluetooth adapter makes it possible to use the webserver over bluetooth with your Android mobile device and you can use XBees to make networks of servers and clients that communicate wiht each other wirelessly. The devices can also use multiple transport stacks simultaneously all with the same webserver code.

I have written some documentation that can be found from here:
https://www.cs.helsinki.fi/u/ljlukkar/wot/

Source code and some examples are here:
https://github.com/lasselukkari/ArduinoWoT

I currently don't have much time for this project but I'm happy to answer any questions.
10  Topics / Home Automation and Networked Objects / Re: Arduino control via Bluetooth on: October 19, 2013, 10:05:17 am
In the slave code you don't declare the pin mode.
11  Topics / Home Automation and Networked Objects / Re: Arduino Uno Record and Playback 433.92mhz signals via web interface on: June 17, 2013, 07:59:20 am
I did something similar:
https://github.com/lasselukkari/Arduino-RC-Timer-Switch

It's just a POC and I'm not planning to develop it any further.
12  Topics / Home Automation and Networked Objects / Re: Control Ethernet Shield with PHP? on: May 23, 2013, 01:20:06 am
I don't want to go to that page I just want to send that command.  How are people doing this?
http://api.jquery.com/jQuery.get/
13  Topics / Home Automation and Networked Objects / Re: Arduino Mega JSON REST interfaces (for HTML GUIs) on: January 26, 2013, 05:45:26 am
Hi llukkari,

Have you tried to do this project with the css and javascript files embedded on the arduino somehow?  Do you have any suggestions on how that might be done?

Thanks,
Tom


You could probably use the sd card. In my hydrobot project, http://www.cs.helsinki.fi/u/ljlukkar/hydrobot/,  I was able to use quite large csv files. I can't see any reason why it wouldn't work for other files as well.

You should try to set the http Cache-Control header so that the browser caches the responce and doesn't request it again.

Backbone.js, the MV* framework I have used in all the examples, is one of the most light weight out of them all.
14  Topics / Home Automation and Networked Objects / Re: Relay RF Control on: January 13, 2013, 04:25:30 am
thanks for the response.
I take from your reply that it would be possible for me to control the relay using the RF linkkit.

So i can control my relay without wiring directly to the UNO using this method?

The radio controllable switches I used are so cheap that I wouldn't bother to build the receiving end by myself. And atleast here Finland the legistlation is so that this has been the only legal way I know to control the mains AC with out being a qualified electrician.

If you want to use your own relays you could probably use the VirtualWire library instead.
 
15  Topics / Home Automation and Networked Objects / Re: Relay RF Control on: January 13, 2013, 03:49:46 am
Have you seen this library: http://code.google.com/p/rc-switch/ ?

I was playing around with it during the holidays and wrote this example:
https://github.com/lasselukkari/Arduino-RC-Timer-Switch (mega only)
Pages: [1] 2 3