Permalink
Browse files

further changes as of chapter 8

  • Loading branch information...
1 parent ce47b9b commit f9fd4e7a5280ffff53eaf14a7ae57a95506dc6e0 @tigoe committed Apr 19, 2011
Showing with 3 additions and 175 deletions.
  1. +3 −1 README
  2. +0 −34 chapter4/general/DhcpExample/DhcpExample.pde
  3. +0 −140 chapter4/project7/AQIWebClient/AQIWebClient.pde
View
@@ -1,4 +1,6 @@
-These code samples, in Arduino, Processing, and PHP, are for my book "Making Things Talk", second edition. The book itself is copyrighted material, published under a Creative Commons CC-NC-ND license. As for the code:
+These code samples, in Arduino, Processing, and PHP, are for my book "Making Things Talk", second edition. Note that the Arduino examples are all written for the current experimental build of Arduino, so will not work in versions 22 or earlier.
+
+The book itself is copyrighted material, published under a Creative Commons CC-NC-ND license. As for the code:
This code is here to help you make things. In general, you may use this code in your programs and documentation. You do not need to contact me for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this repository does not require permission. Selling or distributing a CD-ROM of examples from this repository does require permission. Answering a question by citing this material and quoting example code does not require permission. Incorporating a significant amount of example code from this repository into your product’s documentation does require permission.
@@ -1,34 +0,0 @@
-/* DHCP
- Language: Arduino
-*/
-#include <SPI.h>
-#include <Ethernet.h>
-
-byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01};
-
-void setup() {
- // start the serial library:
- Serial.begin(9600);
- // start the Ethernet connection:
- Serial.println("Asking for an IP address using DHCP...");
- if (!Ethernet.begin(mac)) {
- Serial.println("Failed to configure Ethernet using DHCP");
- //You didn't get an address, so stop here:
- while(true);
- }
- else {
- // print the bytes of the IP address, separated by dots:
- Serial.print("I got an IP address. It's ");
- Serial.print(Ethernet.localIP()[0], DEC);
- Serial.print(".");
- Serial.print(Ethernet.localIP()[1], DEC);
- Serial.print(".");
- Serial.print(Ethernet.localIP()[2], DEC);
- Serial.print(".");
- Serial.println(Ethernet.localIP()[3], DEC);
- }
-}
-
-void loop() {
-}
-
@@ -1,140 +0,0 @@
-/*
- AirNow Web Scraper
- Language: Arduino
- */
-#include <SPI.h>
-#include <Ethernet.h>
-#include <TextFinder.h>
-
-const int connectedLED = 2; // indicates when there's a TCP connection
-const int successLED = 3; // indicates if the meter was set
-const int resetLED = 4; // indicates reset of Arduino
-const int disconnectedLED = 5; // indicates connection to server
-const int meterPin = 9; // controls VU meter
-const int meterMin = 0; // minimum level for the meter
-const int meterMax = 200; // maximum level for the meter
-const int AQIMax = 200; // maximum level for air quality
-const int requestInterval = 10000; // delay between updates to the server
-
-// Enter a MAC address and IP address for your controller below.
-// The IP address will be dependent on your local network:
-byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
-IPAddress ip(192,168,1,20);
-IPAddress server(208,201,239,101 );
-
-// Initialize the Ethernet client library
-// with the IP address and port of the server
-// that you want to connect to (port 80 is default for HTTP):
-Client client;
-
-boolean requested; // whether you've made a request since connecting
-long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
-int airQuality = 0; // AQI value
-boolean meterIsSet = false; // whether the meter is set from the latest reading
-
-void setup() {
- // start the Ethernet connection:
- Ethernet.begin(mac, ip);
- // start the serial library:
- Serial.begin(9600);
- // set all status LED pins:
- pinMode(connectedLED, OUTPUT);
- pinMode(successLED, OUTPUT);
- pinMode(resetLED, OUTPUT);
- pinMode(disconnectedLED, OUTPUT);
- pinMode(meterPin, OUTPUT);
-
- // give the Ethernet shield a second to initialize:
- delay(1000);
- // blink the reset LED:
- blink(resetLED, 3);
- // attempt to connect:
- connectToServer();
-}
-
-void loop()
-{
- // if you're connected, save any incoming bytes
- // to the input string:
- if (client.connected()) {
- if (!requested) {
- requested = makeRequest();
- }
- else {
- // make an instance of TextFinder to search the response:
- TextFinder response(client);
- // see if the response from the server contains the AQI value:
- if(response.find("Air Quality:")) {
- // convert the remaining part into an integer:
- airQuality = response.getValue();
- // set the meter:
- meterIsSet = setMeter(airQuality);
- }
- }
- }
- else if (millis() - lastAttemptTime > requestInterval) {
- // if you're not connected, and two minutes have passed since
- // your last connection, then attempt to connect again:
- client.stop();
- connectToServer();
- }
-
- // set the status LEDs:
- setLeds();
-}
-
-void connectToServer() {
- // clear the state of the meter:
- meterIsSet = false;
-
- // attempt to connect, and wait a millisecond:
- Serial.println("connecting...");
- if (client.connect(server, 80)) {
- requested = false;
- }
- // note the time of this connect attempt:
- lastAttemptTime = millis();
-}
-
-boolean makeRequest() {
- Serial.println("requesting...");
- // make HTTP GET request and fill in the path to
- // the PHP script on your server:
- client.println("GET /~myaccount/scraper.php HTTP/1.1");
- // fill in your server's name:
- client.println("HOST: example.com");
- client.println();
- return true;
-}
-
-boolean setMeter(int thisLevel) {
- Serial.println("setting meter...");
- boolean result = false;
- // map the result to a range the meter can use:
- int meterSetting = map(thisLevel, 0, AQIMax, meterMin, meterMax);
- // set the meter:
- analogWrite(meterPin, meterSetting);
- if (meterSetting > 0) {
- result = true;
- }
- return result;
-}
-
-void setLeds() {
- // connected LED and disconnected LED can just use
- // the client's connected() status:
- digitalWrite(connectedLED, client.connected());
- digitalWrite(disconnectedLED, !client.connected());
- // success LED depends on reading being successful:
- digitalWrite(successLED, meterIsSet);
-}
-
-void blink(int thisPin, int howManyTimes) {
- // Blink the reset LED:
- for (int blinks=0; blinks< howManyTimes; blinks++) {
- digitalWrite(thisPin, HIGH);
- delay(200);
- digitalWrite(thisPin, LOW);
- delay(200);
- }
-}

0 comments on commit f9fd4e7

Please sign in to comment.