Codigo v3, conexion a internet

#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(150,214,114,80);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,177);

// 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;

SoftwareSerial mySerial (8,9); // rx, tx
int ledPin = 13;

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

void loop(){
 
  //leer de 1
  Serial.println("leer 1");
  mySerial.print("g1");
  Serial.print("g1");
  //leer respuesta
  String lectura="21";
  while(mySerial.available()){
      lectura+=(char)mySerial.read() ;
  }    
  if(lectura!=""){
      Serial.println("\n1 dice:");
      Serial.print(lectura);
      
      // Conexion Internet
      // 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 congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // 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)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /insertGenerica.php?temperatura=21 HTTP/1.1");
    client.println("Host: 150.214.114.80:5901");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
         
      // Fin de conexin internet
      
      
  }else{Serial.println("no hay respuesta");}
      delay(3000);
  }

Tipo de post
Blog
Autor
yogurtur