From e571b9a04437648fe2b746d46abadb4628737bf9 Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Thu, 8 May 2014 19:21:00 +0200 Subject: [PATCH] added chapter5 --- chapter5/.DS_Store | Bin 0 -> 15364 bytes chapter5/README.md | 7 ++ chapter5/energy_xively/energy_xively.ino | 142 +++++++++++++++++++++++ chapter5/sensor_test/sensor_test.ino | 64 ++++++++++ 4 files changed, 213 insertions(+) create mode 100644 chapter5/.DS_Store create mode 100644 chapter5/README.md create mode 100644 chapter5/energy_xively/energy_xively.ino create mode 100644 chapter5/sensor_test/sensor_test.ino diff --git a/chapter5/.DS_Store b/chapter5/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e7508b58169c1a86958fe321018bcbb36abb6699 GIT binary patch literal 15364 zcmeHMYit!o6h7xtU>`uY&_W9cmsderpif#V(%aG^2uQUp z#)OA}_OCMK>Q4E<$cFxY3 zIeT}%{pNf#djS9|+su4`AOI+IFl&sUYJkG}+0m~Ae2-2RDIQ=8Y=lIMIs;(kO=fffaQlE9n6M&+a)Yd9aQlx0NE6B^Frse z58yFD--dkKB`go6Yl__i`l0A=F;Ik4e~=9)8}eQ(G*O zuqtA)T6>4(H8i=gwid@*ld!fWm}i~m#uJOVbB#^0%ojF{HqW}lqCmUds-keuKvg+D za_of3({G$xSbFotQK?4S=rNkMj&ODo_Ga6P*LN_m*G9Z1%Q0iT-tvp8;1Z7K>62u{(pI*)G4i1-6%WNTX8 zR(jor=U;Soj;7bgB2g=5n5qbym#66s?E)KPx$i70*7P;>Hd^SWy)?}A-3!WeeIxtj z6%jA7s?~DnuHK=aUsN6%rDy7kyC0?RBPz;T7FA)ImN_jnOT6M~p*(S-k}PGxToR!= zXrw99N*}oP!2x&(4#FWg3a8*SoPoFDU3d@PhY#Q*_y)d(@8Em*2>}rUsH1_SaSW#8 z4LB92;|$Ekd031kSc(g<5^u#7xDs!}2Hb>AXrhGRX4d!#6NzI)I%`{B8g$dc7%nKqMiXmwL*Xo9%^BI$3=q{7ey!O# zx@HXFgl4^rtHw}HUDhO)YDNktH)}2^quybh!>qZeoO(e_DArc9bxl#Pj($hrB)rC{ zcOE{3&)`e=0e&IU4I{#3;&`IlB%F-1a5m=R9GpvpTYxv=LcAH5;8LR9aw1+m(QYlS z!}UbG&De}K-iaQzVFzx*?RY;vfV*%v?jagJijU#5_!@_xA)86rWe(2gnWYRvxXc%`dfbR%jFOzi zD3`SJT(TP<#;5T)Jch^d1fIs%@eJoABo(v4Nts;8mZWRd*Orw3jh^WfwMm0fUFaNV zl`)(vmQIXibTWj1>Dl@$g&1!Hc(0bD&tZHvhKRwOr>1HQ6}`kz#%&`-^pusdeBD%ow_`0f;s&DJCL-NdBAtUS-i14`lU7pq&`N3#t)%wj z0X#?)d=&TUE(U2Eijh z{vaKj=wOA7>k^iSP{oEYKmQz04fZ*ne@r2t|1E|0hx~C}!t!v6 +#include + +// Define CC3000 chip pins +#define ADAFRUIT_CC3000_IRQ 3 +#define ADAFRUIT_CC3000_VBAT 5 +#define ADAFRUIT_CC3000_CS 10 + +// Define current sensor pin +#define CURRENT_SENSOR A0 + +// Define measurement variables +float amplitude_current; +float effective_value; +float effective_voltage = 230; // Set voltage to 230V (Europe) or 110V (US) +float effective_power; +float zero_sensor; + +// Create CC3000 instances +Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, + SPI_CLOCK_DIV2); // you can change this clock speed + +// WLAN parameters +#define WLAN_SSID "yourSSID" +#define WLAN_PASS "yourPassword" +// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2 +#define WLAN_SECURITY WLAN_SEC_WPA2 + +// Xively parameters +#define WEBSITE "api.xively.com" +#define API_key "yourAPIKey" +#define feedID "yourFeedID" + +uint32_t ip; + +void setup(void) +{ + // Initialize Serial + Serial.begin(115200); + + // Calibrate sensor with null current + zero_sensor = getSensorValue(); + Serial.print("Zero point sensor: "); + Serial.println(zero_sensor); + Serial.println(""); + + // Initialize CC3000 chip + Serial.println(F("\nInitializing...")); + if (!cc3000.begin()) + { + Serial.println(F("Couldn't begin()! Check your wiring?")); + while(1); + } + +} + +void loop(void) +{ + // Connect to WiFi network + cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); + Serial.println(F("Connected!")); + + // Wait for DHCP to complete + Serial.println(F("Request DHCP")); + while (!cc3000.checkDHCP()) + { + delay(100); + } + Serial.println("DHCP OK"); + + // Set the website IP + uint32_t ip = cc3000.IP2U32(216,52,233,120); + cc3000.printIPdotsRev(ip); + + // Perform power measurement + float sensor_value = getSensorValue(); + Serial.print("Sensor value: "); + Serial.println(sensor_value); + + // Convert to current + amplitude_current=(float)(sensor_value-zero_sensor)/1024*5/185*1000000; + effective_value=amplitude_current/1.414; + effective_power = abs(effective_value*effective_voltage/1000); + + // Prepare JSON for Xively & get length + int length = 0; + String data = ""; + data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Current\",\"current_value\" : \"" + String((int)effective_value) + "\"}," + "{\"id\" : \"Power\",\"current_value\" : \"" + String((int)effective_power) + "\"}]}"; + Serial.println(data); + length = data.length(); + + // Send request + Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80); + if (client.connected()) { + Serial.println("Connected!"); + client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.1"); + client.println("Host: api.xively.com"); + client.println("X-ApiKey: " + String(API_key)); + client.println("Content-Length: " + String(length)); + client.print("Connection: close"); + client.println(); + client.print(data); + client.println(); + } else { + Serial.println(F("Connection failed")); + return; + } + + Serial.println(F("-------------------------------------")); + while (client.connected()) { + while (client.available()) { + char c = client.read(); + Serial.print(c); + } + } + client.close(); + Serial.println(F("-------------------------------------")); + + Serial.println(F("\n\nDisconnecting")); + cc3000.disconnect(); + + // Wait 10 seconds until next update + delay(10000); + +} + +// Get the reading from the current sensor +float getSensorValue() +{ + int sensorValue; + float avgSensor = 0; + int nb_measurements = 100; + for (int i = 0; i < nb_measurements; i++) { + sensorValue = analogRead(CURRENT_SENSOR); + avgSensor = avgSensor + float(sensorValue); + } + avgSensor = avgSensor/float(nb_measurements); + return avgSensor; +} diff --git a/chapter5/sensor_test/sensor_test.ino b/chapter5/sensor_test/sensor_test.ino new file mode 100644 index 0000000..4ca2d74 --- /dev/null +++ b/chapter5/sensor_test/sensor_test.ino @@ -0,0 +1,64 @@ +// Sketch to test the current sensor + +// Define current sensor pin +#define CURRENT_SENSOR A0 + +// Define measurement variables +float amplitude_current; +float effective_value; +float effective_voltage = 230; // Set voltage to 230V (Europe) or 110V (US) +float effective_power; +float zero_sensor; + +void setup(void) +{ + // Init serial + Serial.begin(115200); + + // Calibrate sensor with null current + zero_sensor = getSensorValue(); + Serial.print("Zero point sensor: "); + Serial.println(zero_sensor); + Serial.println(""); + +} + +void loop(void) +{ + + // Perform power measurement + float sensor_value = getSensorValue(); + Serial.print("Sensor value: "); + Serial.println(sensor_value); + + // Convert to current + amplitude_current=(float)(sensor_value-zero_sensor)/1024*5/185*1000000; + effective_value=amplitude_current/1.414; + + // Plot data + Serial.println("Current amplitude (in mA): "); + Serial.println(amplitude_current,1); + Serial.println("Current effective value (in mA)"); + Serial.println(effective_value,1); + Serial.println("Effective power (in W): "); + Serial.println(abs(effective_value*effective_voltage/1000),1); + Serial.println(""); + + // Poll every 50ms + delay(500); + +} + +// Get the reading from the current sensor +float getSensorValue() +{ + int sensorValue; + float avgSensor = 0; + int nb_measurements = 100; + for (int i = 0; i < nb_measurements; i++) { + sensorValue = analogRead(CURRENT_SENSOR); + avgSensor = avgSensor + float(sensorValue); + } + avgSensor = avgSensor/float(nb_measurements); + return avgSensor; +}