Stap 4: De Code
Nu ga in uw Arduino schets en plak deze code, moet u het wijzigen van uw IP-adres op de code van de mijne, samen met iets anders dat specifiek is voor uw computer.
Het bovenstaande is de twitter die was gewaarschuwd over mijn kat eten
Deze zelfstudies helpen enorm, en ik sterk aanbevelen lezen en bestuderen van hen.
http://tronixstuff.com/2013/12/04/Arduino-tutorial...
http://tronixstuff.com/2013/12/06/Arduino-tutorial...
(om je twitter token) https://dev.twitter.com/docs/auth/tokens-devtwittercom
en dan nu de code:
#include <spi.h><br>#include <ethernet.h> #include <twitter.h> #include <softwareserial.h> SoftwareSerial RFID(2, 3); // RX and TX byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // create MAC address for ethernet shield byte ip[] = {192,168,1,144}; // choose your own IP for ethernet shield Twitter twitter("2602202863-kZu53ICKT3Cx9VjgnG4R5O6XnHB0inzUTLFhOph"); // replace my token with your token char* Saying[] = {"Time for food.", "I'm having a snack", "I'm at my bowl", "What time is it? Food Time!", "Lunch sounds good", "Eating", "Fooooood", "I'm eating", "Le Feeeding Time", "Om nom nom", "Le Noms", "Time for feeding", "Food Time!", "FOOD!", "Time to Pig Out"};</softwareserial.h></twitter.h></ethernet.h></spi.h></p><p>int data1 = 0; int ok = -1; int yes = 13; int no = 12;</p><p>// use first sketch in <a href="http://wp.me/p3LK05-3Gk" rel="nofollow"> http://wp.me/p3LK05-3Gk </a> to get your tag numbers int tag1[14] = {2,48,51,48,48,52,70,52,70,50,50,50,49,3}; int tag2[14] = {2,52,48,48,48,56,54,67,54,54,66,54,66,3}; int newtag[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons</p><p>void setup() { RFID.begin(9600); // start serial to RFID reader Serial.begin(9600); // start serial to PC pinMode(yes, OUTPUT); // for status LEDs pinMode(no, OUTPUT); delay(5000); Ethernet.begin(mac, ip); Serial.begin(9600); }</p><p>boolean comparetag(int aa[14], int bb[14]) { boolean ff = false; int fg = 0; for (int cc = 0 ; cc < 14 ; cc++) { if (aa[cc] == bb[cc]) { fg++; } } if (fg == 14) { ff = true; } return ff; }</p><p>void checkmytags() // compares each tag against the tag just read { ok = 0; // this variable helps decision-making, // if it is 1 we have a match, zero is a read but no match, // -1 is no read attempt made if (comparetag(newtag, tag1) == true) { ok++; } if (comparetag(newtag, tag2) == true) { ok++; } }</p><p>void tweet(char msg[]) { Serial.println("connecting ..."); if (twitter.post(msg)) { // Specify &Serial to output received response to Serial. // If no output is required, you can just omit the argument, e.g. // int status = twitter.wait(); int status = twitter.wait(); if (status == 200) { Serial.println("OK."); } else { Serial.print("failed : code "); Serial.println(status); } } else { Serial.println("connection failed."); } } </p><p>void readTags() { ok = -1;</p><p>if (RFID.available() > 0) { // read tag numbers delay(100); // needed to allow time for the data to come in from the serial buffer.</p><p>for (int z = 0 ; z < 14 ; z++) // read the rest of the tag { data1 = RFID.read(); newtag[z] = data1; } RFID.flush(); // stops multiple reads</p><p>// do the tags match up? checkmytags(); } // now do something based on tag type if (ok > 0) // if we had a match { Serial.println("Accepted"); digitalWrite(yes, HIGH); delay(1000); digitalWrite(yes, LOW); long randNumber = random(15); tweet(Saying[randNumber]); delay(10000); ok = -1; } else if (ok == 0) // if we didn't have a match { Serial.println("Rejected"); digitalWrite(no, HIGH); delay(1000); digitalWrite(no, LOW);</p><p>ok = -1; } } void loop() { readTags(); }