I am looking for the best ways to create a dependable network between two Teensy 3.2's, both using WIZ820io's, and a PC. I have to connect both Teensy's to the PC, one is close to the PC and one is a long distance away. I've been able to do this without many problems, but I am looking to expand on the current setup.
I would like the Teensy's to have their own connection, this can be a one way connection, or bi-directional. I am also interested in Auto-assigning IP addresses, a world I haven't entered but would like to understand more of.
The program written is a stripped down example of my network settings, this includes a counter and a triangle wave generator that are sent over a string once ever ~100 microseconds.
I would like the Teensy's to have their own connection, this can be a one way connection, or bi-directional. I am also interested in Auto-assigning IP addresses, a world I haven't entered but would like to understand more of.
The program written is a stripped down example of my network settings, this includes a counter and a triangle wave generator that are sent over a string once ever ~100 microseconds.
Code:
//#include <FileIO.h>
#include <SPI.h>
#include <Ethernet.h>
////////SERIAL SETTINGS/////////
unsigned long inByte = 0; // incoming serial byte
////////ETHERNET SETTINGS/////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address of the shield
byte ip[] = {192, 168, 1, 177}; //IP Address of the shield
//byte gateway[] = {192, 168, 1, 1};
byte subnet[] = {255, 255, 255, 0}; //specific subnet that all units are on
EthernetServer server(6340); //port 6340
EthernetClient client;
/////////TIMER SETTINGS////////////
unsigned long previousMicros = 0; // will store last time LED was updated
unsigned long currentMicros;
int count = 0;
int dir = 0;
int sine = 1;
int sindir = 1;
// init settings
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
delay(200);
digitalWrite(9, HIGH); //This^ is needed to RESET the WIZ820io, will not work without this
Serial.begin(9600); // Begin Serial comms.
delay(200); // Delay
// Initialize the ethernet device
Ethernet.begin(mac, ip, subnet); // begin ethernet....working without subnet?
// // Start listening for clients
server.begin(); // open as server
}
// Main loop
void loop()
{
client = server.available(); // broadcast to client
client.setTimeout(10); // max waiting time is 10ms
currentMicros = micros(); // get the time at the start of this loop()
tcpread(); // if data comes in TCP do...
sensorstring(); //Send all sensor data
}
///////////////////SENSOR STRING SECTION/////////////////
/*
This section collects data from all desired pins and sends it up the pipe over TCP. server print/write function actually uses SPI to go to wiz820io
but it is seamless.
*/
void sensorstring()
{
if (currentMicros - previousMicros >= 100) // check that current time - last updated time = x ms
{
// analogReadResolution(16); // set resolution to full 16 bits
String dataString; // data string **********Should this be outside the main loop??*************
dataString += String(count); //some value here??();
dataString += ",";
if (dir == 0)
{
if (count < 5000)
{
count++;
}
else
{
count = 0;
}
}
if (dir == 1)
{
if (count <= 5000)
{
count--;
}
if (count == 0)
{
count = 5000;
}
}
if (sine >= 500)
{
sindir = 0;
}
if (sine <= -500)
{
sindir = 1;
}
if (sindir == 1)
{
sine += 20;
}
if (sindir == 0)
{
sine -= 20;
}
dataString += String(sine); //some value here??();
server.print(dataString); // send data string
server.write(0x0D); // carriage return
server.write(0x0A); // end terminal ***These two hex commands are needed for labview in order for TCP read to not over/under read
previousMicros = currentMicros; //stamp time
}
}
////////TCP READ SECTION/////////
/*
This section reads the incoming serial data and assigns it where appropriate.
*/
void tcpread()
{
if (client.available() > 0) // if there's any TCP data available, read it:
{
inByte = client.parseInt(); // convert incoming data to int, waits for end of transmission and makes one coherent number
if (inByte == 111) // set conditions here for parsed Int
{
dir = 0;
}
if (inByte == 222)
{
dir = 1;
}
}
}