Programas de software libre escritos específicamente para Water 4 bits
// David Pello
// Proyecto Water4Bits para Hackitectura.net, Sevilla, Octubre 2008
#include "etherShield.h"
// datos de ethernet
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {192,168,1,200};
static char baseurl[]="http://93.156.25.243:8080";
static uint16_t mywwwport = 80; // puerto tcp (1-254)
char datos_string[12];
int valor = 0;
int cont = 0;
#define BUFFER_SIZE 500
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];
EtherShield es=EtherShield();
// preparamos la página web escribiendo los datos en el buffer tcp
uint16_t print_webpage(uint8_t *buf);
int8_t analyse_cmd(char *str);
void setup(){
Serial.begin(19200);
/*inicializar el enc28j60*/
es.ES_enc28j60Init(mymac);
es.ES_enc28j60clkout(2); // cambiar clkout de 6.25MHz a 12.5MHz
delay(10);
/* Configuración de los LEDs del Magjack, enc28j60 datasheet, página 11 */
// LEDA=verde LEDB=amarillo
//
// 0x880 -> PHLCON LEDB=on, LEDA=on
// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
es.ES_enc28j60PhyWrite(PHLCON,0x880);
delay(500);
//
// 0x990 -> PHLCON LEDB=off, LEDA=off
// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
es.ES_enc28j60PhyWrite(PHLCON,0x990);
delay(500);
//
// 0x880 -> PHLCON LEDB=on, LEDA=on
// enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);
es.ES_enc28j60PhyWrite(PHLCON,0x880);
delay(500);
//
// 0x990 -> PHLCON LEDB=off, LEDA=off
// enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);
es.ES_enc28j60PhyWrite(PHLCON,0x990);
delay(500);
//
// 0x476 -> PHLCON LEDA=links status, LEDB=recepción/transmisión
// enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
es.ES_enc28j60PhyWrite(PHLCON,0x476);
delay(100);
//inicializar la capa ethernet/ip :
es.ES_init_ip_arp_udp_tcp(mymac,myip,80);
pinMode(9, OUTPUT);
}
void loop(){
uint16_t plen, dat_p;
int8_t cmd;
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
/*plen será distinta de cero si hay un paquete válido (sin error crc) */
if(plen!=0){
// arp es broadcast si es desconocida, pero un host podría también verificar la dirección mac enviándola a una dirección unicast.
if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){
es.ES_make_arp_answer_from_request(buf);
return;
}
// mira si los paquetes son para nosotros:
if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
return;
}
if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
es.ES_make_echo_reply_from_request(buf,plen);
return;
}
// puerto tcp de www, compara sólo el byte bajo
if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){
if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn ya manda el syn,ack
return;
}
if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
es.ES_init_len_info(buf); // inicializa algunas estructuras de datos
dat_p=es.ES_get_tcp_data_pointer();
if (dat_p==0){ // quizá no tenemos datos, sólo ack:
if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
es.ES_make_tcp_ack_from_any(buf);
}
return;
}
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
// head, post y otros métodos para códigos de estátus:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n200 OK"));
goto SENDTCP;
}
if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
plen=print_webpage(buf);
goto SENDTCP;
}
cmd=analyse_cmd((char *)&(buf[dat_p+5]));
if (cmd==1){
plen=print_webpage(buf);
}
SENDTCP: es.ES_make_tcp_ack_from_any(buf); // manda ack para el http get
es.ES_make_tcp_ack_with_data(buf,plen); // envia los datos
}
}
}
}
// El valor recibido se guarda en la variable global strbuf
uint8_t find_key_val(char *str,char *key)
{
uint8_t found=0;
uint8_t i=0;
char *kp;
kp=key;
while(*str && *str!=' ' && found==0){
if (*str == *kp){
kp++;
if (*kp == '\0'){
str++;
kp=key;
if (*str == '='){
found=1;
}
}
}else{
kp=key;
}
str++;
}
if (found==1){
// copiar el valor en un buffer y terminarlo con '\0'
while(*str && *str!=' ' && *str!='&' && i 0x2f){
// si es un número ASCII, lo devuelve
r=(*strbuf-0x30);
}
}
return r;
}
uint16_t print_webpage(uint8_t *buf)
{
if (Serial.available() > 5) { // si tenemos datos en el puerto serie (xbee)
int contad = Serial.available(); // cuantos ?
if (contad > 12) contad = 12; // para que no se nos salga del rango
int i;
for (i=0; i
Programa para arduino (squidbee) que lee los datos del sensor desde el puerto serie
/*
Programa para SquidBee
*/
// variables declaration
int sens0 = 0; //sensors
int sens1 = 1;
int sens2 = 2;
int val0 = 0; //aux var
int val1 = 0;
int val2 = 0;
int count = 0;
void setup(){
Serial.begin(19200); //inicializa el puerto serie
}
// function to send data
void sendData(int id,int num, int luminosidad,int humedad,int temperatura){
float l, h, t;
Serial.print("@");
Serial.print(id);
Serial.print("|");
Serial.print(num);
Serial.print("|luminosidad-");
l = (100.0 / 950.0) * luminosidad;
Serial.print((int)l);
Serial.print("|humedad-");
h = (humedad * 5.0 / 1024.0) * 32.25 - 25.81;
Serial.print((int)h);
Serial.print("|temperatura-");
t = (temperatura * 5.0 / 1024.0) / 0.01;
Serial.print((int)t);
Serial.println("#\r"); // end of message
}SQUIDBEE
void loop(){
while (count <= 10000){
val0 = analogRead(sens0);
val1 = analogRead(sens1);
val2 = analogRead(sens2);
sendData(1,count, val0,val1,val2);
delay(5000);
count++;
}
count = 0;
}
Programa que envía los datos del puerto serie (sensor) a un servidor ftp
#!/usr/bin/ruby
require 'net/ftp'
require "serialport"
def leerarduino
puerto = "/dev/ttyUSB0" #may be different for you
baudios = 19200
data_bits = 8
stop_bits = 1
paridad = SerialPort::NONE
sp = SerialPort.new(puerto, baudios, data_bits, stop_bits, paridad)
sleep 1
cadena = sp.gets
sp.close
return cadena
end
def subearchivo
print "> Conectando FTP...\n"
ftp = Net::FTP.new('hackitectura.net', 'xxxxx', 'xxxxxx')
print "> Accediendo...\n"
files = ftp.chdir('hackitectura.net')
print "> Subiendo archivo...\n"
files = ftp.puttextfile("datos.txt", "datos.txt")
ftp.close
print "> OK.\n"
end
############################ Principal
while true
datos = nil
while datos == nil
datos = leerarduino
end
print datos
print "> Escribiendo archivo...\n"
archivo = File.new("datos.txt", "w")
archivo.puts(datos)
archivo.close
print "> OK.\n"
subearchivo
sleep 10
end
Programa que lee el servidor ftp y visualiza los datos del sensor de temperatura y luminosidad en OpenSim (color y canal alpha), LSL (Linden Scripting Languague).
key http_id;
// convertir la temperatura en un vector de colores SL.
vector colorTemp(integer temp)
{
float intermedio;
if(temp < 15) return <0.0,0.0,0.0>;
if(temp > 30) return <1.0,0.0,0.0>;
intermedio = (temp-15.0)/15.0;
return ;
}
default
{
state_entry()
{
llSetTimerEvent(10.0); // ponemos el timer a 10 segundos
}
//funcion llamada por el timer
timer()
{
//hacemos la peticion http;
http_id = llHTTPRequest("http://w4b.hackitectura.net/datos.txt", HTTP_METHOD, "GET","");
}
//funcion que se llama automaticamente cuando recibe la respuesta del HTTP
http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == http_id)
{
string temperatura, luminosidad, humedad;
list listavalores = llParseString2List(body," ",".");
luminosidad = llList2String(listavalores, 0);
humedad = llList2String(listavalores, 1);
temperatura = llList2String(listavalores, 2);
llSay(0, "Luminosidad: " + luminosidad);
llSay(0, "Humedad: " + humedad);
llSay(0, "Temperatura: " + temperatura);
vector color = colorTemp((int)temperatura); //convertimos la temperatura en vector de color
llSetColor(color, ALL_SIDES); // ponemos el color del objeto
integer lum = (int) luminosidad;
if (lum == 0) lum = 1;
float lumf = lum / 100.0;
llSetAlpha(lumf, ALL_SIDES);
}
}
}