Пример #1
0
bool CosmClient::connectViaGateway(byte macAddress[], IPAddress localIP, IPAddress dnsServerIP, IPAddress gatewayIP, IPAddress subnet)
{
    Serial.begin(9600);

    Ethernet.begin(macAddress, localIP, dnsServerIP, gatewayIP, subnet);

    if (_client.connect(_host, _port)) {
        return true;
    }
    else {
        Ethernet.begin(macAddress, localIP, dnsServerIP, gatewayIP, subnet);
        return (_client.connect(_host, _port));
    }
}
Пример #2
0
bool CosmClient::connectWithIP(byte macAddress[], IPAddress localIP)
{
    Serial.begin(9600);

    Ethernet.begin(macAddress, localIP);

    if (_client.connect(_host, _port)) {
        return true;
    }
    else {
        Ethernet.begin(macAddress, localIP);
        return (_client.connect(_host, _port));
    }
}
Пример #3
0
void sendData()
{
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect("kaylee.markild.no", 80))
  
  {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.println("GET /crap/test.txt HTTP/1.1");
    client.println("GET /crap/test.txt HTTP/1.1");
    Serial.println("Host: kaylee.markild.no");
    client.println("Host: kaylee.markild.no");
    Serial.println("User-Agent: ArduinoUno/r3 Ethernet Shield");
    client.println("User-Agent: ArduinoUno/r3 Ethernet Shield");
    Serial.println("Accept: */*");
    client.println("Accept: */*");    
    client.println();
  }
  else
  {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
// this method makes a HTTP connection to the server:
void sendData(String thisData) {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.print("PUT /v2/feeds/");
    client.print(FEEDID);
    client.println(".csv HTTP/1.1");
    client.println("Host: api.xively.com");
    client.print("X-ApiKey: ");
    client.println(APIKEY);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");
    client.println(thisData.length());

    // last pieces of the HTTP PUT request:
    client.println("Content-Type: text/csv");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.println(thisData);
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}
Пример #5
0
/*
 * Sends an HTTP POST request containing the name and the code to the server.
 */
void sendData() {
	Serial.print("attempting to send data (name = ");
	Serial.print(nameBuf);
	Serial.print(", code = ");
	Serial.print(buffer);
	Serial.println(")");


	// if you get a connection, report back via serial:
	if (client.connect(server, port)) {
		Serial.println("connected");
		// Make a HTTP request:
		client.print("POST /barcode");
		client.print("?name=");
		client.print(nameBuf);
		client.print("&code=");
		client.print(buffer);
		client.print("&uptime=");
		client.print(millis());
		client.println(" HTTP/1.0");
		client.println();
		client.flush();
		client.stop();
		Serial.println("request sent");
	}
	else {
		// if you didn't get a connection to the server:
		Serial.println("connection failed");
	}
}
void setup() {
  Serial.begin(9600);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
//---------------ETHERNET INIT
  // 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:
    for(;;)
      ;
  }
  // 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, 3000)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

}
Пример #7
0
void loop() {
  unsigned long time = millis();
  if (next < time) {
    Ethernet.maintain();
    next += send_delay;
    if (next <= time)
      next = time + 1;
    Serial.println("request time !");
    if (client.connected()) {
      Serial.println("already connected");
    } else {
      Serial.print("connecting results : ");
      Serial.println(client.connect("tarzan.info.emn.fr", 80));
    }
    int sent = client.println("GET /ping.php HTTP/1.1");
    sent = client.println(F("Host: tarzan.info.emn.fr"));
    client.println();
  }
  if (client.connected() && client.available() > 0) {
    Serial.println("client received data  : ");
    unsigned char buffer[buff_size + 1];
    while (client.available() > 0) {
      int read = client.read(buffer, buff_size);
      buffer[read] = '\0';
      Serial.println(read);
    }
  }
  if (radio.receiveDone()) {
  }
}
Пример #8
0
// this method makes a HTTP connection to the server:
void sendData(String thisData) {
    // if there's a successful connection:
    if (client.connect("www.pachube.com", 80)) {
        Serial.println("connecting...");
        // send the HTTP PUT request.
        // fill in your feed address here:
        client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n");
        client.print("Host: www.pachube.com\n");
        // fill in your Pachube API key here:
        client.print("X-PachubeApiKey: YOUR_KEY_HERE\n");
        client.print("Content-Length: ");
        client.println(thisData.length(), DEC);

        // last pieces of the HTTP PUT request:
        client.print("Content-Type: text/csv\n");
        client.println("Connection: close\n");

        // here's the actual content of the PUT request:
        client.println(thisData);

        // note the time that the connection was made:
        lastConnectionTime = millis();
    }
    else {
        // if you couldn't make a connection:
        Serial.println("connection failed");
    }
}
Пример #9
0
void CosmClient::sendData(uint32_t feedId, char datastreamId[], double dataToSend)
{
    if (_client.connect("api.cosm.com", 80)) {
        Serial.println("Connecting to Cosm...");

        _client.print("PUT /v2/feeds/");
        _client.print(feedId);
        _client.print("/datastreams/");
        _client.print(datastreamId);
        _client.print(".csv HTTP/1.1\n");
        _client.print("Host: api.cosm.com\n");

        _client.print("X-ApiKey: ");
        _client.print(_api);
        _client.print("\n");
        _client.print("Content-Length: ");

        int lengthOfData = getLength(dataToSend);
        _client.println(lengthOfData, DEC);

        _client.print("Content-Type: text/csv\n");
        _client.println("Connection: close\n");

        _client.print(dataToSend, DEC);
    }
}
Пример #10
0
void Nimbits::createPoint(String pointName)
{
  EthernetClient client;
  if (client.connect(GOOGLE, PORT))
  {
    client.println(F("POST /service/point HTTP/1.1"));
    String content;
    //  writeAuthParams(content);
    content += "email=";
    content += _ownerEmail;
    if (_accessKey.length() > 0)
    {
      content += ("&key=");
      content += (_accessKey);
    }
    content += "&action=create&point=";
    content += (pointName);
    client.println(F("Host:nimbits1.appspot.com"));
    client.println(F("Connection:close"));
    client.println(F("Cache-Control:max-age=0"));
    client.print(F("Content-Type: application/x-www-form-urlencoded\n"));
    client.print(F("Content-Length: "));
    client.print(content.length());
    client.print(F("\n\n"));
    client.print(content);
    while (client.connected() && !client.available())
    {
      delay(1); //waits for data
    }
    client.stop();
    client.flush();
  }

}
Пример #11
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:
  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, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }

}
Пример #12
0
void Nimbits::recordValue(double value, String pointId) {
  EthernetClient client;

 String json;
 json =  "{\"d\":\"";
 json += floatToString(value, 4);

json +=  "\"}";
  String content;

  content += "&json=";
  content += json;
  content += "&id=";
  content +=  pointId;



  if (client.connect(_hostname.c_str(), _port)) {

    doPost(client, VALUE_API, content);

    String response = getFullResponse(client);


    client.stop();
  }
  else {

    client.stop();
  }

}
//builds the url for the api request, connects to the server, and sends the request
boolean serverRequest() {
	//Serial.print(server);
	//Serial.println(page);
	client.stop();

	Serial.println("connecting...");
	if (client.connect(server, 80)) {
		Serial.println("connected");
		client.print("GET /makermanager/index.php?r=api/toolValidate&badge=");
		client.print(rfid_long);
		client.print("&tool=1");
		client.println(" HTTP/1.1");
		client.print("Host: ");
		client.println(server);
		client.println("User-Agent: arduino-ethernet");
		client.println("Connection: close");
		client.println("");
		
		//Serial.println(cmd);
		//client.println(cmd);
		return true;
	}
	else {
		Serial.println("connection failed");
		return false;
	}
}
Пример #14
0
void EC_Connect(void)
{
#ifdef USE_RS485
 #if _DEBUG_>0
	Serial.println(F("Connected to EnergyCam via RS485."));
 #endif
	RS485_ENABLE_TX;
	ec_state = EC_OK;
#else

#define EC_CONNECT_TIMEOUT	5000	// millis
	int reply;
	byte ec_ip[] = {192,168,100,48};
	int ec_port = 8088;
//  int8_t repl = 0;
#if _DEBUG_>0
	Serial.print(F("Connecting to EnergyCam ... "));
#endif
	// if you get a connection, report back via serial:
	timeout = millis();  // time within to get reply
	while (1) {
		if ( (millis()-timeout)>EC_CONNECT_TIMEOUT ) {  // if no answer received within the prescribed time
			//time_client.stop();
#if _DEBUG_>0
			Serial.print(F("timed out..."));
#endif
			break;
		}
		WDG_RST;
		reply = ec_client.connect(ec_ip, ec_port);
		if ( reply>0 ) break;
		else  delay(100);
	}
	if ( reply<=0 ) {
#if _DEBUG_>0
		// if you didn't get a connection to the server:
		Serial.print(F("failed: "));
		if ( reply==-1 ) Serial.println(F("timed out."));
		else if ( reply==-2 ) Serial.println(F("invalid server."));
		else if ( reply==-3 ) Serial.println(F("truncated."));
		else if ( reply==-4 ) Serial.println(F("invalid response"));
		else Serial.println(reply);
#endif
		ec_state = CONNECTION_TIMEOUT;  // to avoid error code 0
	} else {
    // connection was successful.
#if _DEBUG_>0
		Serial.println(F("done."));
#endif
		ec_state = EC_OK;
	}
	
#endif	// #ifdef USE_RS485
}
void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  client.connect(server, 3000);

  char store_id[30];
  
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i]);
  } 
	Serial.println();

  unsigned int hex_num;
  hex_num =  mfrc522.uid.uidByte[0] << 24;
  hex_num += mfrc522.uid.uidByte[1] << 16;
  hex_num += mfrc522.uid.uidByte[2] <<  8;
  hex_num += mfrc522.uid.uidByte[3];

  int  NFC_id=  (int)hex_num;

  sprintf(store_id,"%d" ,NFC_id);

  Serial.print("Store id is: ");
  Serial.println(store_id);

  char req[80];
  strcpy(req, "GET /?uid=");
  strcat(req, store_id);
  strcat(req, "&lat=48.139867&long=11.560935&sensor=MUCHbf HTTP/1.0");
    
  // Make a HTTP request:
  client.println(req);
  client.println();
  Serial.println("Request sent");
  Serial.println(req);

  
  // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

}
// This method makes an HTTP connection to the server
void httpRequest(String link) {
	//if there is a successful connection
	if (client.connect(server, 80)) {
		client.println("Get " + link + " HTTP/1.0 ");
		client.println();

	} else {
		// You couldn't make the connection
		Serial.println("Connection Failed");
		Serial.println("Disconnecting.");
	}
		client.stop(); }
Пример #17
0
void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}   
Пример #18
0
void api_call_http_register_self(byte amsg_server[]) {


	char path[ 30 ];
	char host[ 30 ];
	strcpy_P(path, msg_server_path);
	strcpy_P(host, msg_server_host);

	EthernetClient client;


	Serial.print("HTTP opening connection to ");
	Serial.println(host);
	/*
	if (!ether.dnsLookup(website))
		  Serial.println("DNS failed");
	else
		  Serial.println("DNS resolution done");
	ether.printIp("SRV IP:\t", ether.hisip);
	*/

	if (!client.connect(amsg_server, pgm_read_word(&msg_server_port))) {
		Serial.println("HTTP failed");
		return;
	}

	char d;
	String netbuf = "GET ";
	netbuf += path;
	netbuf += " HTTP/1.0\nHost: ";
	netbuf += host;
	netbuf += "\nConnection: close\n\n\n";

	Serial.print("HTTP connected to ... ");

	char sockbuf[ netbuf.length() ];
	netbuf.toCharArray(sockbuf, netbuf.length());
	client.write(sockbuf);
	delay(100);
	while(client.connected()) {
		while(client.available())   {
			d = client.read();
			if (d == '\n')
				Serial.print("\r\n");
			else
				Serial.print(d);
		}
	}	  
	client.stop();
	Serial.println();
}
Пример #19
0
String Nimbits::login(String email, String password) {
  EthernetClient client;
  _email = email;
  _password = password;
  String content;
  content = "email=";
  content += email;
  content += "&token=";
  content += _password;




  if (client.connect(_hostname.c_str(), _port)) {

    doPost(client, SESSION_API, content);
    String response = getFullResponse(client);
    String str = getContent(response);
  Serial.println(str);
    client.stop();

          int str_len = str.length() + 1;

          StaticJsonBuffer<1024> jsonBuffer;
          char char_array[str_len];

          // Copy it over
          str.toCharArray(char_array, str_len);

          JsonObject& root = jsonBuffer.parseObject(char_array);

      if (!root.success()) {
        _authToken = "";
        return "";

      }

      _authToken = root["token"];

  }
  else {

    client.stop();
  }
  return _authToken;



}
Пример #20
0
double Nimbits::getValue(String point) {
  EthernetClient client;

  String content;

  content += "&id=";
  content +=  (_email + "/" + point);

  if (client.connect(_hostname.c_str(), _port)) {


    doGet(client, VALUE_API, content);
    String response = getFullResponse(client);


    String str = getContent(response);

      client.stop();


      int str_len = str.length() + 1;
      StaticJsonBuffer<256> jsonBuffer;
      char char_array[str_len];

      // Copy it over
      str.toCharArray(char_array, str_len);

      JsonObject& root = jsonBuffer.parseObject(char_array);

  if (!root.success()) {

    return -1.0;

  }

  double d = root["d"];

  return d;


  }
  else {

    client.stop();
    return 0;
  }

}
Пример #21
0
int connectAndRead(){
	//connect to the server
	Serial.println(Ethernet.localIP());
	Serial.println("connecting for DB poll...");
  
	//port 80 is typical of a www page
	if (client.connect(server, 80)) {
		client.print("GET ");
		client.println(location);
		client.println();
		return 1;  // connection successful
	}
	else{
		return 0;  //connection failed
	}
}
Пример #22
0
void sendValues() { //Send values to www.onms.net server
    if (client.connect(server, 80)) {
        Serial.println("Connected to www.onms.net server...");
        client.print("GET ");
        client.print("/?update_id=");
        client.print(update_id);
        client.print("&key=humidity&value=");
        client.print(humidity);
        client.println(" HTTP/1.1");
        client.println("Host: www.onms.net");
        client.println();
        Serial.println("Disconnecting from www.onms.net server...");
        client.stop();
    } else {
        Serial.println("Connection to www.onms.net failed...");
    }
}
Пример #23
0
bool
Twitter::query_time(void)
{
  EthernetClient http;

  if (!http.connect(ip, port))
    {
      println(PSTR("query_time: could not connect to server"));
      return false;
    }

  http_print(&http, PSTR("HEAD "));

  if (proxy)
    {
      http_print(&http, PSTR("http://"));
      http_print(&http, server);
    }

  http_println(&http, PSTR("/ HTTP/1.1"));

  http_print(&http, PSTR("Host: "));
  http_print(&http, server);
  http_newline(&http);

  http_println(&http, PSTR("Connection: close"));

  http_newline(&http);

  while (http.connected())
    {
      if (!read_line(&http, buffer, buffer_len))
        break;

      if (buffer[0] == '\0')
        break;

      if (process_date_header(buffer))
        break;
    }

  http.stop();

  return basetime != 0L;
}
Пример #24
0
String Nimbits::recordValue(String point, float value)
{
  EthernetClient client;
  String content;

  if (client.connect(GOOGLE, PORT))
  {
    client.println(F("POST /service/value HTTP/1.1"));
    //  writeAuthParams(content);
    content += ("email=");
    content += _ownerEmail;
    if (_accessKey.length() > 0)
    {
      content += ("&key=");
      content += (_accessKey);
    }

    char buffer[10];

    dtostrf(value, 5, 5, buffer);
    String str = buffer;

    content += ("&value=");
    content += (str);
    content += ("&point=");
    content += (point);
    client.println(F("Host:nimbits1.appspot.com"));
    client.println(F("Connection:close"));
    client.println(F("Cache-Control:max-age=0"));
    client.print(F("Content-Type: application/x-www-form-urlencoded\n"));
    client.print(F("Content-Length: "));
    client.print(content.length());
    client.print(F("\n\n"));
    client.print(content);
    while (client.connected() && !client.available())
    {
      delay(1); //waits for data
    }
    client.stop();
    client.flush();
  }

  return content;
}
Пример #25
0
void Dreambox_Connect(void)
{
	int reply;
#if _DEBUG_>0
	Serial.print(F("Connecting to "));
	if (dm_device==DM800SE )
		Serial.print(F("DM800SE ... "));
	else
		Serial.print(F("DM800 ... "));
#endif
	// if you get a connection, report back via serial:
	long timeout = millis() + 3000;  // time within to get reply
	while (1) {
		if ( millis()>timeout ) {  // if no answer received within the prescribed time
			//time_client.stop();
#if _DEBUG_>0
			Serial.print(F("timed out..."));
#endif
			break;
		}
		WDG_RST;
		reply = dm_client.connect(dm_ip[dm_device], dm_port);
		if ( reply>0 ) break;
		else  delay(100);
	}
	if ( reply<=0 ) {
#if _DEBUG_>0
		// if you didn't get a connection to the server:
		Serial.print(F("failed: "));
		if ( reply==-1 ) Serial.println(F("timed out."));
		else if ( reply==-2 ) Serial.println(F("invalid server."));
		else if ( reply==-3 ) Serial.println(F("truncated."));
		else if ( reply==-4 ) Serial.println(F("invalid response"));
		else Serial.println(reply);
#endif
		dm_state[dm_device] = CONNECTION_TIMEOUT;
	} else {
    // connection was successful.
#if _DEBUG_>0
		Serial.println(F("done."));
#endif
		dm_state[dm_device] = DM_OK;
	}
}
Пример #26
0
long Nimbits::getTime()
{
  EthernetClient client;

  if (client.connect(GOOGLE, PORT))
  {
    client.print(F("GET /service/time?"));
    writeAuthParamsToClient(client);
    writeHostToClient(client);

    String response = getResponse(client);
    return atol(&response[0]);

  }
  else
  {
    return -1;
  }
}
Пример #27
0
float Nimbits::getValue(String pointName)
{
  EthernetClient client;

  if (client.connect(GOOGLE, PORT))
  {
    client.print(F("GET /service/value?"));
    writeAuthParamsToClient(client);
    client.print(F("&point="));
    client.print(pointName);
    writeHostToClient(client);

    return atof(&getResponse(client)[0]);
  }
  else
  {
    return -1;
  }

}
// this method makes a HTTP connection to the server:
void httpRequest() {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }
}
Пример #29
0
void updateDB_Door(){	
	Serial.println("connecting for DB update");
	if (client.connect(server, 80)) {
		client.print("GET http://153.42.193.63/ardi_db_update.php?door=");
		if(clientDoor)
			client.print(clientDoor,BIN);
		else
			client.print("'0'");
			
		client.print(" HTTP/1.0");
		client.println("Host: http://153.42.193.63");
		client.println();
		doorChanged = 1;
	} 
	else {  //connection failed
		Serial.println("connection failed");
		doorChanged = 0;
	}
	client.stop();
	client.flush();
}
Пример #30
0
Weather::ReturnVals Weather::GetVals(const char * key, uint32_t zip, const char * pws, bool usePws) const
{
	ReturnVals vals = {0};
	EthernetClient client;
	if (client.connect(m_wundergroundAPIHost, 80))
	{
		char getstring[255];
		trace(F("Connected\n"));
		if (usePws)
			snprintf(getstring, sizeof(getstring), "GET http://%s/api/%s/yesterday/conditions/q/pws:%s.json HTTP/1.1\r\n",m_wundergroundAPIHost, key, pws);
		else
			snprintf(getstring, sizeof(getstring), "GET http://%s/api/%s/yesterday/conditions/q/%ld.json HTTP/1.1\r\n",m_wundergroundAPIHost, key, (long) zip);

		//trace("GetString:%s\n",getstring);
		client.write((uint8_t*) getstring, strlen(getstring));
		
		//send host header
		snprintf(getstring, sizeof(getstring), "Host: %s\r\nConnection: close\r\n\r\n",m_wundergroundAPIHost);
		//trace("GetString:%s\n",getstring);
		client.write((uint8_t*) getstring, strlen(getstring));

		ParseResponse(client, &vals);
		vals.resolvedIP=client.GetIpAddress();

		client.stop();
		if (!vals.valid)
		{
			if (vals.keynotfound)
				trace("Invalid WUnderground Key\n");
			else
				trace("Bad WUnderground Response\n");
		}
	}
	else
	{
		trace(F("connection failed\n"));
		client.stop();
	}
	return vals;
}