Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BME280 ESP8266Interface IFTTT mbed
main.cpp@0:d4f636717e56, 2015-08-27 (annotated)
- Committer:
- MACRUM
- Date:
- Thu Aug 27 01:26:29 2015 +0000
- Revision:
- 0:d4f636717e56
Initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MACRUM | 0:d4f636717e56 | 1 | /* |
| MACRUM | 0:d4f636717e56 | 2 | * PackageLicenseDeclared: Apache-2.0 |
| MACRUM | 0:d4f636717e56 | 3 | * Copyright (c) Toyomasa Watarai |
| MACRUM | 0:d4f636717e56 | 4 | * |
| MACRUM | 0:d4f636717e56 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| MACRUM | 0:d4f636717e56 | 6 | * you may not use this file except in compliance with the License. |
| MACRUM | 0:d4f636717e56 | 7 | * You may obtain a copy of the License at |
| MACRUM | 0:d4f636717e56 | 8 | * |
| MACRUM | 0:d4f636717e56 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| MACRUM | 0:d4f636717e56 | 10 | * |
| MACRUM | 0:d4f636717e56 | 11 | * Unless required by applicable law or agreed to in writing, software |
| MACRUM | 0:d4f636717e56 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| MACRUM | 0:d4f636717e56 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| MACRUM | 0:d4f636717e56 | 14 | * See the License for the specific language governing permissions and |
| MACRUM | 0:d4f636717e56 | 15 | * limitations under the License. |
| MACRUM | 0:d4f636717e56 | 16 | */ |
| MACRUM | 0:d4f636717e56 | 17 | |
| MACRUM | 0:d4f636717e56 | 18 | #include "mbed.h" |
| MACRUM | 0:d4f636717e56 | 19 | #include "ESP8266Interface.h" |
| MACRUM | 0:d4f636717e56 | 20 | #include "TCPSocketConnection.h" |
| MACRUM | 0:d4f636717e56 | 21 | #include "ifttt.h" |
| MACRUM | 0:d4f636717e56 | 22 | #include "BME280.h" |
| MACRUM | 0:d4f636717e56 | 23 | |
| MACRUM | 0:d4f636717e56 | 24 | #define IFTTT_SECRET_KEY "YOUR_SECRET_KEY_OF_THE_MAKER_CHANNEL" // https://ifttt.com/maker |
| MACRUM | 0:d4f636717e56 | 25 | #define WIFI_SSID "SSID" |
| MACRUM | 0:d4f636717e56 | 26 | #define WIFI_PASSWORD "PASSWORD" |
| MACRUM | 0:d4f636717e56 | 27 | |
| MACRUM | 0:d4f636717e56 | 28 | ESP8266Interface wifi(dp9, dp8, dp7, WIFI_SSID, WIFI_PASSWORD); // TX, RX, Reset, SSID, Password |
| MACRUM | 0:d4f636717e56 | 29 | Serial pc(USBTX, USBRX); |
| MACRUM | 0:d4f636717e56 | 30 | BME280 sensor(I2C_SDA, I2C_SCL); |
| MACRUM | 0:d4f636717e56 | 31 | InterruptIn btn1(dp16); |
| MACRUM | 0:d4f636717e56 | 32 | InterruptIn btn2(dp15); |
| MACRUM | 0:d4f636717e56 | 33 | |
| MACRUM | 0:d4f636717e56 | 34 | static int has_event = 0; |
| MACRUM | 0:d4f636717e56 | 35 | |
| MACRUM | 0:d4f636717e56 | 36 | void btn1_pressed() |
| MACRUM | 0:d4f636717e56 | 37 | { |
| MACRUM | 0:d4f636717e56 | 38 | has_event = 1; |
| MACRUM | 0:d4f636717e56 | 39 | } |
| MACRUM | 0:d4f636717e56 | 40 | |
| MACRUM | 0:d4f636717e56 | 41 | void btn2_pressed() |
| MACRUM | 0:d4f636717e56 | 42 | { |
| MACRUM | 0:d4f636717e56 | 43 | has_event = 2; |
| MACRUM | 0:d4f636717e56 | 44 | } |
| MACRUM | 0:d4f636717e56 | 45 | |
| MACRUM | 0:d4f636717e56 | 46 | int main() |
| MACRUM | 0:d4f636717e56 | 47 | { |
| MACRUM | 0:d4f636717e56 | 48 | btn1.mode(PullUp); |
| MACRUM | 0:d4f636717e56 | 49 | btn2.mode(PullUp); |
| MACRUM | 0:d4f636717e56 | 50 | btn1.fall(&btn1_pressed); |
| MACRUM | 0:d4f636717e56 | 51 | btn2.fall(&btn2_pressed); |
| MACRUM | 0:d4f636717e56 | 52 | |
| MACRUM | 0:d4f636717e56 | 53 | wifi.init(); |
| MACRUM | 0:d4f636717e56 | 54 | wifi.connect(); //Use DHCP |
| MACRUM | 0:d4f636717e56 | 55 | pc.printf("\nIP Address is %s\n", wifi.getIPAddress()); |
| MACRUM | 0:d4f636717e56 | 56 | TCPSocketConnection socket; |
| MACRUM | 0:d4f636717e56 | 57 | |
| MACRUM | 0:d4f636717e56 | 58 | while(1) { |
| MACRUM | 0:d4f636717e56 | 59 | char v1[8], v2[8], v3[8]; |
| MACRUM | 0:d4f636717e56 | 60 | float f1, f2, f3; |
| MACRUM | 0:d4f636717e56 | 61 | f1 = sensor.getTemperature(); |
| MACRUM | 0:d4f636717e56 | 62 | f2 = sensor.getHumidity(); |
| MACRUM | 0:d4f636717e56 | 63 | f3 = sensor.getPressure(); |
| MACRUM | 0:d4f636717e56 | 64 | sprintf(v1, "%2.2f", f1); |
| MACRUM | 0:d4f636717e56 | 65 | sprintf(v2, "%2.2f", f2); |
| MACRUM | 0:d4f636717e56 | 66 | sprintf(v3, "%4.2f", f3); |
| MACRUM | 0:d4f636717e56 | 67 | pc.printf("%2.2f degC, %2.2f %%, %04.2f hPa, \n", f1, f2, f3); |
| MACRUM | 0:d4f636717e56 | 68 | |
| MACRUM | 0:d4f636717e56 | 69 | if (has_event == 1) { |
| MACRUM | 0:d4f636717e56 | 70 | IFTTT ifttt("BME280_iOS", IFTTT_SECRET_KEY, &socket); |
| MACRUM | 0:d4f636717e56 | 71 | ifttt.addIngredients(v1, v2, v3); |
| MACRUM | 0:d4f636717e56 | 72 | ifttt.trigger(IFTTT_POST); |
| MACRUM | 0:d4f636717e56 | 73 | has_event = 0; |
| MACRUM | 0:d4f636717e56 | 74 | } |
| MACRUM | 0:d4f636717e56 | 75 | if (has_event == 2) { |
| MACRUM | 0:d4f636717e56 | 76 | IFTTT ifttt("BME280_Android", IFTTT_SECRET_KEY, &socket); |
| MACRUM | 0:d4f636717e56 | 77 | ifttt.addIngredients(v1, v2, v3); |
| MACRUM | 0:d4f636717e56 | 78 | ifttt.trigger(IFTTT_POST); |
| MACRUM | 0:d4f636717e56 | 79 | has_event = 0; |
| MACRUM | 0:d4f636717e56 | 80 | } |
| MACRUM | 0:d4f636717e56 | 81 | wait(1); |
| MACRUM | 0:d4f636717e56 | 82 | } |
| MACRUM | 0:d4f636717e56 | 83 | } |