Stap 2: Het deel van de Arduino
Maak de volgende twee bestanden in je Arduino IDE:
text_reader.c bevat de functie ReadFile, die leest het bestand /tmp/arduino.txt en geeft als resultaat de inhoud. Als nog geen bestand hebt gemaakt, resultaat het leeg.
/* 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 is de belangrijkste arduino schets. Het leest de tekenreeks die door ReadFile, verstrekt opsplitsen met het scheidingsteken wijzigen regel ("\n"), parseert de gegevens en schakelt de LED in- of uitschakelen, dienovereenkomstig.
/* 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); }