galera.. to precisando de ajuda com o codigo abaixo..
ele conecta na placa do arduino, o client funciona mais nao retorna o valor que eu preciso..
só retorna conexao com falha no meu serial monitor
o codigo ta abaixo.. se alguem puder ajudar, VLW!
#include <SPI.h>
#include <Ethernet.h>
//#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
//#define FEEDID 00000 // replace your feed ID
//#define USERAGENT "My Project" // user agent is the project name
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 110 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask// byte mac[] = {
//0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};// fill in an available IP address on your network here,
// for manual configuration:
//IPAddress ip(192,168,2,20);
//IPAddress subnet(255,255,255,0);
// initialize the library instance:
EthernetClient client;// 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(192,168,1,100); // numeric IP for api.cosm.com
//char server[] = "api.cosm.com"; // name address for Cosm APIunsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.comvoid 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
}
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection:
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
}void loop() {
// read the analog sensor:
int sensorReading = analogRead(A0);
// convert the data to a String to send it:String dataString = "sensor1,";
dataString += sensorReading;// you can append multiple readings to this String if your
// Cosm feed is set up to handle multiple values:
int otherSensorReading = analogRead(A1);
dataString += "\nsensor2,";
dataString += otherSensorReading;// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(dataString);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}// this method makes a HTTP connection to the server:
void sendData(String thisData) {
// if there's a successful connection:
if (client.connect(server, 8080)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("PUT /api/post?apikey=4ff112f9dd9721f995d7996fcd5dfdcc&json={power:252.4,temperature:15.4}");
client.println();
delay(300);/* client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.cosm.com");
client.print("X-CosmApiKey: ");
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();
}
Tags:
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por