Étape 4: Rédaction du code
Maintenant vient le temps d’écrire du code !
Algorithme :
Nous voulons que notre appareil pour obtenir toutes les données des capteurs > pousser les données du capteur au cloud > obtenir les données du capteur sur notre application.
Copiez le code dans votre arduino IDE et puis remplacez les VARIABLE ID et jeton avec jetons et ID à votre compte de Ubidots.com.
CODE :
------------
#define WIFI_AP « iPhone » #define WIFI_PASSWORD « helloworld1 » #define WIFI_AUTH Edison WIFI_WPA / / le choix entre EDISONWIFI_OPEN, LWIFI_WPA ou LWIFI_WEP.
Ubidots information #define URL « things.ubidots.com » #define jeton « YOUR_TOKEN » / / remplacer par votre jeton de Ubidots générée dans votre onglet Profil #define VARIABLEID « SUNLIGHT_ID » #define VARIABLEID1 « TEMPRATURE_ID » //temprature #define VARIABLEID2 « Poussière SENSOR_ID » //dust #define VARIABLEID3 « AIR_SENSOR_ID » / / air qualité 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() {pinMode(8,INPUT) ; Serial.Begin(9600) ; myBarometer.init() ; garder une nouvelle tentative tant que connecté à l’AP Serial.println("Connecting to AP") ; tandis que (0 == EdisonWiFi.connect (WIFI_AP, EdisonWiFiLoginInfo (WIFI_AUTH, WIFI_PASSWORD))) {delay(1000);}} void loop() {température = myBarometer.bmp085GetTemperature(myBarometer.bmp085ReadUT()) ; //Get la température, bmp085ReadUT doit être appelée première durée = pulseIn (8, faible); lowpulseoccupancy = lowpulseoccupancy + durée ; ratio = lowpulseoccupancy/1000; / / pourcentage entier 0 = > concentration 100 = 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 un String poussière, Corneille String) {Serial.println ("envoi valeur à Ubidots...") ; C EdisonWiFiClient ; tandis que (! c.connect (URL, 80)) {Serial.println ("nouvelle tentative pour se connecter...") ; Delay(100) ; } String data = "{\"value\":"+ valeur + "}" ; String thisLength = String(data.length()) ; Construire c.print de demande HTTP POST ("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 c.print de demande HTTP POST ("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)) ; ////////////////////////////////////////////////////////// data = "{\"value\":"+ crow + "}"; thisLength = String(data.length()) ; Construire c.print de demande HTTP POST ("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)) ; /////////////////////////////////////////////////////////////////// data = "{\"value\":"+ dust + "}"; thisLength = String(data.length()) ; Construire c.print de demande HTTP POST ("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 tout en (c) {Serial.print((char)c.read());} c.stop() ; }
--------------
N’oubliez pas d’éditer les tags avec votre variable ID et ID de compte
Également modifier l’information wifi votre wifi SSID et le mot de passe.