void loop()
{
	static double v = 0.0;
	static int count = 0;

	EthernetClient client = server.available();
	if (client)
	{
		while(true){

			Serial.print("Printing data... count=");
			Serial.println(count);

			client.print(count++); client.print("; ");
			client.print(0.00 + v, 2); client.print("; ");
			client.print(1.23 + v, 2); client.print("; ");
			client.print(2.098 + v, 3); client.print("; ");
			client.print(3.83974 + v, 5); client.print("; ");
			client.print(1.23 + v, 10); client.print("; ");
			client.println(6.1276512765 + v, 10);
			client.println("\r\n");

			Serial.println("Done!");
			delay(1000);

			v += 0.2;
		}
	}else{
		Serial.print("NO client!!! count=");
		Serial.println(count++);
	}

	delay(1000);

}
Exemplo n.º 2
0
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();

  // we get a request
  if (client) {
    Serial.println(F("Client connected"));
    // an http request ends with a blank line
    boolean done = false;
    while (client.connected() && !done) {
      while (client.available () > 0 && !done) {
        done = processIncomingByte (client.read ());
      }
    }  // end of while client connected

    // get ROV status values as json string
    String rovStatus = getRovStatus();

    // send a standard http response header
    client.println(F("HTTP/1.1 200 OK"));
    client.println(F("Content-Type: text/json"));
    client.println(F("Connection: close"));  // close after completion of the response
    client.println();   // end of HTTP header
    client.println(rovStatus);

    // give the web browser time to receive the data
    delay(10);
    // close the connection:
    client.stop();
    Serial.println(F("Client disconnected"));
  }  // end of got a new client
}  // end of loop
Exemplo n.º 3
0
void setup() {

Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.println("ready");
}
Exemplo n.º 4
0
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();
        Serial.write(c);

        //*******************233333333333333333333333333333333333333333333*******
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) 
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: textml");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("<ml>");
          break;
        }
        if (c == '\n')
        {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}
Exemplo n.º 5
0
void loop() {
  client = server.available();
  if (client) {                
    while (client.connected()) {
      if (client.available()) {
        if (client.find("GET /")) {
          
          //INICIAR CRONOMETRAGEM
          if (client.find("setCron=")) {
            int charReaded = client.read();
			if(charReaded == 49){
              iniciarCronometragem();
            }
          }
		  
		   //RETORNA O TEMPO DESDE O INICIO		  
          if (client.find("getCron=")) {
            int charReaded = client.read();
			if(charReaded == 49){
              getCronometragem();
			}
          }
        }
      }
      Serial.println();
      break;
    }
    client.println(" HTTP/1.1 200 OK ");
  }
  // give the web browser time to receive the data
  delay(1);
  client.stop();
}
Exemplo n.º 6
0
void loopServer() {
	EthernetClient client = server.available();
	if (client) {
		while (client.connected()) {
			if (client.available()) {
				char c = client.read();

				//read char by char HTTP request
				if (readString.length() < 100) {

					//store characters to string
					readString += c;
					//Serial.print(c);
				}

				//if HTTP request has ended
				if (c == '\n') {

					///////////////
					Serial.print(readString); //print to serial monitor for debuging

					//now output HTML data header
					client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
					client.println(F("Content-Type: text/html"));
					client.println();

					client.println(F("Ok"));

					delay(1);
					//stopping client
					client.stop();

					if (readString.indexOf("R1=1") > 0){
						soldoRelays[0]->On();
//						Serial.println(">>>>>>>>>>>>");
//						Serial.println("R1=1");
					}
					if (readString.indexOf("R2=1") > 0){
						soldoRelays[1]->On();
//						Serial.println(">>>>>>>>>>>>");
//						Serial.println("R2=1");
					}
					if (readString.indexOf("R1=0") > 0){
						soldoRelays[0]->Off();
//						Serial.println(">>>>>>>>>>>>");
//						Serial.println("R1=0");
					}
					if (readString.indexOf("R2=0") > 0){
						soldoRelays[1]->Off();
//						Serial.println(">>>>>>>>>>>>");
//						Serial.println("R2=0");
					}
					readString="";

				}
			}
		}
	}
}
Exemplo n.º 7
0
void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!gotAMessage) {
      Serial.println("We have a new client");
      client.println("Hello, client!"); 
      gotAMessage = true;
    }

    // read the bytes incoming from the client:
    char thisChar = client.read();
    // echo the bytes back to the client:
    server.write(thisChar);
    // echo the bytes to the server as well:
    Serial.print(thisChar);
  }
}
Exemplo n.º 8
0
void NetworkConnectionClass::init()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());

  sensorData = new LinkedList<SensorData*>();
  state = RECV_DATA;
}
Exemplo n.º 9
0
bool SetupWebServer ( void )
{
    bool success = false;

#ifdef INTERFACE_ETHERNET
#ifdef ETHERNET_WEBSERVER
        server.begin();
#endif //ETHERNET_WEBSERVER
#endif // INTERFACE_ETHERNET

    return success;
}
Exemplo n.º 10
0
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
Exemplo n.º 11
0
void setup() {
  Serial.begin(9600);
 // Open serial communications and wait for port to open:
  pinMode(3, INPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  startRequest = false;
}
void setup()
{
	Serial.begin(9600);

	uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
	IPAddress myIP(192,168,0,6);

	Serial.println("Iniciando...");

	Ethernet.begin(mac,myIP);

	server.begin();

	Serial.println("Rodando!!!");
}
Exemplo n.º 13
0
void setup()
{
  Serial.begin(115200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(SPK, OUTPUT);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  for(int i = 0; i < MAX_TASKS; i++)
  {
    taskList[i][0] = 0;
  }
  createTask(1, 0, 0);
  Serial.println("setup complete");
}
Exemplo n.º 14
0
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print(F("server is at "));
  Serial.println(Ethernet.localIP());

  // setup Motors
  setupMotors();

}
Exemplo n.º 15
0
    void log_ethernet_logger(const char * name, const char * value, const char * unit){
        #ifdef DEBUG_ETHERNET_LOGGER
            DEBUG_1("Starting");
        #endif
        #ifdef ETHERNET_ENABLE_SERVER
            eth_server.print(millis(),DEC);
            eth_server.write(",");
            eth_server.write(name);
            eth_server.write(",");
            eth_server.write(value);
            eth_server.write(",");
            eth_server.write(unit);
            eth_server.println(",");
        #endif
        #ifdef ETHERNET_ENABLE_MQTT
            if (!mqtt_client.connected()){
                #ifdef ETHERNET_MQTT_USER
                    mqtt_client.connect(ETHERNET_MQTT_CLIENT, ETHERNET_MQTT_USER, "ETHERNET_MQTT_PASS");
                #else
                    mqtt_client.connect(ETHERNET_MQTT_CLIENT);
                #endif
                if (!mqtt_client.connected()){
                    return;
                }
            }
            char *nbuf;
            char *vbuf;
            int len;

            nbuf=(char*)malloc(sizeof(char)*(strlen(name)+1));
            vbuf=(char*)malloc(sizeof(char)*(strlen(name)+1));
            strcpy(nbuf, name);
            strcpy(vbuf,value);

            mqtt_client.publish(nbuf, vbuf);

            free(nbuf);
            free(vbuf);
        #endif
        #ifdef DEBUG_ETHERNET_LOGGER
            DEBUG_1("Finishing");
        #endif
    }
Exemplo n.º 16
0
void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}
Exemplo n.º 17
0
void loop()
{
	Bootloader::poll();
	EthernetClient client = server.available();
	if (client)
	{
		Comm comm(&client);
		comm.send( F("time"), millis() );

		CommClient cc = comm.available();
		if (cc)
		{
			//TODO
		}

		delay(100);

	}
}
void loop() {

  client = server.available();

  if (client) {
    client.println("Toggling MODEM POWER");
    Serial.println("Toggling MODEM Power");

  if (ACTIVATED == false) {
    client.println(" PIN 13 -> HIGH ");
    digitalWrite( powerPIN , HIGH ); // TURN POWERTAIL ON 
    ACTIVATED = true;
  }
  else {
    client.println(" PIN 13 -> LOW");
    digitalWrite( powerPIN, LOW ); // TURN POWERTAIL OFF
    ACTIVATED = false;
  }
      
    client.stop();
  }

  switch ( Ethernet.maintain() ) {

    case 1:
      Serial.println("Error: renewed fail");
      break;
    case 2:
      Serial.println("Renewed success");
      break;
    case 3:
      Serial.println("Error: rebind fail");
      break;
    case 4:
      Serial.println("Rebind success");
      break;
    default:
      break;

  }
  
}
Exemplo n.º 19
0
void EthernetSup::begin(unsigned char *_mac, unsigned char *_ip)
{
    IPAddress ip(_ip[0], _ip[1], _ip[2], _ip[3]);
    Ethernet.begin(_mac, ip);
    server.begin();
    
    for (char i = 0; i < MAX_BUTTONS; i++)
    {
      buttonId[i] = -1;
      buttonType[i] = -1;
      buttonState[i] = 0;
      for (char j = 0; j < MAX_TEXT_BUTTON; j++)
      {
        buttonText[i][j] = 0;
      }
      dimmerStep[i] = -1;
      dimmerValue[i] = 0;
      dimmerDirection[i] = -1;
    }
}
Exemplo n.º 20
0
void setup() {
   // open the serial port
  Serial.begin(9600);
  // start the Ethernet connection:
  Serial.println("Trying to get an IP address using DHCP");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // initialize the ethernet device not using DHCP:
    Ethernet.begin(mac, ip, gateway, subnet);
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  // start listening for clients
  server.begin();
 
}
Exemplo n.º 21
0
    void init_ethernet_logger(){
        #ifdef DEBUG_ETHERNET_LOGGER
            DEBUG_1("Starting");
        #endif
        byte mac[]={ETHERNET_MAC_ADDRESS};
        #ifdef ETHERNET_IP_ADDRESS
            byte ip[]={ETHERNET_IP_ADDRESS};
            byte dns[]={ETHERNET_DNS_ADDRESS};
            byte gw[]={ETHERNET_GW_ADDRESS};
            byte nm[]={ETHERNET_NETMASK};
            Ethernet.begin(mac,ip,dns,gw,nm);
        #else
            // Use DHCP
            Ethernet.begin(mac);
        #endif
        #ifdef ETHERNET_ENABLE_SERVER
            eth_server.begin();
        #endif


        #ifdef DEBUG_ETHERNET_LOGGER
            DEBUG_1("Finishing");
        #endif
        }
Exemplo n.º 22
0
void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
}
Exemplo n.º 23
0
void loop() {
 
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  int mois = analogRead(1);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
  }
 
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
   client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
        
          // output the value of the DHT-11
            client.print("<div id='banner'>");
            client.print(h);
            client.print(",");
            client.print(t);
            client.print(",");
            client.print(mois);
           client.print("</div>");
           
                
         
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}
Exemplo n.º 24
0
unsigned char EthernetSup::available() 
{
  unsigned char ret = 0;
  
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true;
    boolean isReferer = false;
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();

        if (!isReferer)
        {
          findButtonId(client, c);
          findDimmerValue(client, c);
          isReferer = checkReferer(client, c);
        }

        if (c == '\n' && currentLineIsBlank) 
        {
          // send a standard http response header
          printP(client, http200);
          printP(client, contenttype);
          printP(client, connkeep);  
          printP(client, doctype);

          // Verificando o tipo de botao
          if (buttonIdx != -1)
          {
            if (buttonType[buttonIdx] == ONOFF_BUTTON)
            {
              buttonState[buttonIdx] = (buttonState[buttonIdx] ? 0 : 1);
            }
            else if (buttonType[buttonIdx] == DIMMER_BUTTON)
            {
              if (dimmerDirection[buttonIdx] == 1)
              {
                if (dimmerValue[buttonIdx] + dimmerStep[buttonIdx] < 255)
                  dimmerValue[buttonIdx] += dimmerStep[buttonIdx];
                else
                  dimmerValue[buttonIdx] = 255;
              }
              else if (dimmerDirection[buttonIdx] == 2)
              {
                if (dimmerValue[buttonIdx] - dimmerStep[buttonIdx] > 0)
                  dimmerValue[buttonIdx] -= dimmerStep[buttonIdx];
                else
                  dimmerValue[buttonIdx] = 0;
              }
            }
          }
          
          printP(client, head_ini);
          printP(client, stylesheet);
          printP(client, head_fim);
          printP(client, div_ini);
          
          for (int i = 0; i < MAX_BUTTONS; i++)
          {
            if (buttonType[i] != -1)
            {
              if (buttonType[i] == DIMMER_BUTTON)
              {
                printP(client, dimmer_ini1);
                client.print(buttonText[i]);

                // converting to percent
                int val1 = map(dimmerValue[i], 0, 255, 0, 100);
                client.print(val1, DEC);
                client.print("%");

                printP(client, dimmer_ini2);

                // link do dimmer UP
                printP(client, btnid);
                client.print(buttonId[i], DEC);
                printP(client, dimmerdown);
                printP(client, dimmer_mid11); 
                printP(client, colorgreen);
                printP(client, dimmer_mid12);
                printP(client, dimmer_space);
                printP(client, dimmer_space);
                client.print("-");
                printP(client, dimmer_space);
                printP(client, dimmer_space);
                printP(client, dimmer_mid2); 

                // link do dimmer DOWN
                printP(client, btnid);
                client.print(buttonId[i], DEC);
                printP(client, dimmerup);
                printP(client, dimmer_mid21);
                printP(client, colorgreen);
                printP(client, dimmer_mid22);
                printP(client, dimmer_space);
                printP(client, dimmer_space);
                client.print("+");
                printP(client, dimmer_space);
                printP(client, dimmer_space);
                printP(client, dimmer_fim); 
              }
              else
              {
                printP(client, button_ini);

                // link do botao
                printP(client, btnid);
                client.print(buttonId[i], DEC);
                printP(client, button_mid1);
                
                // cor do botao
                if (buttonState[i] == 1)
                {
                  printP(client, colorred);
                }
                else
                {
                  printP(client, colorblue);
                }
                printP(client, button_mid2);

                // texto do botao
                client.print(buttonText[i]);
                printP(client, button_fim);  
              }
            }
          }
          printP(client, div_fim);
          
          ret = 1;
          break;
        }

      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
  
  return ret;
}
Exemplo n.º 25
0
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        
        if(c == '?'){
          startRequest = true;
          noReq = false;
        }else if(c == ' '){
           startRequest = false;
        }
       
        if(startRequest){
          httpReq += c;
        }
       
        if(light){
          button = "Turn the system off.";
          action = "/?OFF";
          sense();
        }else{
          button = "Arm the system.";
          action = "/?ON";
          motionStart = "";
          measurement = "";
        }

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          if(noReq){
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head>");
            client.println("<link rel='stylesheet' href='//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css'><script type='text/javascript' src='http://code.jquery.com/jquery-2.1.1.min.js'></script><script type='text/javascript'>$.ajax({url: 'http://107.170.57.28/return.php', type: 'get', success: function (response){$('body').html(response)}});</script></head><body></body>");
            client.println("</head>");
            client.println("</html>");
          }else{
            client.println();
            client.print("{\"sensorLog\": \"");
            client.print(sensorLog);
            client.print(motionStart);
            client.print(measurement);
            client.print("\",\"action\": \"" + action + "\"}"); 
          }
          break;
        }

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    
    if(httpReq == "?ON"){
       digitalWrite(7, HIGH);
       digitalWrite(3, LOW);
       sensorLog = ("Calibrating sensor <br /> Done <br /> SENSOR ACTIVE <br /> Motion Detected at: ");
       measurement = " sec";
       light = true;
     }else if(httpReq == "?OFF"){
//       digitalWrite(7, LOW);
       sensorLog = ("System turned off");
       light = false;
     }else if(httpReq == "?PLAY"){
       play();
     }
     noReq = true;
     Serial.print("HTTPReq: ");
     Serial.print(httpReq);
     httpReq = "";
    // give the web browser time to receive the data
    delay(1);
    
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}
Exemplo n.º 26
0
void setup(void)
{
	status.reset();

	/// Discrete & Analog IO
	pinMode(13, OUTPUT);	// Arduino on-board LED
	pinMode(A0, OUTPUT);	// Buzzer

	/// Comm. Channels
	// UART
	Serial.begin(115200); 	Serial.flush();
	Serial.println("Starting up greenOmatic Duemilanove Testbed...");
	Serial.print("Program compiled on ");
	Serial.print(__DATE__);
	Serial.print(" at ");
	Serial.println(__TIME__);
	Serial.println();

	// RF
#ifdef INTERFACE_ASK_RX
	pinMode(RF_RX_PIN, INPUT);
	vw_set_rx_pin(RF_RX_PIN);
    vw_setup(RF_BAUD);
    vw_rx_start  ();
	Serial.print  ("ASK RF Receiver configured on PIN ");
	Serial.print  (RF_RX_PIN);
	Serial.print  (" @ ");
	Serial.print  (RF_BAUD, DEC);
	Serial.println(" baud.");
#endif //INTERFACE_ASK_RX

	// Ethernet
#ifdef INTERFACE_ETHERNET
	Serial.print("Starting Ethernet... ");

#ifdef ETHERNET_DYNAMIC_IP
	int eth_stat = Ethernet.begin(mac);
	if (0 == eth_stat)
	{
		Serial.println(" Problem starting ethernet !");
		status.ethernet_valid = status.ERROR;
	}
	else
	{
		Serial.print("Ethernet started, IP = ");
		Serial.println( Ethernet.localIP() );
		status.ethernet_valid = status.VALID;
	}
#else
	Ethernet.begin(mac, IPaddr);
	Serial.print("Ethernet started, IP = ");
	Serial.println( Ethernet.localIP() );
	status.ethernet_valid = status.VALID;
#endif //ETHERNET_DYNAMIC_IP

#ifdef ETHERNET_WEBSERVER
		server.begin();
#endif //ETHERNET_WEBSERVER

#ifdef ETHERNET_UDPCLIENT
		Udp.begin(localPort);
#endif //ETHERNET_UDPCLIENT

#endif

	/// Peripherals
	// I2C RTC
#ifdef PERIPHERAL_RTCC
	Wire.begin();
	rtc.begin();
	if (rtc.isrunning())
	{
		status.time_valid = status.VALID;
		GetDatetimeString(rtc.now());
	    Serial.print("RTCC configured on I2C.  Time is currently ");
	    Serial.println(currentTime);

#ifdef ETHERNET_UDPCLIENT
		//TODO: Get NTP Time
#else
	    // Compare RTC time to this programs compile time
	    DateTime rtcTime = rtc.now();
	    DateTime compileTime(F(__DATE__), F(__TIME__));

	    // If the compile-time is later (more recent) than the current RTC time, update the RTC
	    if (compileTime.secondstime() > rtcTime.secondstime())
	    {
	    	Serial.println("Program compile-time is later than RTC time; updating RTC.");
	    	rtc.adjust( DateTime(F(__DATE__), F(__TIME__)) );
	    }
#endif //ETHERNET_UDPCLIENT
	}
	else
	{
		status.time_valid = status.ERROR;
		// TODO, can we narrow this down further like with the DS1307RTC library?
	}
#endif

	Serial.println("\nInitialization complete!\n\n");
}
Exemplo n.º 27
0
void NetworkConnectionClass::run()
{
  client = server.available();
  while (client.connected()) {
    switch (state) {
      case IDLE:
        state = SEND_DATA;
        return;
      case RECV_DATA:
        getConfigData();
        for (LinkedList<SensorData*>::Iterator it = sensorData->begin(); it != sensorData->end(); it++) {
          if ((*it)->code == 12) {
            LightClass *temp = new LightClass();
            temp->init((*it)->pin);
            (*it)->dev = (SensorClass*)temp;
          }
          else if ((*it)->code == 0) {
            LedClass *d1 = new LedClass();
            d1->init((*it)->pin);
            (*it)->dev = (SensorClass*)d1;
          }
          else if ((*it)->code == 1) {
            MotorClass *d2 = new MotorClass();
            d2->init((*it)->pin);
            (*it)->dev = (SensorClass*)d2;
          }
          else if ((*it)->code == 2) {
            Serial.println("Street created");
            StreetLightClass *d3 = new StreetLightClass();
            d3->init((*it)->pin);
            (*it)->dev = (SensorClass*)d3;
          }
        }
        state = SEND_DATA;
        break;
      case SEND_DATA:
        unsigned long currentMillis = millis();

        if (currentMillis - previousMillis > interval) {
          previousMillis = currentMillis;

          for (LinkedList<SensorData*>::Iterator it = sensorData->begin(); it != sensorData->end(); it++) {
            // Check just sensors, not devices. Devices have code 0.
            if ((*it)->code != 0 && (*it)->code != 1 && (*it)->code != 2) {
              float dataValue;
              if ((*it)->code == 12) {
                dataValue = (*it)->dev->getValue("");
              }
              float newValueMax = (*it)->oldValue + (*it)->treshold;
              float newValueMin = (*it)->oldValue - (*it)->treshold;
              // Check if the sensor has exceeded threshold, send data and update oldValue of sensor
              //if (dataValue <= newValueMin || dataValue >= newValueMax) {
              sendData((*it)->name, String((int)dataValue));
              (*it)->oldValue = dataValue;
              //Serial.println((*it)->name);
              //Serial.println(dataValue);
              //}
            }
          }
        }

        checkForUpdate();

        state = SEND_DATA;
        break;
    }
  }
}
Exemplo n.º 28
0
int main(void)
{
	/// setup
	init();
	setup();

	/// loop control
	for(frame=0; ; ++frame)
	{
		digitalWrite(13, HIGH);

		status.reset();

	    String new_msg = "Loop #";
	    new_msg.concat(frame);

#ifdef PERIPHERAL_RTCC
	    /// check time
		if (status.VALID == status.time_valid)
		{
			GetDatetimeString(rtc.now());
		}

/* 		TODO: port RTC.chipPresent() functionality over to RTClib
	    if ( RTC.read(tm) )
	    {
	    	status.time_valid = status.VALID;
	    }
	    else
	    {
	       if ( RTC.chipPresent() )
	       {
	    	  status.time_valid = status.INVALID;
	 	      Serial.println("The DS1307 is stopped.  Please set the RTC time.");
	 	      Serial.println();
	       }
	       else
	       {
	    	 status.time_valid = status.UNINSTALLED;
	         Serial.println("DS1307 read error!  Please check the circuitry.");
	         Serial.println();
	       }
	     }
*/
#endif

	    /// Check interfaces for received messages
	    // Serial, direct to the Command Line Interface (CLI)
		if(Serial.available() > 0)
		{
			char 	buff_console [8];
			for(uint8_t len_console = 0x00; Serial.available() > 0; len_console++)
			{
				buff_console[len_console] = Serial.read();
				CLI(buff_console, len_console);
			}
		}

#ifdef INTERFACE_ASK_RX
	    // RF (1-wire ASK, aka VirtualWire), print to console
		uint8_t  buff_rf   [VW_MAX_MESSAGE_LEN];
		uint8_t  len_rf  =  VW_MAX_MESSAGE_LEN;
		if(vw_get_message(buff_rf, &len_rf))
		{
#ifdef PERIPHERAL_RTCC
			// Prefix received messages with current date-time on console
			if (status.VALID == status.time_valid)
			{
				Serial.print(currentTime);
			    Serial.write(" | ");
			}
#endif //PERIPHERAL_RTCC
			Serial.print("RF Received :  ");
			for(uint8_t i = 0; i < len_rf; i++)
			{
				Serial.print((char)buff_rf[i]);
			}
			Serial.println();
		}
#endif //INTERFACE_ASK_RX

#ifdef ETHERNET_WEBSERVER
		  EthernetClient client = server.available();
		  if (client) {
		    Serial.println("new http client");
		    // an http request ends with a blank line
		    boolean currentLineIsBlank = true;
		    while (client.connected())
		    {
		      if (client.available())
		      {
		        char c = client.read();
		        Serial.write(c);
		        // if you've gotten to the end of the line (received a newline
		        // character) and the line is blank, the http request has ended,
		        // so you can send a reply
		        if (c == '\n' && currentLineIsBlank)
		        {
		          // send a standard http response header
		          client.println("HTTP/1.1 200 OK");
		          client.println("Content-Type: text/html");
		          client.println("Connection: close");  // the connection will be closed after completion of the response
		          client.println("Refresh: 60");  // refresh the page automatically every 60 sec
		          client.println();
		          client.println("<!DOCTYPE HTML>");
		          client.println("<html>");

#ifdef PERIPHERAL_RTCC
		          client.print("green-O-matic RTC Time : ");
		          client.println(currentTime);
#endif //PERIPHERAL_RTCC

#ifdef INTERFACE_ASK_RX
		          client.println("Most recently received 433MHz ASK Transmission : ");
#endif //INTERFACE_ASK_RX

		          client.println("</html>");
		          break;
		        }
		        if (c == '\n')
		        {
		          // you're starting a new line
		          currentLineIsBlank = true;
		        }
		        else if (c != '\r')
		        {
		          // you've gotten a character on the current line
		          currentLineIsBlank = false;
		        }
		      }
		    }
		    // give the web browser time to receive the data
		    delay(5);
		    // close the connection
		    client.stop();
		    Serial.println("client disconnected");
		  }
#endif //ETHERNET_WEBSERVER

		digitalWrite(13, LOW);

		delay (LOOP_DELAY);
	};
	return 0;
}
void ArduinoConnectEthernet::progMode(EthernetServer server)
{
	setConnected(true);
	while(isConnected() == true)
	{
		EthernetClient client = server.available();
	    if (client) 
	    {
	        boolean currentLineIsBlank = true;
	        input = "";
	        while (client.connected()) 
	        {
	            if (client.available()) 
	            {
	                char c = client.read();
	                if(reading && c == ' ') reading = false;
	                if(c == '?') 
	                {
	                    reading = true;
	                }

	                if(reading)
	                {
	                	//opt
	                	//Serial.println(c);
	                    if (c!='?') 
	                    {
	                        input += c;
	                    }
	                }

	                if(c == '\n' && currentLineIsBlank)  
	                {
	                    break;
	                }

	                if (c == '\n') 
	                {
	                    currentLineIsBlank = true;
	                }
	                else if (c != '\r') 
	                {
	                    currentLineIsBlank = false;
	                }
	            }
	        }
	        memset(buffer,'\0',sizeof(buffer));
	        String httpValue = "arduinoValue: ";
	        bool readCmd = false;
	        parseRequest(input);
	        if(result[0] == "pinMode")
			{
				if(result[2] == "0")
				{
					result[1].toCharArray(buffer, 50);
					pinMode(atoi(buffer), INPUT);
					Serial.println(result[0] + " " + result[1] + " " + result[2]);
				}
				else if(result[2] == "1")
				{
					result[1].toCharArray(buffer, 50);
					pinMode(atoi(buffer), OUTPUT);
					Serial.println("1");
				}
				else if(result[2] == "2")
				{
					result[1].toCharArray(buffer, 50);
					pinMode(atoi(buffer), INPUT_PULLUP);
					Serial.println("1");
				}
			}
			else if(result[0] == "digitalWrite")
			{
				if(result[2] == "0")
				{
					result[1].toCharArray(buffer, 50);
					digitalWrite(atoi(buffer), LOW);
					Serial.println("lw");
				}
				else if(result[2] == "1")
				{
					result[1].toCharArray(buffer, 50);
					digitalWrite(atoi(buffer), HIGH);
					Serial.println("hi");
				}
			}
			else if(result[0] == "digitalRead")
			{
				result[1].toCharArray(buffer, 50);
				Serial.println(digitalRead(atoi(buffer)));
				if(digitalRead(atoi(buffer)))
				{
					httpValue = httpValue + "HIGH";
				}
				else
				{
					httpValue = httpValue + "LOW";
				}
				readCmd = true;
			}
			else if(result[0] == "analogWrite")
			{
				result[1].toCharArray(buffer, 50);
				char buffer2[50];
				result[2].toCharArray(buffer2, 50);
				analogWrite(atoi(buffer),atoi(buffer2));
				Serial.println("1");
			}
			else if(result[0] == "analogRead")
			{
				result[1].toCharArray(buffer, 50);
				Serial.println(analogRead(atoi(buffer)));

				httpValue = httpValue + analogRead(atoi(buffer));
				readCmd = true;
			}
			else if(result[0] == "continueFlow")
			{
				setConnected(false);
				Serial.println("1");
			}
			client.println("HTTP/1.1 200 OK");
			if(readCmd)
			{
				client.println(httpValue);
	    	}
	    	client.println("Connection: close");
	        client.stop();
	        delay(100);
		}
	}
	
}
Exemplo n.º 30
0
void tcpConnection()
{ 
  if(client)
  {
    static boolean hello = false;
    
    if(client.connected())
    {
      
      if(client.available())
      {
        if(!hello)
        {
          Serial.println("client present");
          client.println("Hello!");
          hello = true;
        }
        else
        {
          char s = client.read();
          
          if(!(s == 13 || s == 10))
          {
            stringbuilder += s;
          }
          
          if(s == '\n' && stringbuilder != "")
          {
            Serial.println(stringbuilder);
            Serial.println(Contains(stringbuilder, ","));
            if(Contains(stringbuilder, ","))
            {
              int id = stringbuilder.substring(IndexOf(stringbuilder, ",")).toInt();
              client.println(id);
              stringbuilder = RemoveFirst(stringbuilder, id + ",");
              client.println(stringbuilder);
              int cyc = stringbuilder.substring(IndexOf(stringbuilder, ",")).toInt();
              client.println(id);
              stringbuilder = RemoveFirst(stringbuilder, id + ",");
              client.println(stringbuilder);
              int inter = stringbuilder.substring(0).toInt();

              createTask(id, cyc, inter);
              client.print("Created task(");
              client.print(id);
              client.print(",");
              client.print(cyc);
              client.print(",");
              client.print(inter);
              client.println(")");
            }
            else
            {
              if(stringbuilder.toInt() >= 0)
              {
                tone(SPK, stringbuilder.toInt(), 200);
              }
            }

            stringbuilder = "";
          }
        }
      }
    }
    else
    {
      Serial.println("client disconnected");
      client.stop();
      hello = false;
    }
  }
  else
  {
    client = server.available();
  }

}