Arduino : transmission de mesures

Transmission Wifi

Transmission de données via une requète GET à un serveur HTTP.

Il faut

100% Complete

Câblage

Arduino - CC3000

Code

// Transmission Wifi ***************************************************************************
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include "utility/debug.h"
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   2  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed
#define WLAN_SSID       "YourWifiSSID"           // cannot be longer than 32 characters!
#define WLAN_PASS       "WifiPassWord"
#define WLAN_SECURITY   WLAN_SEC_WPA2 // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS  3000      // Amount of time to wait (in milliseconds) with no data
                                   // received before closing the connection.  If you know the server
                                   // you're accessing is quick to respond, you can reduce this value.
#define WEBSITE      "www.yourwebsite.fr"
#define WEBPAGE      "yourpage.php"
String g_STR_Request;
uint32_t ip;

void setup() {

WifiInit();

}

void loop() {

  // Envoie les donnees au serveur HTTP
  WifiSend();
  delay( TIMER * 3600000 );
}

// ----------------------------------------------------------------------------------------
// Initilisation de la connection wifi.
// Une fois la procedure executee, la variable client est initialisee.
//
void WifiInit(){
  /* Initialise the module */
  Serial.println(F("\nInitialisation de la connection WIFI..."));
  if (!cc3000.begin()) {
    Serial.println(F("Couldn't begin()! Check your wiring?"));
    while(1);
  }
 
  Serial.print(F("\nTentative de connection a :")); Serial.println(WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  Serial.println(F("Connected!"));
 
  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP()) {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  
 
  // Try looking up the website's IP address
  ip = 0;
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
}

// ----------------------------------------------------------------------------------------
// Envoie un enregistrement dans la base des capteurs.
// @see : https://www.adafruit.com/products/1469
void WifiSend(){
  g_STR_Request = WEBPAGE;
    
  // ID du capteur --------------------------
  g_STR_Request += "&capteurID=";
  g_STR_Request.concat(CAPTEURS_ID);
    
  // Temperature --------------------------
  Serial.println("Lecture de la temperature");
  float temperatureF;
  if (getTemperature(&temperatureF, true) == READ_OK) {
    g_STR_Request += "&temperature=";
    g_STR_Request.concat(temperatureF);
  }
  // PH ------------------------------------
  Serial.println("Lecture du PH");
  float mesurePH;
  if( getPh( &mesurePH ) == READ_OK){
    g_STR_Request += "&ph=";
    g_STR_Request.concat(mesurePH);
  }
 
  /* Try connecting to the website.
     Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
  */
  Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
  Serial.print("Send request :");
  Serial.println( g_STR_Request );
  if (www.connected()) {
    // On va devoir convertir le Strring en char pour l'envoyer dans la fonction www.fastrprint
    int str_len = g_STR_Request.length() + 1;
    char l_CHR_Request[str_len];
    g_STR_Request.toCharArray(l_CHR_Request, str_len);  // Convertion en char
    
    www.fastrprint(F("GET "));
    www.fastrprint( l_CHR_Request );   
    www.fastrprint(F(" HTTP/1.1\r\n"));
    www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
    www.fastrprint(F("\r\n"));
    www.println();
    
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }
  Serial.println(F("-------------------------------------"));
 
  /* Read data until either the connection is closed, or the idle timeout is reached. */
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    while (www.available()) {
      char c = www.read();
      Serial.print(c);
      lastRead = millis();
    }
  }
  www.close();
  Serial.println(F("-------------------------------------"));
 
  /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  /* the next time your try to connect ... */
  Serial.println(F("\n\nDisconnecting"));
  cc3000.disconnect();
}

Autres projets dans la même section

Copyright © 2015 Alex-design.fr All rights reserved.