Étape 18 : Écrire le code
Brûler ce code dans votre chambre
CODE-
#include « Barometer.h »
#include
#define WIFI_AP « iPhone »
#define WIFI_PASSWORD « helloworld1 »
#define WIFI_AUTH LWIFI_WPA / / le choix entre LWIFI_OPEN, LWIFI_WPA ou LWIFI_WEP.
Informations Ubidots
#define URL « things.ubidots.com »
#define jeton « WF1hbsrYltg5vwGDowAejTykHigEi5 » / / remplacer par votre jeton de Ubidots générée dans votre onglet de profil
#define VARIABLEID « 55d5f6047625422bxxxb0441 »
#define VARIABLEID1 « 55d5f5e37625xxxce1e0358f » //temprature
#define VARIABLEID2 « 55d4c74xxx25424830973c35 » //dust
#define VARIABLEID3 « 55d5xxx7762542363264e715 » //crow
Baromètre myBarometer ;
température du flotteur ;
unsigned longue durée ;
unsigned long starttime ;
unsigned long sampletime_ms = 3000 ;
unsigned long lowpulseoccupancy = 0 ;
ratio de flotteur = 0 ;
concentration de flotter = 0 ;
int compteur = 0 ;
void setup()
{
LTask.begin() ;
LWiFi.begin() ;
pinMode(8,INPUT) ;
Serial.Begin(9600) ;
myBarometer.init() ;
garder une nouvelle tentative jusqu'à ce que connecté à AP
Serial.println (« Connecting to AP ») ;
tandis que (0 == LWiFi.connect (WIFI_AP, LWiFiLoginInfo (WIFI_AUTH, WIFI_PASSWORD)))
{
Delay(1000) ;
}
}
void loop()
{
température = myBarometer.bmp085GetTemperature(myBarometer.bmp085ReadUT()) ; Obtenir la température, bmp085ReadUT doit être appelée en premier
durée = pulseIn (8, faible) ;
lowpulseoccupancy = lowpulseoccupancy + durée ;
ratio = lowpulseoccupancy/1000 ; Pourcentage de l’entier 0 = > 100
concentration = 1,1 * pow (rapport, 3)-3.8 * pow (ratio, 2) +520 * ratio + 0,62 ;
save_value(String(analogRead(a0)), String(temperature), String(concentration), String(analogRead(A2))) ;
Delay(500) ;
}
void save_value (String value, String, String poussière, Corneille String) {
Serial.println ("envoi valeur sur Ubidots...") ;
C LWiFiClient ;
tandis que (! c.connect (URL, 80))
{
Serial.println ("nouvelle tentative pour se connecter...") ;
Delay(100) ;
}
Chaîne de données = "{\"value\":"+ valeur + "}" ;
String thisLength = String(data.length()) ;
Construire la requête HTTP POST
c.Print ("POST /api/v1.6/variables/") ;
c.Print(VARIABLEID) ;
c.Print("/values?Token=") ;
c.Print(Token) ;
c.println ("HTTP/1.1") ;
c.println ("Content-Type : application/json") ;
c.println ("Content-Length:" + thisLength) ;
c.Print ("hôte:") ;
c.println(URL) ;
c.Print (données + « \n ») ;
c.Print(Char(26)) ;
données = "{\"value\":"+ un + "}" ;
thisLength = String(data.length()) ;
Construire la requête HTTP POST
c.Print ("POST /api/v1.6/variables/") ;
c.Print(VARIABLEID1) ;
c.Print("/values?Token=") ;
c.Print(Token) ;
c.println ("HTTP/1.1") ;
c.println ("Content-Type : application/json") ;
c.println ("Content-Length:" + thisLength) ;
c.Print ("hôte:") ;
c.println(URL) ;
c.Print (données + « \n ») ;
c.Print(Char(26)) ;
//////////////////////////////////////////////////////////
données = "{\"value\":"+ crow + "}" ;
thisLength = String(data.length()) ;
Construire la requête HTTP POST
c.Print ("POST /api/v1.6/variables/") ;
c.Print(VARIABLEID3) ;
c.Print("/values?Token=") ;
c.Print(Token) ;
c.println ("HTTP/1.1") ;
c.println ("Content-Type : application/json") ;
c.println ("Content-Length:" + thisLength) ;
c.Print ("hôte:") ;
c.println(URL) ;
c.Print (données + « \n ») ;
c.Print(Char(26)) ;
///////////////////////////////////////////////////////////////////
données = "{\"value\":"+ poussière + "}" ;
thisLength = String(data.length()) ;
Construire la requête HTTP POST
c.Print ("POST /api/v1.6/variables/") ;
c.Print(VARIABLEID2) ;
c.Print("/values?Token=") ;
c.Print(Token) ;
c.println ("HTTP/1.1") ;
c.println ("Content-Type : application/json") ;
c.println ("Content-Length:" + thisLength) ;
c.Print ("hôte:") ;
c.println(URL) ;
c.Print (données + « \n ») ;
c.Print(Char(26)) ;
//////////////////////////////////////////////////////
lire la réponse du serveur
alors que {(c)
Serial.Print((Char)c.Read()) ;
}
c.Stop() ;
}