Stap 5: Programma, het bord
Er zijn belangrijke dingen van de seveal te wijzigen in de onderstaande code. De eerste is het IP-adres. Elk bord moet een uniek IP-adres hebben anders die de router erg in de war krijgt. Ik begon bij IP-adres ip(192,168,1,178); en vervolgens toegevoegd aan het laatste nummer. Sommige routers hebben verschillende aantallen BV 192.168.2.x en een snelle controle op een PC met IPCONFIG in een DOS-shell zal de juiste eerste 3 nummers.
Het andere nummer te wijzigen is het MAC adresbereik. Het bereik byte mac [] {} =
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE}; is een standaardaantal – misschien verandering naar een ander hex nummer op elke bord, bijvoorbeeld de laatstgenoemde, graaf achteruit in hex 0xED, 0xEC.
Het laatste ding om te veranderen is of de Raad van bestuur een uploader of downloader. Deze code doet allebei en ongeveer halverwege naar beneden is dit
if(!client.Connected() & & (millis() - lastConnectionTime > postingInterval)) {}
sendData(Analog0,Analog1,Analog2,Analog3,Analog4);
commentaar uit beide verzenden of gegevens ophalen
getData();
dat is geconfigureerd om gegevens te verzenden. Om te fetchen terug die gegevens, senddata, commentaar en uncomment getdata.
Er is wat overgebleven code uitgecommentarieerd voor dingen zoals dumping uit de gehele tekenreeks van Xively die handig is voor foutopsporing om te trainen op de tekenreeks versneden en uitpakken van de individuele sensor lezingen.
Xively kunnen andere dingen doen zoals verzenden een SMS-bericht of ander bericht wanneer aan bepaalde voorwaarden is voldaan.
Veel plezier!
/* Pachube sensor client This sketch connects an analog sensor to Pachube (http://www.pachube.com) using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield, either one will work, as long as it's got a Wiznet Ethernet module on board. This example has been updated to use version 2.0 of the Pachube.com API. To make it work, create a feed with a datastream, and give it the ID sensor1. Or change the code below to match your feed. Circuit: * Analog sensor attached to analog in 0 for pushbuttons * Ethernet shield attached to pins 10, 11, 12, 13 The LCD circuit: standard is 12,11,5,4,3,2 change to 8,9,4,5,6,7 * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 and cut the header pin to D10 going to the LCD display (ethernet board needs this, and on the LCD display only used to turn backlight off Serial debug commented out now and send to LCD instead Change IP address to a different number for each board created 15 March 2010 modified 9 Apr 2012 by Tom Igoe with input from Usman Haque and Joe Saavedra http://arduino.cc/en/Tutorial/PachubeClient This code is in the public domain. */#include <SPI.h>#include <Ethernet.h>#include <LiquidCrystal.h>#define APIKEY "shsCNFtxuGELLZx8ehqglXAgDo9lkyBam5Zj22p3g3urH2FM" // replace your pachube api key here#define FEEDID 970253233 // replace your feed ID#define USERAGENT "Arduino1" // user agent is the project name// assign a MAC address for the ethernet controller.// Newer Ethernet shields have a MAC address printed on a sticker on the shield// fill in your address here:byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE};// fill in an available IP address on your network here,// for manual configuration:IPAddress ip(192,168,1,178);// initialize the library instance:EthernetClient client;// initialize the lcd library with the numbers of the interface pinsLiquidCrystal lcd(8,9,4,5,6,7);// if you don't want to use DNS (and reduce your sketch size)// use the numeric IP instead of the name for the server://IPAddress server(216,52,233,122); // numeric IP for api.pachube.comchar server[] = "api.xively.com"; // name address for xively APIunsigned long lastConnectionTime = 0; // last time you connected to the server, in millisecondsboolean lastConnected = false; // state of the connection last time through the main loopconst unsigned long postingInterval = 10*1000; //delay between updates to Pachube.comint counter;String stringOne,stringTwo; // built string when data comes backboolean stringData = false; // reset when a new block of data comes backvoid setup() { //delay(1000); // in case the serial port causes a latchup // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Serial.println("api.xively.com"); // if go more than ?32s then integer calc doesn't work lcd.begin(16, 2); Cls(); // clear the screen delay(1000); lcd.setCursor(0,1); // x,y top left corner is 0,0 lcd.print("api.xively.com"); lcd.setCursor(0,0); lcd.print("Start Ethernet"); ////Serial.println("Start Ethernet"); delay(1000); // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { //Serial.println("Failed to configure Ethernet using DHCP"); // DHCP failed, so use a fixed IP address: //lcd.setCursor(0,1); //lcd.print("Failed to configure"); Ethernet.begin(mac, ip); } Serial.println("Wait 10s"); lcd.setCursor(0,1); lcd.print("Wait 10s "); }void loop() { // read the analog sensor: int Analog0 = analogRead(A0); // with a LCD display analog0 is all the buttons int Analog1 = analogRead(A1); int Analog2 = analogRead(A2); int Analog3 = analogRead(A3); int Analog4 = analogRead(A4); // int sensorReading = analogRead(A2); // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: if (client.available()) { char c = client.read(); Serial.print(c); if (stringData == false) { stringData = true; // some data has come in } if (stringData == true) { stringOne += c; // build the string } if ((c>32) and (c<127)) { lcd.print(c); counter +=1; } if (counter==16) { lcd.setCursor(0,1); //lcd.print(" "); //lcd.setCursor(0,1); counter = 0; //delay(100); } } // if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { //Serial.println(); //Serial.println("Disconnect"); client.stop(); lcd.setCursor(0,0); lcd.print("Disconnect "); lcd.setCursor(0,1); counter = 0; if (stringData == true) { PrintResults(); // extract the values and print out stringData = false; // reset the flag stringOne = ""; // clear the string } } // if you're not connected, and ten seconds have passed since // your last connection, then connect again and send data: if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { //sendData(Analog0,Analog1,Analog2,Analog3,Analog4);// comment out either send or get data getData(); } // store the state of the connection for next time through // the loop: lastConnected = client.connected();}void PrintResults() // print results of the GET from xively{ int n = 292; // start at the sensor data int i; char lf = 10; int v; Cls(); lcd.setCursor(0,0); stringOne += lf; // add an end of line character for(i=0;i<5;i++) { while (stringOne.charAt(n) != 44) // find first comma { n +=1; } n += 1; while (stringOne.charAt(n) != 44) // find second comma { n+=1 ; } n+=1; stringTwo = ""; while (stringOne.charAt(n) != 10) // find the end of the line which is a line feed ascii 10 { //lcd.print(stringOne.charAt(n)); stringTwo+=stringOne.charAt(n); n+=1; } v=stringTwo.toInt(); lcd.print(v); lcd.print(" "); // space at end if (i==1) { lcd.setCursor(0,1); } } }void Cls() // clear LCD screen{ lcd.setCursor(0,0); lcd.print(" "); // clear lcd screen lcd.setCursor(0,1); lcd.print(" ");} void PrintValues(int n0,int n1,int n2, int n3, int n4){ //Serial.print(n0); //Serial.print(" "); //Serial.print(n1); //Serial.print(" "); //Serial.print(n2); //Serial.print(" "); //Serial.print(n3); //Serial.print(" "); //Serial.println(n4); Cls(); lcd.setCursor(0,0); lcd.print(n0); lcd.print(" "); lcd.print(n1); lcd.setCursor(0,1); lcd.print(n2); lcd.print(" "); lcd.print(n3); lcd.print(" "); lcd.print(n4); delay(2000);} // this method makes a HTTP connection to the server:void sendData(int data0,int data1,int data2,int data3, int data4) { PrintValues(data0,data1,data2,data3,data4); //Serial.println("Connecting..."); lcd.setCursor(0,0); lcd.print("Connecting... "); lcd.setCursor(0,1); lcd.print("No reply "); // if there is a reply this will very quickly get overwritten lcd.setCursor(0,1); counter = 0; // if there's a successful connection: if (client.connect(server, 80)) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.pachube.com"); client.print("X-PachubeApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); // calculate the length of the sensor reading in bytes: // 8 bytes for "sensor1," + number of digits of the data: //int thisLength = 8 + getLength(thisData); //client.println(thisLength); // 8 is length of sensor1 and 2 more for crlf int stringLength = 8 + getLength(data0) + 10 + getLength(data1) + 10 + getLength(data2) + 10 + getLength(data3) + 10 + getLength(data4); client.println(stringLength); // last pieces of the HTTP PUT request: client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println(); // here's the actual content of the PUT request: client.print("sensor1,"); client.println(data0); client.print("sensor2,"); client.println(data1); client.print("sensor3,"); client.println(data2); client.print("sensor4,"); client.println(data3); client.print("sensor5,"); client.println(data4); //Serial.println("Wait for reply"); // xively responds with some text, if nothing then there is an error lcd.setCursor(0,1); lcd.print("Wait for reply "); } else { // if you couldn't make a connection: //Serial.println("connection failed"); //Serial.println(); //Serial.println("so disconnecting."); client.stop(); //lcd.setCursor(0,1); //lcd.print("Connect Fail"); } // note the time that the connection was made or attempted: lastConnectionTime = millis();}// this method makes a HTTP connection to the server:void getData() { // if there's a successful connection: if (client.connect(server, 80)) { //Serial.println("connecting to request data..."); lcd.setCursor(0,0); lcd.print("Connect "); client.print("GET /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); client.println("Host: api.pachube.com"); client.print("X-PachubeApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.println("Content-Type: text/csv"); client.println("Connection: close"); client.println(); //Serial.println("Finished requesting, wait for response."); lcd.setCursor(0,1); lcd.print("Finish request"); } else { // if you couldn't make a connection: //Serial.println("connection failed"); //Serial.println(); //Serial.println("so disconnecting."); client.stop(); } // note the time that the connection was made or attempted: lastConnectionTime = millis();}// This method calculates the number of digits in the// sensor reading. Since each digit of the ASCII decimal// representation is a byte, the number of digits equals// the number of bytes:int getLength(int someValue) { // there's at least one byte: int digits = 1; // continually divide the value by ten, // adding one to the digit count for each // time you divide, until you're at 0: int dividend = someValue /10; while (dividend > 0) { dividend = dividend /10; digits++; } // return the number of digits: return digits;}