Trimiterea a doua valori

Despre Internet Of Things

Trimiterea a doua valori

Mesajde dberger » 31 Iul 2017, 15:34

Am pornit de la o idee mai veche, statia meteo cu log pe SDcard. Vreau sa trimit valorile citite de pe diferiti senzori pe iot.robofun.ro Din pacate, nu reusesc sa trimit doua valori intrun singur sir de date. Pentru conectivitate la internet folosesc un shield Ethernet cu W5100.
In momentul de fata folosesc doar senzorul DHT22. Pot sa trimit doar o singura valoare umiditate sau temperatura.

Cod: Selectaţi tot
#include <SPI.h>
#include <Ethernet.h>

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//int t, h;

// assign a MAC address for the ethernet controller.
// fill in your address here:
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, 0, 177);

// fill in your Domain Name Server address here:
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

// initialize the library instance:
EthernetClient client;

char server[] = "iot.robofun.ro";
//IPAddress server();

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 600L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  dht.begin();
  //give the ethernet module time to boot up:
  delay(2000);

  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  // print the Ethernet board/shield's IP address:
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // 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.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    IoTpublish();
  }
}

// this method makes a HTTP connection to the server:
void IoTpublish() {
  // close any connection before send a new request.
  // This will free the socket on the shield
  client.stop();
  String SENSOR_TOKEN1="7apqevfeekvlvr6q966k32roen";//temperatura
  String SENSOR_TOKEN2="pbdcsprl6ojo03smrve1gsi4mp";//umiditate

  /*
  //citeste umiditatea relativa
  int h = dht.readHumidity();
  //Citeste temperatura
  int t = dht.readTemperature();
 
  String data = String("GET /api/v1/senzor/" + SENSOR_TOKEN1 + "/input?value=" + t + " HTTP/1.1");
  data = String("GET /api/v1/senzor/" + SENSOR_TOKEN2 + "/input?value=" + h + " HTTP/1.1");
  */
 
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("Conectat!");

    //citeste umiditatea relativa
    int h = dht.readHumidity();
    //citeste temperatura
    int t = dht.readTemperature();

    Serial.print("Temperatura: ");
    Serial.print(t);
    Serial.print(" C, ");
    Serial.print("Umiditatea: ");
    Serial.print(h);
    Serial.print(" %");
    Serial.println();
   
    // send the HTTP GET request:
    client.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    //client.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //client.println(data);
   
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //Serial.println(data);
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    lastConnectionTime = millis();

    // note the time that the connection was made:
    } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }

  }


Printr-un artificiu "urat" deschid cate un client pentru a trimite fiecare valoare in parte. Cum pot sa revin la varianta "ortodoxa" si sa trimit in-trun string ambele valori. In final vor fi mai multe, pentru restul senzorilor care trebuie adaugati ulterior.

Cod: Selectaţi tot
#include <SPI.h>
#include <Ethernet.h>

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//int t, h;

// assign a MAC address for the ethernet controller.
// fill in your address here:
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, 0, 177);

// fill in your Domain Name Server address here:
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

// initialize the library instance:
EthernetClient client;

char server[] = "iot.robofun.ro";
//IPAddress server();

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 60L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  dht.begin();
  //give the ethernet module time to boot up:
  delay(2000);

  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  // print the Ethernet board/shield's IP address:
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // 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.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    IoTpublish();
  }
}

// this method makes a HTTP connection to the server:
void IoTpublish() {
  // close any connection before send a new request.
  // This will free the socket on the shield
  client.stop();
  String SENSOR_TOKEN1="7apqevfeekvlvr6q966k32roen";//temperatura
  String SENSOR_TOKEN2="pbdcsprl6ojo03smrve1gsi4mp";//umiditate

  /*
  //citeste umiditatea relativa
  int h = dht.readHumidity();
  //Citeste temperatura
  int t = dht.readTemperature();
 
  String data = String("GET /api/v1/senzor/" + SENSOR_TOKEN1 + "/input?value=" + t + " HTTP/1.1");
  data = String("GET /api/v1/senzor/" + SENSOR_TOKEN2 + "/input?value=" + h + " HTTP/1.1");
  */
 
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("Conectat!");

    //citeste umiditatea relativa
    int h = dht.readHumidity();
    //citeste temperatura
    int t = dht.readTemperature();

    Serial.print("Temperatura: ");
    Serial.print(t);
    Serial.print(" C, ");
    Serial.print("Umiditatea: ");
    Serial.print(h);
    Serial.print(" %");
    Serial.println();
   
    // send the HTTP GET request:
    client.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    //client.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //client.println(data);
   
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //Serial.println(data);
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    //lastConnectionTime = millis();

    // note the time that the connection was made:
    } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
  client.stop();
  delay(250);
 
  if (client.connect(server, 80)) {
    Serial.println("Conectat!");

    //citeste umiditatea relativa
    int h = dht.readHumidity();
    //citeste temperatura
    int t = dht.readTemperature();

    Serial.print("Temperatura: ");
    Serial.print(t);
    Serial.print(" C, ");
    Serial.print("Umiditatea: ");
    Serial.print(h);
    Serial.print(" %");
    Serial.println();
   
    // send the HTTP GET request:
    //client.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    client.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //client.println(data);
   
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //Serial.println(data);
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    lastConnectionTime = millis();

    // note the time that the connection was made:
    } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }

  }
dberger
 
Mesaje: 81
Membru din: 23 Aug 2013, 17:07

Re: Trimiterea a doua valori

Mesajde informer » 31 Iul 2017, 17:54

Da` sa trimiti mai multe request-uri HTTP, cate unu` pt. fiecare senzor FARA sa faci client.stop si client.connect intre ele, ai incercat?
informer
 
Mesaje: 1372
Membru din: 11 Ian 2013, 11:17
Localitate: Timisoara

Re: Trimiterea a doua valori

Mesajde dberger » 31 Iul 2017, 18:18

informer scrie:Da` sa trimiti mai multe request-uri HTTP, cate unu` pt. fiecare senzor FARA sa faci client.stop si client.connect intre ele, ai incercat?


Da, am incercat. Imi apare mesajul HTTP/1.1 BAD_REQUEST. Un singur request pentru fiecare client e OK! Acelasi mesaj apare si la string (am crezut ca nu l-am scris corect).
Fişiere ataşate
400_BAD_REQUEST.jpg
dberger
 
Mesaje: 81
Membru din: 23 Aug 2013, 17:07

Re: Trimiterea a doua valori

Mesajde informer » 31 Iul 2017, 20:05

Ceva e ciudat... GET este un request HTTP iar HTTP-ul este un protocol request/response.
De exemplu un browser web este un client care poate trimite request-uri de tip GET fara sa fie nevoie sa deschizi cate un browser nou la fiecare click... :).
Nu prea inteleg de ce clientu` asta de Arduino nu merge....
informer
 
Mesaje: 1372
Membru din: 11 Ian 2013, 11:17
Localitate: Timisoara

Re: Trimiterea a doua valori

Mesajde dberger » 31 Iul 2017, 20:58

Da, interesant... dar nu inteleg de ce nu merge. Mai ales ca am vazut cateva exemple asemanatoare in doc-urile de pe blog/iot - robofun.ro
dberger
 
Mesaje: 81
Membru din: 23 Aug 2013, 17:07

Re: Trimiterea a doua valori

Mesajde informer » 31 Iul 2017, 21:30

Pui te rog exact codu` care l-ai folosit cu requesturi succesive de la un singur client?
informer
 
Mesaje: 1372
Membru din: 11 Ian 2013, 11:17
Localitate: Timisoara

Re: Trimiterea a doua valori

Mesajde dberger » 31 Iul 2017, 21:38

cu fiecare request in parte e OK! cu amandoua... BAD_REQUEST

Cod: Selectaţi tot
#include <SPI.h>
#include <Ethernet.h>

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//int t, h;

// assign a MAC address for the ethernet controller.
// fill in your address here:
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, 0, 177);

// fill in your Domain Name Server address here:
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

// initialize the library instance:
EthernetClient client;

char server[] = "iot.robofun.ro";
//IPAddress server();

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  dht.begin();
  //give the ethernet module time to boot up:
  delay(2000);

  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  // print the Ethernet board/shield's IP address:
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // 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.write(c);
  }
 

  // if ... seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    IoTpublish();
  }
}

// this method makes a HTTP connection to the server:
void IoTpublish() {
  // close any connection before send a new request.
  // This will free the socket on the shield
  client.stop();
  String SENSOR_TOKEN1="7apqevfeekvlvr6q966k32roen";//temperatura
  String SENSOR_TOKEN2="pbdcsprl6ojo03smrve1gsi4mp";//umiditate

  /*
  //citeste umiditatea relativa
  int h = dht.readHumidity();
  //Citeste temperatura
  int t = dht.readTemperature();
 
  String data = String("GET /api/v1/senzor/" + SENSOR_TOKEN1 + "/input?value=" + t + " HTTP/1.1");
  data = String("GET /api/v1/senzor/" + SENSOR_TOKEN2 + "/input?value=" + h + " HTTP/1.1");
  */
 
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("Conectat!");

    //citeste umiditatea relativa
    int h = dht.readHumidity();
    //citeste temperatura
    int t = dht.readTemperature();

    Serial.print("Temperatura: ");
    Serial.print(t);
    Serial.print(" C, ");
    Serial.print("Umiditatea: ");
    Serial.print(h);
    Serial.print(" %");
    Serial.println();
   
    // send the HTTP GET request:
    client.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    client.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //client.println(data);
   
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN1+ "/input?value=" +t+ " HTTP/1.1");
    //Serial.println("GET /api/v1/senzor/" +SENSOR_TOKEN2+ "/input?value=" +h+ " HTTP/1.1");
   
    //Serial.println(data);
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    lastConnectionTime = millis();

    // note the time that the connection was made:
    } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }

}
dberger
 
Mesaje: 81
Membru din: 23 Aug 2013, 17:07

Re: Trimiterea a doua valori

Mesajde informer » 31 Iul 2017, 21:52

Ia pune un client.println(); dupa fiecare GET ca altfel se cheama ca nu s-o terminat request-u`....
informer
 
Mesaje: 1372
Membru din: 11 Ian 2013, 11:17
Localitate: Timisoara

Re: Trimiterea a doua valori

Mesajde dberger » 31 Iul 2017, 22:07

informer scrie:Ia pune un client.println(); dupa fiecare GET ca altfel se cheama ca nu s-o terminat request-u`....


Pus. Aceeasi problema.
Fişiere ataşate
400_BAD_REQUEST_3.jpg
dberger
 
Mesaje: 81
Membru din: 23 Aug 2013, 17:07

Re: Trimiterea a doua valori

Mesajde informer » 31 Iul 2017, 22:35

No pai nu stiu atunci, sorry... asta e, inchide conexiunea cu stop si conecteaza-te iar cu acelasi client... nici nu-s sigur ca asta de fapt ii o problema la HTTP si oricum n-ai milioane de senzori... de fapt de ce te deranjeaza? :lol:
informer
 
Mesaje: 1372
Membru din: 11 Ian 2013, 11:17
Localitate: Timisoara

Următorul

Înapoi la Internet Of Things



cron