Project Description

The thing system – Clients

by The Thing System



Overview

More complicated clients, one which monitor updates from the steward looking for changing conditions, and reacting to them on behalf of the user by asking the steward to perform actions, are entirely possible. Technically speaking these are intelligent agents working in a multi-agent system with shared goals of making life more convenient for the user. We like to think that we’re implementing magic.

Example code

  1. example clients, saying “Hello Lightbulbs”


var http = require("http");
var util = require("util");
var url = require("url");
var WebSocket = require('ws');

function onRequest(request, response) {
var ws;

};

ws.onmessage = function(event) {
console.log("Socket message: " + util.inspect(event.data));
response.write( "<h2>Turning lightbulb '" + pathname +"'</h2>" + util.inspect(event.data, {depth: null}));
ws.close();
response.end();

};

ws.onclose = function(event) {
console.log("Socket closed: " + util.inspect(event.wasClean));
};

ws.onerror = function(event) {
console.log("Socket error: " + util.inspect(event, {depth: null}));
try {
ws.close();
console.log("Closed websocket.");
} catch (ex) {}
};

}

var server = http.createServer(onRequest).listen(9999);
console.log("Server started on port 9999.");

  1. Example using Arduino


#include <Dhcp.h;
#include Dns.h;
#include Ethernet.h>
#include EthernetClient.h>
#include util.h>
#include SPI.h;

const char *server = "192.168.1.91";

const int buttonPin = 7;
const int ledPin = 6;

int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
int sentPacket = 0;

long lastDebounceTime = 0;
long debounceDelay = 50;

char *jsonOff = "{\"path\":\"/api/v1/actor/perform/device/lighting\",\"requestID\":\"2\",\"perform\":\"off\",\"parameter\":\"\"}";
char *jsonOn = "{\"path\":\"/api/v1/actor/perform/device/lighting\",\"requestID\":\"1\",\"perform\":\"on\",\"parameter\":\"{\\"brightness\\":100}\"}";
byte mac[] = { 0x0, 0xA2, 0xDA, 0x0D, 0x90, 0xE2 };

EthernetClient client;
WebSocketClient webSocketClient;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);

Serial.begin(9600);
while(!Serial) { }

if (Ethernet.begin(mac) == 0) {
Serial.println("Error: Failed to configure Ethernet using DHCP");
while(1) { }
}
}

void loop() {
String data;
int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = reading;
if ( buttonState && !sentPacket ) {
if ( ledState ) {
Serial.println( "Turning lights off.");
Serial.println("Connecting to steward...");
if( client.connect(server,8887) ) {
Serial.println("Connected");
webSocketClient.path = "/manage";
webSocketClient.host = "dastardly.local";
if (webSocketClient.handshake(client)) {
Serial.println("Handshake successful");
} else {
Serial.println("Handshake failed.");
while(1) {
// Hang on failure
}
}
webSocketClient.sendData(jsonOff);
}

}

if (client.connected()) {
data = webSocketClient.getData();
if (data.length() > 0) {
Serial.print("Received data: ");
Serial.println(data);
client.stop();
if ( ledState ) {
digitalWrite(ledPin, LOW);
ledState = LOW;
} else {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
}
}
}

if ( lastButtonState != reading ) {
sentPacket = 0;
}
lastButtonState = reading;
}

Learn More

Goto Original


TAG : 201509 W5100, Ethernet, W5100, Ardunio, Things, Client