Étape 7: Code d’Arduino
#include « twApi.h »
#include « twLogger.h »
#include « twOSPort.h »
#include « WiFi.h »
#include « Ethernet.h »
#include « stdio.h »
#include « string.h »
#include « Wire.h »
Char [] ssid = "wifi_network_name" ;
passe de char [] = "wifi_password" ;
État int = WL_IDLE_STATUS ;
/ * Nom de chose créée dans les nuages * /
char * thingName = « Smart_Driving » ;
/ * IP/nom d’hôte du serveur * /
char * serverName = « cloud_name.com » ;
/ * port * /
int port = 80 ;
/ * Clé API créé * /
char * apiKey = « key_name » ;
/ * fréquence * /
int timeBetweenRefresh = 1000 ;
/ * Paramètres à mesurer * /
struct {double Steer ; double Piezo_vibration;} propriétés ;
Sub sendPropertyUpdate()
{
propertyList * proplist = twApi_CreatePropertyList ("Steer", twPrimitive_CreateFromNumber (propriétés. Steer), 0) ; Si (! proplist) {TW_LOG (TW_ERROR, « sendPropertyUpdate : liste de propriété allocation Error "); return;} twApi_AddPropertyToList (proplist, « Piezo_vibration », twPrimitive_CreateFromNumber (propriétés. Piezo_vibration), 0) ; twApi_PushProperties (TW_THING, thingName, proplist,-1, FALSE) ; twApi_DeletePropertyList(proplist) ;
}
Sub dataCollectionTask()
{
Assigner les ports correspondants dans le Conseil aux propriétés créées dans le code
N’importe quel capteur peut être utilisé en ce qui concerne l’usage
Faire l’étalonnage avec des valeurs en conséquence
int a = analogRead(A0) ; Delay(500) ; Propriétés. Steer = a ;
int b = analogRead(A1) ; Delay(500) ; Propriétés. Piezo_vibration = b ;
Serial.Print("Time:") ; Serial.Print(Millis()) ; Serial.Print ("Steering control:") ; Serial.Print (propriétés. Steer, 1) ; Serial.Print("") ; Serial.Print ("intensité de vibration piézoélectrique:") ; Serial.Print (propriétés. Piezo_vibration, 1) ; Serial.Print("") ; Serial.println() ;
sendPropertyUpdate() ;
}
enum msgCodeEnum propertyHandler (const char * entityName, const char * propertyName, twInfoTable ** valeur, char isWrite, void * userdata)
{
char * astérisque = "*" ; Si (! propertyName) propertyName = astérisque ; TW_LOG (TW_TRACE, « propertyHandler - fonction appelée pour entité %s, propriété de %s », entityName, propertyName) ; Si {(valeur)
Si (strcmp (propertyName, « Steer ») == 0) * valeur = twInfoTable_CreateFromNumber (propertyName, propriétés. Steer) ; else if (strcmp (propertyName, « Piezo_vibration ») == 0) * valeur = twInfoTable_CreateFromNumber (propertyName, propriétés. Piezo_vibration) ; else return TWX_NOT_FOUND ; Return TWX_SUCCESS ; } else {TW_LOG (TW_ERROR, « propertyHandler - pointeur NULL pour la valeur »); return TWX_BAD_REQUEST;}
}
void setup()
{int err = 0 ;
Serial.Begin(9600) ;
Delay(500) ;
Si (WiFi.status() == WL_NO_SHIELD)
{Serial.println ("la WiFi n’est pas connecté") ;
ne continuent pas :
while (true) ; }
tentez de vous connecter au réseau Wifi :
Si (status! = WL_CONNECTED)
{Serial.print ("Wifi se connecte à") ; Serial.println(SSID) ;
Se connecter au réseau WPA/WPA2 :
statut = WiFi.begin (ssid, pass) ;
Attendez 10 secondes pour le raccordement : delay(10000) ;
} Serial.println("You're connected to the network") ;
Serial.println ("système de l’éco-conduite intelligente") ;
Delay(500) ;
Serial.println ("Intel Edison communication avec nuage") ;
Delay(1000) ;
ERR = twApi_Initialize (nom du serveur, port, TW_URI, apiKey, NULL, MESSAGE_CHUNK_SIZE, MESSAGE_CHUNK_SIZE, TRUE) ; twApi_SetSelfSignedOk() ; twApi_RegisterProperty (TW_THING, thingName, « Steer », TW_NUMBER, NULL, « Toujours », 0, propertyHandler, NULL) ; twApi_RegisterProperty (TW_THING, thingName, « Piezo_vibration », TW_NUMBER, NULL, « Toujours », 0, propertyHandler, NULL) ; twApi_BindThing(thingName) ;
/ * Connexion au serveur * /
Si (! twApi_Connect (CONNECT_TIMEOUT, CONNECT_RETRIES))
{Serial.println ("Sucessefully connecté pour nuage!"); delay(1000);}
}
void loop()
{delay(1000) ;
dataCollectionTask() ;
}