Código fuente definitivo Servidor

Este es el código final del servidor. El código del cliente es el que se escribió el segundo día.

 

#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFD, 0xF5 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
// IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,234);  // numeric IP for no DNS.

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
//EthernetClient client;
EthernetClient client;

SoftwareSerial mySerial (8,9); // rx, tx
int ledPin = 13;
String colmena="a";
String idcolmenar="8";

void setup(){
  mySerial.begin (9600);
  Serial.begin (9600);
  pinMode(ledPin, OUTPUT);
  pinMode(8, INPUT);
  pinMode(9, OUTPUT);
  ConectarEthernet();
}

void loop(){

  //leer de 1
  Serial.println("Colmena 1 Peticion Temperatura:");
  mySerial.print("g1");
  //leer respuesta
  String lectura="";
  String cadena="";
  while(mySerial.available()){
    lectura+=(char)mySerial.read() ;
  }    
  if(lectura!=""){
    Serial.print("Colmena 1 Responde Temperatura: ");
    Serial.println(lectura  + "C");
    // if you get a connection, report back via serial:
     if (client.connect("150.214.114.80", 5901)) {
      Serial.println("*Conexion OK: Subida de datos. ");
      // Make a HTTP request:
      cadena="GET /insertGenerica.php?idcolmenar=" + idcolmenar + "&colmena=" + colmena + "&nombre=Temp&valor=" + lectura + " HTTP/1.1";
 
      client.println(cadena);
      Serial.println("       => idcolmenar= " + idcolmenar);
      Serial.println("       => colmena= " + colmena);
      Serial.println("       => temperatura= " + lectura + "C");
      client.println("Host: 150.214.114.80:5901");
      //client.println("Connection: close");
      client.println();
      client.stop();
      delay(10000);
    }
    else {
      // If you didn't get a connection to the server:
      Serial.println("connection failed");
      //ConectarEthernet();      
    }
  }
  else{
    Serial.println("no hay respuesta");
  }
  delay(3000);
}

void ConectarEthernet(){
 
  // Conexion Internet
  Serial.println("Intentando conexion...");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to configure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  else
  {
     Serial.println("Conexion con internet abierta. ");
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  //  if (client.connect(server, 5901)) {
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.stop();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed-");
  }
  // Fin de conexion internet

}

 

Tipo de post
Blog
Autor
jalopas