Attic fan controller
Dependencies: Chainable_RGB_LED DHT ESP8266Interface M2XStreamClient jsonlite mbed
main.cpp@0:4c3752fbe0a6, 2016-01-03 (annotated)
- Committer:
- cantbewong
- Date:
- Sun Jan 03 19:03:51 2016 +0000
- Revision:
- 0:4c3752fbe0a6
initial draft
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cantbewong | 0:4c3752fbe0a6 | 1 | #include "mbed.h" |
cantbewong | 0:4c3752fbe0a6 | 2 | #include "ChainableLED.h" |
cantbewong | 0:4c3752fbe0a6 | 3 | #include "M2XStreamClient.h" |
cantbewong | 0:4c3752fbe0a6 | 4 | #include "ESP8266Interface.h" |
cantbewong | 0:4c3752fbe0a6 | 5 | #include "TCPSocketConnection.h" |
cantbewong | 0:4c3752fbe0a6 | 6 | |
cantbewong | 0:4c3752fbe0a6 | 7 | DigitalOut myled(LED1); |
cantbewong | 0:4c3752fbe0a6 | 8 | // ChainableLED(clk, data, number_of_leds) |
cantbewong | 0:4c3752fbe0a6 | 9 | ChainableLED color_led(D4, D5, 1); |
cantbewong | 0:4c3752fbe0a6 | 10 | #include "DHT.h" |
cantbewong | 0:4c3752fbe0a6 | 11 | |
cantbewong | 0:4c3752fbe0a6 | 12 | DHT sensor(D3, DHT11); |
cantbewong | 0:4c3752fbe0a6 | 13 | DigitalOut relay(D6); |
cantbewong | 0:4c3752fbe0a6 | 14 | |
cantbewong | 0:4c3752fbe0a6 | 15 | /* |
cantbewong | 0:4c3752fbe0a6 | 16 | * ESP8266 Wifi Config for nucleo 411 |
cantbewong | 0:4c3752fbe0a6 | 17 | */ |
cantbewong | 0:4c3752fbe0a6 | 18 | ESP8266Interface wifi(D8,D2,D3,"SouthHackUp","",115200); // TX,RX,Reset,SSID,Password,Baud |
cantbewong | 0:4c3752fbe0a6 | 19 | |
cantbewong | 0:4c3752fbe0a6 | 20 | // |
cantbewong | 0:4c3752fbe0a6 | 21 | // Fill these field in from you ATT M2X Account |
cantbewong | 0:4c3752fbe0a6 | 22 | // |
cantbewong | 0:4c3752fbe0a6 | 23 | char deviceId[] = "<deviceID>"; // Device you want to push to |
cantbewong | 0:4c3752fbe0a6 | 24 | char streamName[] = "<streamID>"; // Stream you want to push to |
cantbewong | 0:4c3752fbe0a6 | 25 | char m2xKey[] = "<deviceAPIKey>"; // Your M2X API Key or Master API Key |
cantbewong | 0:4c3752fbe0a6 | 26 | |
cantbewong | 0:4c3752fbe0a6 | 27 | |
cantbewong | 0:4c3752fbe0a6 | 28 | int main() { |
cantbewong | 0:4c3752fbe0a6 | 29 | // dht related |
cantbewong | 0:4c3752fbe0a6 | 30 | int error = 0; |
cantbewong | 0:4c3752fbe0a6 | 31 | float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
cantbewong | 0:4c3752fbe0a6 | 32 | |
cantbewong | 0:4c3752fbe0a6 | 33 | |
cantbewong | 0:4c3752fbe0a6 | 34 | printf("Starting...\r\n"); |
cantbewong | 0:4c3752fbe0a6 | 35 | |
cantbewong | 0:4c3752fbe0a6 | 36 | // connect to wifi |
cantbewong | 0:4c3752fbe0a6 | 37 | wifi.init(); //Reset |
cantbewong | 0:4c3752fbe0a6 | 38 | wifi.connect(); //Use DHCP |
cantbewong | 0:4c3752fbe0a6 | 39 | printf("IP Address is %s \n\r", wifi.getIPAddress()); |
cantbewong | 0:4c3752fbe0a6 | 40 | |
cantbewong | 0:4c3752fbe0a6 | 41 | while(1) { |
cantbewong | 0:4c3752fbe0a6 | 42 | // generate random RGB values for LED's |
cantbewong | 0:4c3752fbe0a6 | 43 | uint8_t R = rand(); |
cantbewong | 0:4c3752fbe0a6 | 44 | uint8_t G = rand(); |
cantbewong | 0:4c3752fbe0a6 | 45 | uint8_t B = rand(); |
cantbewong | 0:4c3752fbe0a6 | 46 | // print message to terminal |
cantbewong | 0:4c3752fbe0a6 | 47 | //printf("R=%x,G=%x,B=%x,",R,G,B); |
cantbewong | 0:4c3752fbe0a6 | 48 | |
cantbewong | 0:4c3752fbe0a6 | 49 | // ChainableLED.setColorRGB(index_of_led, red, green, blue) |
cantbewong | 0:4c3752fbe0a6 | 50 | color_led.setColorRGB(0, R, G, B); |
cantbewong | 0:4c3752fbe0a6 | 51 | myled = 1; // LED is ON |
cantbewong | 0:4c3752fbe0a6 | 52 | relay = 1; |
cantbewong | 0:4c3752fbe0a6 | 53 | |
cantbewong | 0:4c3752fbe0a6 | 54 | wait(1.0); // 200 ms |
cantbewong | 0:4c3752fbe0a6 | 55 | |
cantbewong | 0:4c3752fbe0a6 | 56 | |
cantbewong | 0:4c3752fbe0a6 | 57 | myled = 0; // LED is OFF |
cantbewong | 0:4c3752fbe0a6 | 58 | relay = 0; |
cantbewong | 0:4c3752fbe0a6 | 59 | error = sensor.readData(); |
cantbewong | 0:4c3752fbe0a6 | 60 | if (0 == error) { |
cantbewong | 0:4c3752fbe0a6 | 61 | c = sensor.ReadTemperature(CELCIUS); |
cantbewong | 0:4c3752fbe0a6 | 62 | f = sensor.ReadTemperature(FARENHEIT); |
cantbewong | 0:4c3752fbe0a6 | 63 | k = sensor.ReadTemperature(KELVIN); |
cantbewong | 0:4c3752fbe0a6 | 64 | h = sensor.ReadHumidity(); |
cantbewong | 0:4c3752fbe0a6 | 65 | dp = sensor.CalcdewPoint(c, h); |
cantbewong | 0:4c3752fbe0a6 | 66 | dpf = sensor.CalcdewPointFast(c, h); |
cantbewong | 0:4c3752fbe0a6 | 67 | printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\r\n", k, c, f); |
cantbewong | 0:4c3752fbe0a6 | 68 | printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\r\n", h, dp, dpf); |
cantbewong | 0:4c3752fbe0a6 | 69 | } else { |
cantbewong | 0:4c3752fbe0a6 | 70 | printf("Error: %d\n", error); |
cantbewong | 0:4c3752fbe0a6 | 71 | } |
cantbewong | 0:4c3752fbe0a6 | 72 | wait(1.0); // 1 sec |
cantbewong | 0:4c3752fbe0a6 | 73 | } |
cantbewong | 0:4c3752fbe0a6 | 74 | } |