Last active
January 14, 2019 00:02
-
-
Save ypelletier/6253398 to your computer and use it in GitHub Desktop.
Envoi de données à carriots.com avec un arduino et un shield ethernet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************** Carriots_test.ino *********************** | |
* | |
* Exemple simple pour explorer les possibilités de carriots.com | |
* Basé sur un tutoriel de carriots.com | |
* | |
* Shield ethernet | |
* Potentiomètre branché à A3 | |
* LED branchée à D7 | |
* | |
* La LED est allumée quand la valeur reçue à A3 est supérieure à 500, | |
* et éteinte sur la valeur est inférieure à 500. | |
* Lorsque la LED change d'état, l'état de la LED et la valeur reçue à A3 | |
* sont envoyées à carriots.com | |
* | |
* http://electroniqueamateur.blogspot.com/2013/08/envoyer-des-donnees-carriotscom-avec-un.html | |
* | |
**********************************************************************/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
// Votre "full device APIKey" se trouve dans le control panel de carriots.com: My Settings -> My Account | |
// "id_developer" de votre "device" est de la forme defaultDevice@votre_login | |
const String APIKEY = "votre_APIKey"; // Mettre ici votre Full Privileges Apikey | |
const String DEVICE = "votre_id_developper"; // Mettre le "id_developer" de votre device | |
// Adresse MAC de votre Shield Ethernet. Il faut simplement éviter que deux appareils de votre réseau | |
// local aient la même adresse MAC (vous pouvez laisser la ligne suivante telle quelle dans la plupart | |
// des cas. | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
IPAddress server(82,223,244,60); // C'est l'adresse IP de api.carriots.com | |
EthernetClient client; | |
int LEDPin = 7; // Une LED est branchée à la sortie 7 | |
int PotPin = A3; // Un potentiomètre a son curseur branchée à l'entrée analogique 3 | |
int LED = 0; // État précédent de la LED (0 si éteinte, 1 si allumée) | |
int newLED = 0; // Nouvel état de la LED (0 si éteinte, 1 si allumée) | |
void setup() { | |
pinMode(LEDPin, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("Demarrage"); | |
// DHCP (dynamic host configuration protocol): on cherche une adresse ip pour | |
// votre shield Ethernet. | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Echec de la configuration DHCP Ethernet"); | |
while(true); // alors pas la peine d'aller plus loin | |
} | |
Serial.println("Reussite de la configuration DHCP Ethernet"); | |
delay(1000); | |
} | |
void loop() { | |
int val = analogRead(PotPin); // Valeur du potentiomètre | |
if (val > 500) { // 500 est la frontière arbitraire entre LED allumée et LED éteinte | |
newLED = 1; // On allume la LED | |
} | |
else { | |
newLED = 0; // on éteint la LED | |
} | |
digitalWrite(LEDPin, newLED); | |
if (LED != newLED) { // Si l'état de la LED a changé | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println("Envoi du Stream"); | |
LED = newLED; | |
sendStream(); // on envoi les données au serveur carriots | |
} | |
delay(500); | |
// Si on reçoit des données de l'internet, on les envoit au moniteur | |
// série (utile pour déboguer). | |
while (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
if (!client.connected()) { | |
client.stop(); | |
} | |
} | |
// Envoi des données | |
void sendStream() | |
{ | |
String txt1 = String(analogRead(PotPin)); //String contenant la valeur du potentiomètre | |
Serial.print("Valeur du potentiometre: " ); | |
Serial.println(txt1); // pour débogage | |
String txt2 = ""; | |
if ( LED == 0 ) { | |
txt2 = "LED_OFF"; // String indiquant l'état de la LED | |
} | |
else { | |
txt2 = "LED_ON"; | |
} | |
Serial.print("Etat de la LED: "); | |
Serial.println(txt2); // Pour débogage | |
if (client.connect(server, 80)) { // Si la connexion fonctionne | |
Serial.println("Connection etablie"); | |
String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":{\"valeur\":\""+txt1+"\",\"LED\":\""+txt2+"\"}}"; | |
// HTTP request | |
client.println("POST /streams HTTP/1.1"); | |
client.println("Host: api.carriots.com"); | |
client.println("Accept: application/json"); | |
client.println("User-Agent: Arduino-Carriots"); | |
client.println("Content-Type: application/json"); | |
client.print("carriots.apikey: "); | |
client.println(APIKEY); | |
client.print("Content-Length: "); | |
int thisLength = json.length(); | |
client.println(thisLength); | |
client.println("Connection: close"); | |
client.println(); | |
client.println(json); | |
} | |
else { | |
// la connexion n'a pas fonctionné | |
Serial.println("Echec de la connexion!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment