Étape 2: L’Arduino partie
Créez les deux fichiers suivants en vous IDE Arduino :
text_reader.c contient la fonction readFile(), qui lit le fichier /tmp/arduino.txt et retourne son contenu. Si aucun fichier n’a encore été créé, il retourne la valeur null.
/* text_reader.c Part of "Intel IoT Edison web controlled LED demo" Copyright 2014 Pavlos Iliopoulos, techprolet.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include <stdio.h>#include <stdlib.h> #include <string.h> #include <malloc.h> char* readFile (){ FILE *f = fopen("/tmp/arduino.txt", "rb"); if (f != NULL) { fseek(f, 0, SEEK_END); long pos = ftell(f); fseek(f, 0, SEEK_SET); char *bytes = (char*)malloc(pos); fread(bytes, pos, 1, f); fclose(f); return bytes; } else { return NULL; } }
web_button.INO est le sketch arduino principal. Il lit la chaîne fournie par readFile(), il partage avec le délimiteur de changement de ligne ("\n"), analyse les données et permettra la LED allumé ou éteint, en conséquence.
/* web_button.ino Part of "Intel IoT Edison web controlled LED demo" Copyright 2014 Pavlos Iliopoulos, techprolet.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include <Wire.h> #include "rgb_lcd.h" #include "text_reader.c" const char * delimiter = "\n"; char * str; char * pch; rgb_lcd lcd; int ledPin = 13; bool ledStatus = false; bool lastLedStatus = false; bool stringIsOk = false; String remoteIp; void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); pinMode(ledPin, OUTPUT); digitalWrite(ledPin,LOW); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setRGB(0,0,0); // Print a message to the LCD. lcd.print("Remote IP:"); delay (1000); } void loop() { // put your setup code here, to run once: str = readFile(); pch = strtok (str, delimiter); if (pch != NULL){ ledStatus = (String(pch) == "true"); pch = strtok (NULL, delimiter); remoteIp = String (pch); pch = strtok (NULL, delimiter); stringIsOk = String (pch) == "OK"; //read the rest of the string, you can omit this while ((pch != NULL)) { pch = strtok (NULL, delimiter); } } if (stringIsOk && (ledStatus!=lastLedStatus)){ pinMode(ledPin, OUTPUT); digitalWrite(ledPin,ledStatus); Serial.println (ledStatus); //first, empty the screen from the old address lcd.setCursor(0, 1); lcd.print(" "); lcd.setRGB(0,0,ledStatus?255:00); lcd.setCursor(0, 1); lcd.print(remoteIp); Serial.println(remoteIp); Serial.println("-------------------"); lastLedStatus = ledStatus; } delay(100); }