114 lines
2.8 KiB
C++
114 lines
2.8 KiB
C++
#include "WiFlyHQ_.h"
|
|
#include <EEPROM.h>
|
|
|
|
uint16_t remote_Port = 8000;
|
|
uint16_t local_Port = 9000;
|
|
char ip_host[16] = "";
|
|
#define DEFAULT_ip_host "192.168.0.1"
|
|
#define ssid "VDOSSIER"
|
|
#define pass "VDOSSIER"
|
|
boolean incoming_ok = false;
|
|
|
|
byte VER = 0x01;
|
|
|
|
char test_message[] = { // Message template commands
|
|
'/', 'L', 'u', 'Z', // ANALOG OUT TEMPLATE
|
|
B0 , B0 , B0, B0,
|
|
',', 'i', B0, B0,
|
|
B0 , B0 , B0, B0 // 4 analog outs = 4 integers
|
|
};
|
|
|
|
char incomming_message[] = { // Message template commands
|
|
'/', 'L', 'u', 'Z', // ANALOG OUT TEMPLATE
|
|
B0 , B0 , B0, B0,
|
|
',', 'i', B0, B0,
|
|
B0 , B0 , B0, B0 // 4 analog outs = 4 integers
|
|
};
|
|
|
|
byte incomming_count = 0;
|
|
|
|
WiFly wifly;
|
|
|
|
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
|
|
{
|
|
asm volatile (" jmp 0");
|
|
}
|
|
|
|
void ini()
|
|
{
|
|
Serial.begin(115200);
|
|
if (EEPROM.read(0x30) == VER)
|
|
{
|
|
for(int i=0; i<16; i++) ip_host[i]='\x00'; //Limpia memoria
|
|
for(int i=0; i<16; i++) ip_host[i] = EEPROM.read(i);
|
|
}
|
|
else
|
|
{
|
|
for(int i=0; i<16; i++) ip_host[i]='\x00'; //Limpia memoria
|
|
for(int i=0; i<16; i++) EEPROM.write(i,'\x00'); //Limpia memoria
|
|
for(int i=0; i<strlen(DEFAULT_ip_host); i++) ip_host[i] = DEFAULT_ip_host[i];
|
|
for(int i=0; i<strlen(ip_host); i++) EEPROM.write(i, ip_host[i]);
|
|
EEPROM.write(0x30,VER);
|
|
}
|
|
wifly.begin(&Serial);
|
|
// wifly.setBaud(115200);
|
|
// wifly.save();
|
|
if (!wifly.isAssociated()) {
|
|
/* Setup for UDP packets, sent automatically */
|
|
wifly.setIpProtocol(WIFLY_PROTOCOL_UDP);
|
|
wifly.setHost(ip_host, remote_Port); // Send UDP packet to this server and port
|
|
wifly.setPort(local_Port); // listen in this local port
|
|
/* Setup the WiFly to connect to a wifi network */
|
|
wifly.setSSID(ssid);
|
|
wifly.setPassphrase(pass);
|
|
wifly.enableDHCP();
|
|
wifly.join();
|
|
}
|
|
char buf[32];
|
|
wifly.print("MAC:");
|
|
wifly.println(wifly.getMAC(buf, sizeof(buf)));
|
|
wifly.print("IP: ");
|
|
wifly.println(wifly.getIP(buf, sizeof(buf)));
|
|
wifly.print("Netmask: ");
|
|
wifly.println(wifly.getNetmask(buf, sizeof(buf)));
|
|
wifly.print("Gateway: ");
|
|
wifly.println(wifly.getGateway(buf, sizeof(buf)));
|
|
wifly.print("LocalPort: ");
|
|
wifly.println(wifly.getPort());
|
|
wifly.print("HostPort: ");
|
|
wifly.println(wifly.getHostPort());
|
|
wifly.setDeviceID("Lampara1");
|
|
wifly.print("DeviceID: ");
|
|
wifly.println(wifly.getDeviceID(buf, sizeof(buf)));
|
|
wifly.println("Lampara ready");
|
|
}
|
|
|
|
void setup() {
|
|
pinMode(9, OUTPUT);
|
|
pinMode(10, OUTPUT);
|
|
pinMode(11, OUTPUT);
|
|
analogWrite(9, 0);
|
|
analogWrite(10, 0);
|
|
analogWrite(11, 0);
|
|
ini();
|
|
delay(2000);
|
|
}
|
|
|
|
void loop() {
|
|
if(wifly.available()>0)
|
|
{
|
|
char inByte;
|
|
inByte = wifly.read();
|
|
test_ip(inByte);
|
|
test_incoming(inByte);
|
|
}
|
|
if (incoming_ok)
|
|
{
|
|
analogWrite(11, incomming_message[15]);
|
|
incoming_ok=false;
|
|
}
|
|
}
|
|
|
|
|
|
|