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: X_NUCLEO_COMMON ST_INTERFACES
main.cpp@35:871447da961d, 2020-11-11 (annotated)
- Committer:
- saileshtimilsena
- Date:
- Wed Nov 11 17:32:22 2020 +0000
- Revision:
- 35:871447da961d
- Parent:
- 34:a5724eeaaf9d
- Child:
- 37:b7acef59086c
update the code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adustm | 1:e86b1cffc402 | 1 | // ---------------------------------------------------------------------------- |
adustm | 4:cf7342047b4d | 2 | // Copyright 2016-2018 ARM Ltd. |
adustm | 1:e86b1cffc402 | 3 | // |
adustm | 1:e86b1cffc402 | 4 | // SPDX-License-Identifier: Apache-2.0 |
adustm | 1:e86b1cffc402 | 5 | // |
adustm | 1:e86b1cffc402 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
adustm | 1:e86b1cffc402 | 7 | // you may not use this file except in compliance with the License. |
adustm | 1:e86b1cffc402 | 8 | // You may obtain a copy of the License at |
adustm | 1:e86b1cffc402 | 9 | // |
adustm | 1:e86b1cffc402 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
adustm | 1:e86b1cffc402 | 11 | // |
adustm | 1:e86b1cffc402 | 12 | // Unless required by applicable law or agreed to in writing, software |
adustm | 1:e86b1cffc402 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
adustm | 1:e86b1cffc402 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
adustm | 1:e86b1cffc402 | 15 | // See the License for the specific language governing permissions and |
adustm | 1:e86b1cffc402 | 16 | // limitations under the License. |
adustm | 1:e86b1cffc402 | 17 | // ---------------------------------------------------------------------------- |
MarceloSalazar | 9:265744785d33 | 18 | #ifndef MBED_TEST_MODE |
screamer | 28:0e774865873d | 19 | |
adustm | 1:e86b1cffc402 | 20 | #include "mbed.h" |
adustm | 1:e86b1cffc402 | 21 | #include "simple-mbed-cloud-client.h" |
screamer | 10:b27c962b3c3f | 22 | #include "LittleFileSystem.h" |
screamer | 33:cfd9430e7d1e | 23 | |
saileshtimilsena | 35:871447da961d | 24 | #include "stdbool.h" //for stepper motor function |
saileshtimilsena | 35:871447da961d | 25 | |
saileshtimilsena | 35:871447da961d | 26 | #define STEPPER_PIN_1 PA_15 //ARD.D9-PWM PA15 |
saileshtimilsena | 35:871447da961d | 27 | #define STEPPER_PIN_2 PA_2 //ARD.D10-PWM PA2 |
saileshtimilsena | 35:871447da961d | 28 | #define STEPPER_PIN_3 PA_7 //ARD.D11-PWM PA7 |
saileshtimilsena | 35:871447da961d | 29 | #define STEPPER_PIN_4 PA_6 //ARD.D12-PWM PA6 |
saileshtimilsena | 35:871447da961d | 30 | |
saileshtimilsena | 35:871447da961d | 31 | DigitalOut steeperOne(STEPPER_PIN_1); |
saileshtimilsena | 35:871447da961d | 32 | DigitalOut steeperTwo(STEPPER_PIN_2); |
saileshtimilsena | 35:871447da961d | 33 | DigitalOut steeperThree(STEPPER_PIN_3); |
saileshtimilsena | 35:871447da961d | 34 | DigitalOut steeperFour(STEPPER_PIN_4); |
saileshtimilsena | 35:871447da961d | 35 | |
saileshtimilsena | 35:871447da961d | 36 | DigitalOut myIOs(PB_14); // review the results on PB_14 |
saileshtimilsena | 35:871447da961d | 37 | DigitalOut myIOs2(PA_5); // review the results on PA_5 |
saileshtimilsena | 35:871447da961d | 38 | int step_number=0; |
saileshtimilsena | 35:871447da961d | 39 | int open_flag=0; |
saileshtimilsena | 35:871447da961d | 40 | int close_flag=0; |
saileshtimilsena | 35:871447da961d | 41 | |
screamer | 33:cfd9430e7d1e | 42 | // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi. |
screamer | 33:cfd9430e7d1e | 43 | NetworkInterface *net; |
screamer | 33:cfd9430e7d1e | 44 | |
screamer | 33:cfd9430e7d1e | 45 | // Default block device available on the target board |
screamer | 33:cfd9430e7d1e | 46 | BlockDevice* bd = BlockDevice::get_default_instance(); |
screamer | 33:cfd9430e7d1e | 47 | SlicingBlockDevice sd(bd, 0, 2*1024*1024); |
screamer | 33:cfd9430e7d1e | 48 | |
screamer | 33:cfd9430e7d1e | 49 | #if COMPONENT_SD || COMPONENT_NUSD |
screamer | 33:cfd9430e7d1e | 50 | // Use FATFileSystem for SD card type blockdevices |
screamer | 33:cfd9430e7d1e | 51 | FATFileSystem fs("fs"); |
screamer | 33:cfd9430e7d1e | 52 | #else |
screamer | 33:cfd9430e7d1e | 53 | // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions |
screamer | 33:cfd9430e7d1e | 54 | LittleFileSystem fs("fs"); |
screamer | 33:cfd9430e7d1e | 55 | #endif |
screamer | 33:cfd9430e7d1e | 56 | |
screamer | 33:cfd9430e7d1e | 57 | // Default User button for GET example and for resetting the storage |
screamer | 33:cfd9430e7d1e | 58 | InterruptIn button(BUTTON1); |
screamer | 33:cfd9430e7d1e | 59 | // Default LED to use for PUT/POST example |
screamer | 33:cfd9430e7d1e | 60 | DigitalOut led(LED1, 1); |
screamer | 33:cfd9430e7d1e | 61 | |
screamer | 33:cfd9430e7d1e | 62 | // How often to fetch sensor data (in seconds) |
screamer | 33:cfd9430e7d1e | 63 | #define SENSORS_POLL_INTERVAL 3.0 |
screamer | 33:cfd9430e7d1e | 64 | |
screamer | 33:cfd9430e7d1e | 65 | // Send all sensor data or just limited (useful for when running out of memory) |
screamer | 33:cfd9430e7d1e | 66 | #define SEND_ALL_SENSORS |
screamer | 33:cfd9430e7d1e | 67 | |
screamer | 33:cfd9430e7d1e | 68 | // Sensors related includes and initialization |
saileshtimilsena | 35:871447da961d | 69 | #include "HTS221Sensor.h" //humidity and temperature |
saileshtimilsena | 35:871447da961d | 70 | #include "LPS22HBSensor.h" //Barometer |
screamer | 10:b27c962b3c3f | 71 | |
screamer | 10:b27c962b3c3f | 72 | static DevI2C devI2c(PB_11,PB_10); |
screamer | 12:1f1a50e973db | 73 | static HTS221Sensor sen_hum_temp(&devI2c); |
screamer | 12:1f1a50e973db | 74 | static LPS22HBSensor sen_press_temp(&devI2c); |
screamer | 28:0e774865873d | 75 | static DigitalOut shutdown_pin(PC_6); |
saileshtimilsena | 35:871447da961d | 76 | //static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7); |
saileshtimilsena | 35:871447da961d | 77 | |
screamer | 33:cfd9430e7d1e | 78 | // Temperature reading from microcontroller |
screamer | 33:cfd9430e7d1e | 79 | AnalogIn adc_temp(ADC_TEMP); |
screamer | 33:cfd9430e7d1e | 80 | // Voltage reference reading from microcontroller |
screamer | 33:cfd9430e7d1e | 81 | AnalogIn adc_vref(ADC_VREF); |
screamer | 11:8df4529f060d | 82 | |
MarceloSalazar | 9:265744785d33 | 83 | // Declaring pointers for access to Pelion Client resources outside of main() |
screamer | 12:1f1a50e973db | 84 | MbedCloudClientResource *res_button; |
screamer | 12:1f1a50e973db | 85 | MbedCloudClientResource *res_led; |
adustm | 1:e86b1cffc402 | 86 | |
screamer | 33:cfd9430e7d1e | 87 | // Additional resources for sensor readings |
screamer | 28:0e774865873d | 88 | #ifdef SEND_ALL_SENSORS |
screamer | 12:1f1a50e973db | 89 | MbedCloudClientResource *res_humidity; |
screamer | 12:1f1a50e973db | 90 | MbedCloudClientResource *res_pressure; |
screamer | 28:0e774865873d | 91 | MbedCloudClientResource *res_adc_temp; |
screamer | 13:42b49a0caade | 92 | #endif /* SEND_ALL_SENSORS */ |
adustm | 1:e86b1cffc402 | 93 | |
screamer | 33:cfd9430e7d1e | 94 | // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) |
screamer | 33:cfd9430e7d1e | 95 | // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed |
screamer | 33:cfd9430e7d1e | 96 | EventQueue eventQueue; |
screamer | 33:cfd9430e7d1e | 97 | |
screamer | 10:b27c962b3c3f | 98 | // When the device is registered, this variable will be used to access various useful information, like device ID etc. |
screamer | 10:b27c962b3c3f | 99 | static const ConnectorClientEndpointInfo* endpointInfo; |
adustm | 1:e86b1cffc402 | 100 | |
screamer | 10:b27c962b3c3f | 101 | /** |
adustm | 4:cf7342047b4d | 102 | * PUT handler |
adustm | 4:cf7342047b4d | 103 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 104 | * @param newValue Updated value for the resource |
adustm | 4:cf7342047b4d | 105 | */ |
screamer | 32:2871fbeb627d | 106 | void put_callback(MbedCloudClientResource *resource, m2m::String newValue) { |
screamer | 29:6ff737b67e7d | 107 | printf("*** PUT received, new value: %s \n", newValue.c_str()); |
screamer | 11:8df4529f060d | 108 | led = atoi(newValue.c_str()); |
adustm | 1:e86b1cffc402 | 109 | } |
adustm | 1:e86b1cffc402 | 110 | |
adustm | 4:cf7342047b4d | 111 | /** |
adustm | 4:cf7342047b4d | 112 | * POST handler |
adustm | 4:cf7342047b4d | 113 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 114 | * @param buffer If a body was passed to the POST function, this contains the data. |
adustm | 4:cf7342047b4d | 115 | * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer. |
adustm | 4:cf7342047b4d | 116 | * @param size Size of the body |
adustm | 4:cf7342047b4d | 117 | */ |
screamer | 32:2871fbeb627d | 118 | void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) { |
screamer | 32:2871fbeb627d | 119 | printf("*** POST received (length %u). Payload: ", size); |
screamer | 32:2871fbeb627d | 120 | for (size_t ix = 0; ix < size; ix++) { |
screamer | 32:2871fbeb627d | 121 | printf("%02x ", buffer[ix]); |
screamer | 32:2871fbeb627d | 122 | } |
screamer | 32:2871fbeb627d | 123 | printf("\n"); |
screamer | 11:8df4529f060d | 124 | } |
saileshtimilsena | 35:871447da961d | 125 | //Serial port define for uart1 usb |
saileshtimilsena | 35:871447da961d | 126 | //pb7 rx |
saileshtimilsena | 35:871447da961d | 127 | //pb6 tx |
saileshtimilsena | 35:871447da961d | 128 | //Serial device(USBTX, USBRX); |
saileshtimilsena | 35:871447da961d | 129 | Serial device(PB_6, PB_7); // tx, rx |
saileshtimilsena | 35:871447da961d | 130 | |
adustm | 1:e86b1cffc402 | 131 | |
screamer | 11:8df4529f060d | 132 | /** |
screamer | 13:42b49a0caade | 133 | * Button function triggered by the physical button press. |
screamer | 11:8df4529f060d | 134 | */ |
saileshtimilsena | 35:871447da961d | 135 | |
saileshtimilsena | 35:871447da961d | 136 | |
saileshtimilsena | 35:871447da961d | 137 | |
saileshtimilsena | 35:871447da961d | 138 | |
saileshtimilsena | 35:871447da961d | 139 | void Tx_interrupt(); |
saileshtimilsena | 35:871447da961d | 140 | void Rx_interrupt(); |
saileshtimilsena | 35:871447da961d | 141 | void send_line(); |
saileshtimilsena | 35:871447da961d | 142 | void read_line(); |
saileshtimilsena | 35:871447da961d | 143 | |
saileshtimilsena | 35:871447da961d | 144 | |
saileshtimilsena | 35:871447da961d | 145 | // Circular buffers for serial TX and RX data - used by interrupt routines |
saileshtimilsena | 35:871447da961d | 146 | const int buffer_size = 255; |
saileshtimilsena | 35:871447da961d | 147 | // might need to increase buffer size for high baud rates |
saileshtimilsena | 35:871447da961d | 148 | char tx_buffer[buffer_size+1]; |
saileshtimilsena | 35:871447da961d | 149 | char rx_buffer[buffer_size+1]; |
saileshtimilsena | 35:871447da961d | 150 | // Circular buffer pointers |
saileshtimilsena | 35:871447da961d | 151 | // volatile makes read-modify-write atomic |
saileshtimilsena | 35:871447da961d | 152 | volatile int tx_in=0; |
saileshtimilsena | 35:871447da961d | 153 | volatile int tx_out=0; |
saileshtimilsena | 35:871447da961d | 154 | volatile int rx_in=0; |
saileshtimilsena | 35:871447da961d | 155 | volatile int rx_out=0; |
saileshtimilsena | 35:871447da961d | 156 | // Line buffers for sprintf and sscanf |
saileshtimilsena | 35:871447da961d | 157 | char tx_line[80]; |
saileshtimilsena | 35:871447da961d | 158 | char rx_line[80]; |
saileshtimilsena | 35:871447da961d | 159 | |
saileshtimilsena | 35:871447da961d | 160 | int i=0; |
saileshtimilsena | 35:871447da961d | 161 | int rx_i=0; |
saileshtimilsena | 35:871447da961d | 162 | device.baud(115200); |
saileshtimilsena | 35:871447da961d | 163 | |
saileshtimilsena | 35:871447da961d | 164 | // Setup a serial interrupt function to receive data |
saileshtimilsena | 35:871447da961d | 165 | device.attach(&Rx_interrupt, Serial::RxIrq); |
saileshtimilsena | 35:871447da961d | 166 | // Setup a serial interrupt function to transmit data |
saileshtimilsena | 35:871447da961d | 167 | device.attach(&Tx_interrupt, Serial::TxIrq); |
saileshtimilsena | 35:871447da961d | 168 | |
saileshtimilsena | 35:871447da961d | 169 | |
screamer | 11:8df4529f060d | 170 | void button_press() { |
screamer | 12:1f1a50e973db | 171 | int v = res_button->get_value_int() + 1; |
screamer | 12:1f1a50e973db | 172 | res_button->set_value(v); |
screamer | 29:6ff737b67e7d | 173 | printf("*** Button clicked %d times \n", v); |
adustm | 1:e86b1cffc402 | 174 | } |
saileshtimilsena | 35:871447da961d | 175 | |
saileshtimilsena | 35:871447da961d | 176 | // Called everytime a new character goes into |
saileshtimilsena | 35:871447da961d | 177 | // the RX buffer. Test that character for \n |
saileshtimilsena | 35:871447da961d | 178 | // Note, rxGetLastChar() gets the last char that |
saileshtimilsena | 35:871447da961d | 179 | // we received but it does NOT remove it from |
saileshtimilsena | 35:871447da961d | 180 | // the RX buffer. |
saileshtimilsena | 35:871447da961d | 181 | void read_line() { |
saileshtimilsena | 35:871447da961d | 182 | int i; |
saileshtimilsena | 35:871447da961d | 183 | i = 0; |
saileshtimilsena | 35:871447da961d | 184 | // Start Critical Section - don't interrupt while changing global buffer variables |
saileshtimilsena | 35:871447da961d | 185 | NVIC_DisableIRQ(UART1_IRQn); |
saileshtimilsena | 35:871447da961d | 186 | // Loop reading rx buffer characters until end of line character |
saileshtimilsena | 35:871447da961d | 187 | while ((i==0) || (rx_line[i-1] != '\r')) { |
saileshtimilsena | 35:871447da961d | 188 | // Wait if buffer empty |
saileshtimilsena | 35:871447da961d | 189 | if (rx_in == rx_out) { |
saileshtimilsena | 35:871447da961d | 190 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
saileshtimilsena | 35:871447da961d | 191 | NVIC_EnableIRQ(UART1_IRQn); |
saileshtimilsena | 35:871447da961d | 192 | while (rx_in == rx_out) { |
saileshtimilsena | 35:871447da961d | 193 | } |
saileshtimilsena | 35:871447da961d | 194 | // Start Critical Section - don't interrupt while changing global buffer variables |
saileshtimilsena | 35:871447da961d | 195 | NVIC_DisableIRQ(UART1_IRQn); |
saileshtimilsena | 35:871447da961d | 196 | } |
saileshtimilsena | 35:871447da961d | 197 | rx_line[i] = rx_buffer[rx_out]; |
saileshtimilsena | 35:871447da961d | 198 | i++; |
saileshtimilsena | 35:871447da961d | 199 | rx_out = (rx_out + 1) % buffer_size; |
saileshtimilsena | 35:871447da961d | 200 | } |
saileshtimilsena | 35:871447da961d | 201 | // End Critical Section |
saileshtimilsena | 35:871447da961d | 202 | NVIC_EnableIRQ(UART1_IRQn); |
saileshtimilsena | 35:871447da961d | 203 | rx_line[i-1] = 0; |
saileshtimilsena | 35:871447da961d | 204 | return; |
saileshtimilsena | 35:871447da961d | 205 | } |
adustm | 4:cf7342047b4d | 206 | /** |
adustm | 4:cf7342047b4d | 207 | * Notification callback handler |
adustm | 4:cf7342047b4d | 208 | * @param resource The resource that triggered the callback |
adustm | 4:cf7342047b4d | 209 | * @param status The delivery status of the notification |
adustm | 4:cf7342047b4d | 210 | */ |
adustm | 4:cf7342047b4d | 211 | void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) { |
screamer | 29:6ff737b67e7d | 212 | printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status); |
adustm | 4:cf7342047b4d | 213 | } |
adustm | 1:e86b1cffc402 | 214 | |
adustm | 4:cf7342047b4d | 215 | /** |
adustm | 4:cf7342047b4d | 216 | * Registration callback handler |
adustm | 4:cf7342047b4d | 217 | * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) |
adustm | 4:cf7342047b4d | 218 | */ |
adustm | 4:cf7342047b4d | 219 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
screamer | 17:fc98adcf835a | 220 | printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str()); |
screamer | 10:b27c962b3c3f | 221 | endpointInfo = endpoint; |
adustm | 4:cf7342047b4d | 222 | } |
adustm | 1:e86b1cffc402 | 223 | |
saileshtimilsena | 35:871447da961d | 224 | /* |
saileshtimilsena | 35:871447da961d | 225 | for one rotation when passed true or false in function OneStep(bool) |
saileshtimilsena | 35:871447da961d | 226 | */ |
saileshtimilsena | 35:871447da961d | 227 | void OneStep(bool dir){ |
saileshtimilsena | 35:871447da961d | 228 | if(dir) |
saileshtimilsena | 35:871447da961d | 229 | { |
saileshtimilsena | 35:871447da961d | 230 | switch(step_number){ |
saileshtimilsena | 35:871447da961d | 231 | case 0: |
saileshtimilsena | 35:871447da961d | 232 | steeperOne =1; |
saileshtimilsena | 35:871447da961d | 233 | steeperTwo =0; |
saileshtimilsena | 35:871447da961d | 234 | steeperThree =0; |
saileshtimilsena | 35:871447da961d | 235 | steeperFour =0; |
saileshtimilsena | 35:871447da961d | 236 | break; |
saileshtimilsena | 35:871447da961d | 237 | case 1: |
saileshtimilsena | 35:871447da961d | 238 | steeperOne =0; |
saileshtimilsena | 35:871447da961d | 239 | steeperTwo =1; |
saileshtimilsena | 35:871447da961d | 240 | steeperThree =0; |
saileshtimilsena | 35:871447da961d | 241 | steeperFour =0; |
saileshtimilsena | 35:871447da961d | 242 | break; |
saileshtimilsena | 35:871447da961d | 243 | case 2: |
saileshtimilsena | 35:871447da961d | 244 | steeperOne =0; |
saileshtimilsena | 35:871447da961d | 245 | steeperTwo =0; |
saileshtimilsena | 35:871447da961d | 246 | steeperThree =1; |
saileshtimilsena | 35:871447da961d | 247 | steeperFour =0; |
saileshtimilsena | 35:871447da961d | 248 | break; |
saileshtimilsena | 35:871447da961d | 249 | case 3: |
saileshtimilsena | 35:871447da961d | 250 | steeperOne =0; |
saileshtimilsena | 35:871447da961d | 251 | steeperTwo =0; |
saileshtimilsena | 35:871447da961d | 252 | steeperThree =0; |
saileshtimilsena | 35:871447da961d | 253 | steeperFour =1; |
saileshtimilsena | 35:871447da961d | 254 | break; |
saileshtimilsena | 35:871447da961d | 255 | }// end of switch |
saileshtimilsena | 35:871447da961d | 256 | }// end of if |
saileshtimilsena | 35:871447da961d | 257 | |
saileshtimilsena | 35:871447da961d | 258 | else |
saileshtimilsena | 35:871447da961d | 259 | { |
saileshtimilsena | 35:871447da961d | 260 | switch(step_number){ |
saileshtimilsena | 35:871447da961d | 261 | case 0: |
saileshtimilsena | 35:871447da961d | 262 | steeperOne =0; |
saileshtimilsena | 35:871447da961d | 263 | steeperTwo =0; |
saileshtimilsena | 35:871447da961d | 264 | steeperThree =0; |
saileshtimilsena | 35:871447da961d | 265 | steeperFour =1; |
saileshtimilsena | 35:871447da961d | 266 | break; |
saileshtimilsena | 35:871447da961d | 267 | case 1: |
saileshtimilsena | 35:871447da961d | 268 | steeperOne =0; |
saileshtimilsena | 35:871447da961d | 269 | steeperTwo =0; |
saileshtimilsena | 35:871447da961d | 270 | steeperThree =1; |
saileshtimilsena | 35:871447da961d | 271 | steeperFour =0; |
saileshtimilsena | 35:871447da961d | 272 | break; |
saileshtimilsena | 35:871447da961d | 273 | case 2: |
saileshtimilsena | 35:871447da961d | 274 | steeperOne =0; |
saileshtimilsena | 35:871447da961d | 275 | steeperTwo =1; |
saileshtimilsena | 35:871447da961d | 276 | steeperThree =0; |
saileshtimilsena | 35:871447da961d | 277 | steeperFour =0; |
saileshtimilsena | 35:871447da961d | 278 | break; |
saileshtimilsena | 35:871447da961d | 279 | case 3: |
saileshtimilsena | 35:871447da961d | 280 | steeperOne =1; |
saileshtimilsena | 35:871447da961d | 281 | steeperTwo =0; |
saileshtimilsena | 35:871447da961d | 282 | steeperThree =0; |
saileshtimilsena | 35:871447da961d | 283 | steeperFour =0; |
saileshtimilsena | 35:871447da961d | 284 | break; |
saileshtimilsena | 35:871447da961d | 285 | |
saileshtimilsena | 35:871447da961d | 286 | |
saileshtimilsena | 35:871447da961d | 287 | }// end of switch |
saileshtimilsena | 35:871447da961d | 288 | }// end of else |
saileshtimilsena | 35:871447da961d | 289 | step_number++; |
saileshtimilsena | 35:871447da961d | 290 | if(step_number > 3){ |
saileshtimilsena | 35:871447da961d | 291 | step_number = 0; |
saileshtimilsena | 35:871447da961d | 292 | } |
saileshtimilsena | 35:871447da961d | 293 | } // end of OneStep() function |
screamer | 10:b27c962b3c3f | 294 | /** |
screamer | 10:b27c962b3c3f | 295 | * Initialize sensors |
screamer | 10:b27c962b3c3f | 296 | */ |
screamer | 10:b27c962b3c3f | 297 | void sensors_init() { |
saileshtimilsena | 35:871447da961d | 298 | uint8_t id1, id2; |
screamer | 10:b27c962b3c3f | 299 | |
screamer | 29:6ff737b67e7d | 300 | printf ("\nSensors configuration:\n"); |
screamer | 10:b27c962b3c3f | 301 | // Initialize sensors |
screamer | 12:1f1a50e973db | 302 | sen_hum_temp.init(NULL); |
screamer | 12:1f1a50e973db | 303 | sen_press_temp.init(NULL); |
screamer | 10:b27c962b3c3f | 304 | |
screamer | 10:b27c962b3c3f | 305 | /// Call sensors enable routines |
screamer | 12:1f1a50e973db | 306 | sen_hum_temp.enable(); |
screamer | 12:1f1a50e973db | 307 | sen_press_temp.enable(); |
screamer | 10:b27c962b3c3f | 308 | |
screamer | 29:6ff737b67e7d | 309 | sen_hum_temp.read_id(&id1); |
screamer | 29:6ff737b67e7d | 310 | sen_press_temp.read_id(&id2); |
screamer | 10:b27c962b3c3f | 311 | |
screamer | 29:6ff737b67e7d | 312 | printf("HTS221 humidity & temperature = 0x%X\n", id1); |
screamer | 29:6ff737b67e7d | 313 | printf("LPS22HB pressure & temperature = 0x%X\n", id2); |
screamer | 10:b27c962b3c3f | 314 | |
screamer | 17:fc98adcf835a | 315 | printf("\n"); ; |
screamer | 10:b27c962b3c3f | 316 | } |
screamer | 10:b27c962b3c3f | 317 | |
screamer | 10:b27c962b3c3f | 318 | /** |
screamer | 10:b27c962b3c3f | 319 | * Update sensors and report their values. |
screamer | 10:b27c962b3c3f | 320 | * This function is called periodically. |
screamer | 10:b27c962b3c3f | 321 | */ |
screamer | 10:b27c962b3c3f | 322 | void sensors_update() { |
saileshtimilsena | 35:871447da961d | 323 | float temp3_value, humid_value, pressure_value = 0.0; |
screamer | 10:b27c962b3c3f | 324 | |
screamer | 32:2871fbeb627d | 325 | sen_hum_temp.get_humidity(&humid_value); |
screamer | 32:2871fbeb627d | 326 | sen_press_temp.get_pressure(&pressure_value); |
screamer | 32:2871fbeb627d | 327 | temp3_value = adc_temp.read()*100; |
saileshtimilsena | 35:871447da961d | 328 | |
saileshtimilsena | 35:871447da961d | 329 | printf("%2.0f^%2.0f^%3.0f",temp3_value,humid_value,pressure_value); |
saileshtimilsena | 35:871447da961d | 330 | printf("\n"); |
saileshtimilsena | 35:871447da961d | 331 | |
saileshtimilsena | 35:871447da961d | 332 | // printf ("%02d : %02d : %02d\n", temp3_value, hummitly, pressure); |
saileshtimilsena | 35:871447da961d | 333 | |
saileshtimilsena | 35:871447da961d | 334 | // added a new code for stepper motor |
saileshtimilsena | 35:871447da961d | 335 | // if temperature reading is higher, open motor |
saileshtimilsena | 35:871447da961d | 336 | if(temp3_value > 30) |
saileshtimilsena | 35:871447da961d | 337 | { |
saileshtimilsena | 35:871447da961d | 338 | open_flag = 0; |
saileshtimilsena | 35:871447da961d | 339 | while(open_flag==0){ |
saileshtimilsena | 35:871447da961d | 340 | OneStep(false); |
saileshtimilsena | 35:871447da961d | 341 | wait(0.01); |
saileshtimilsena | 35:871447da961d | 342 | bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); |
saileshtimilsena | 35:871447da961d | 343 | if (btn_pressed) { |
saileshtimilsena | 35:871447da961d | 344 | printf("Door Opened...\n"); |
saileshtimilsena | 35:871447da961d | 345 | open_flag=1; |
saileshtimilsena | 35:871447da961d | 346 | close_flag=0; |
saileshtimilsena | 35:871447da961d | 347 | }//end of if(btn_pressed) |
saileshtimilsena | 35:871447da961d | 348 | }// end of while |
saileshtimilsena | 35:871447da961d | 349 | }// end of if(temp3_value>20) |
saileshtimilsena | 35:871447da961d | 350 | // now we expect to close |
saileshtimilsena | 35:871447da961d | 351 | |
saileshtimilsena | 35:871447da961d | 352 | if(temp3_value < 20)//door has to close |
saileshtimilsena | 35:871447da961d | 353 | { |
saileshtimilsena | 35:871447da961d | 354 | close_flag = 0; |
saileshtimilsena | 35:871447da961d | 355 | while(close_flag==0){ |
saileshtimilsena | 35:871447da961d | 356 | OneStep(true); |
saileshtimilsena | 35:871447da961d | 357 | wait(0.01); |
saileshtimilsena | 35:871447da961d | 358 | bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); |
saileshtimilsena | 35:871447da961d | 359 | if (btn_pressed) { |
saileshtimilsena | 35:871447da961d | 360 | printf("Door Closed...\n"); |
saileshtimilsena | 35:871447da961d | 361 | open_flag=0; |
saileshtimilsena | 35:871447da961d | 362 | close_flag=1; |
saileshtimilsena | 35:871447da961d | 363 | }//end of if(btn_pressed) |
saileshtimilsena | 35:871447da961d | 364 | }// end of while |
screamer | 10:b27c962b3c3f | 365 | } |
screamer | 10:b27c962b3c3f | 366 | |
screamer | 13:42b49a0caade | 367 | if (endpointInfo) { |
screamer | 33:cfd9430e7d1e | 368 | #ifdef SEND_ALL_SENSORS |
screamer | 32:2871fbeb627d | 369 | res_humidity->set_value(humid_value); |
screamer | 32:2871fbeb627d | 370 | res_pressure->set_value(pressure_value); |
screamer | 32:2871fbeb627d | 371 | res_adc_temp->set_value(temp3_value); |
screamer | 13:42b49a0caade | 372 | #endif /* SEND_ALL_SENSORS */ |
screamer | 28:0e774865873d | 373 | } |
screamer | 10:b27c962b3c3f | 374 | } |
screamer | 10:b27c962b3c3f | 375 | |
saileshtimilsena | 35:871447da961d | 376 | int main(void) |
saileshtimilsena | 35:871447da961d | 377 | { |
screamer | 17:fc98adcf835a | 378 | printf("\nStarting Simple Pelion Device Management Client example\n"); |
adustm | 4:cf7342047b4d | 379 | |
screamer | 29:6ff737b67e7d | 380 | int storage_status = fs.mount(&sd); |
screamer | 29:6ff737b67e7d | 381 | if (storage_status != 0) { |
screamer | 29:6ff737b67e7d | 382 | printf("Storage mounting failed.\n"); |
screamer | 29:6ff737b67e7d | 383 | } |
screamer | 30:15743b79c6cb | 384 | // If the User button is pressed ons start, then format storage. |
screamer | 29:6ff737b67e7d | 385 | bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE); |
screamer | 29:6ff737b67e7d | 386 | if (btn_pressed) { |
screamer | 29:6ff737b67e7d | 387 | printf("User button is pushed on start...\n"); |
screamer | 29:6ff737b67e7d | 388 | } |
screamer | 30:15743b79c6cb | 389 | |
screamer | 29:6ff737b67e7d | 390 | if (storage_status || btn_pressed) { |
screamer | 29:6ff737b67e7d | 391 | printf("Formatting the storage...\n"); |
screamer | 30:15743b79c6cb | 392 | int storage_status = StorageHelper::format(&fs, &sd); |
screamer | 10:b27c962b3c3f | 393 | if (storage_status != 0) { |
screamer | 13:42b49a0caade | 394 | printf("ERROR: Failed to reformat the storage (%d).\n", storage_status); |
screamer | 10:b27c962b3c3f | 395 | } |
screamer | 28:0e774865873d | 396 | } else { |
screamer | 28:0e774865873d | 397 | printf("You can hold the user button during boot to format the storage and change the device identity.\n"); |
screamer | 10:b27c962b3c3f | 398 | } |
screamer | 10:b27c962b3c3f | 399 | |
screamer | 10:b27c962b3c3f | 400 | sensors_init(); |
screamer | 10:b27c962b3c3f | 401 | |
adustm | 4:cf7342047b4d | 402 | // Connect to the internet (DHCP is expected to be on) |
screamer | 13:42b49a0caade | 403 | printf("Connecting to the network using Wifi...\n"); |
MarceloSalazar | 9:265744785d33 | 404 | net = NetworkInterface::get_default_instance(); |
adustm | 4:cf7342047b4d | 405 | |
screamer | 10:b27c962b3c3f | 406 | nsapi_error_t net_status = -1; |
screamer | 10:b27c962b3c3f | 407 | for (int tries = 0; tries < 3; tries++) { |
screamer | 10:b27c962b3c3f | 408 | net_status = net->connect(); |
screamer | 10:b27c962b3c3f | 409 | if (net_status == NSAPI_ERROR_OK) { |
screamer | 10:b27c962b3c3f | 410 | break; |
screamer | 10:b27c962b3c3f | 411 | } else { |
screamer | 13:42b49a0caade | 412 | printf("Unable to connect to network. Retrying...\n"); |
screamer | 10:b27c962b3c3f | 413 | } |
screamer | 10:b27c962b3c3f | 414 | } |
MarceloSalazar | 9:265744785d33 | 415 | |
screamer | 10:b27c962b3c3f | 416 | if (net_status != NSAPI_ERROR_OK) { |
screamer | 13:42b49a0caade | 417 | printf("ERROR: Connecting to the network failed (%d)!\n", net_status); |
adustm | 1:e86b1cffc402 | 418 | return -1; |
adustm | 1:e86b1cffc402 | 419 | } |
adustm | 1:e86b1cffc402 | 420 | |
MarceloSalazar | 9:265744785d33 | 421 | printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address()); |
adustm | 1:e86b1cffc402 | 422 | |
screamer | 17:fc98adcf835a | 423 | printf("Initializing Pelion Device Management Client...\n"); |
screamer | 17:fc98adcf835a | 424 | |
MarceloSalazar | 9:265744785d33 | 425 | // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM |
MarceloSalazar | 9:265744785d33 | 426 | SimpleMbedCloudClient client(net, bd, &fs); |
adustm | 4:cf7342047b4d | 427 | int client_status = client.init(); |
adustm | 4:cf7342047b4d | 428 | if (client_status != 0) { |
screamer | 13:42b49a0caade | 429 | printf("ERROR: Pelion Client initialization failed (%d)\n", client_status); |
adustm | 1:e86b1cffc402 | 430 | return -1; |
adustm | 1:e86b1cffc402 | 431 | } |
adustm | 1:e86b1cffc402 | 432 | |
adustm | 4:cf7342047b4d | 433 | // Creating resources, which can be written or read from the cloud |
screamer | 32:2871fbeb627d | 434 | res_button = client.create_resource("3200/0/5501", "Button Count"); |
screamer | 12:1f1a50e973db | 435 | res_button->set_value(0); |
screamer | 12:1f1a50e973db | 436 | res_button->methods(M2MMethod::GET); |
screamer | 12:1f1a50e973db | 437 | res_button->observable(true); |
screamer | 12:1f1a50e973db | 438 | res_button->attach_notification_callback(button_callback); |
adustm | 1:e86b1cffc402 | 439 | |
screamer | 33:cfd9430e7d1e | 440 | res_led = client.create_resource("3201/0/5853", "LED State"); |
screamer | 33:cfd9430e7d1e | 441 | res_led->set_value(1); |
screamer | 33:cfd9430e7d1e | 442 | res_led->methods(M2MMethod::GET | M2MMethod::PUT); |
screamer | 33:cfd9430e7d1e | 443 | res_led->attach_put_callback(put_callback); |
screamer | 33:cfd9430e7d1e | 444 | |
screamer | 33:cfd9430e7d1e | 445 | #ifdef SEND_ALL_SENSORS |
screamer | 10:b27c962b3c3f | 446 | // Sensor resources |
saileshtimilsena | 35:871447da961d | 447 | /* res_temperature = client.create_resource("3303/0/5700", "Temperature HTS221 (C)"); |
screamer | 12:1f1a50e973db | 448 | res_temperature->set_value(0); |
screamer | 12:1f1a50e973db | 449 | res_temperature->methods(M2MMethod::GET); |
saileshtimilsena | 35:871447da961d | 450 | res_temperature->observable(true);*/ |
screamer | 10:b27c962b3c3f | 451 | |
screamer | 32:2871fbeb627d | 452 | res_humidity = client.create_resource("3304/0/5700", "Humidity"); |
screamer | 12:1f1a50e973db | 453 | res_humidity->set_value(0); |
screamer | 12:1f1a50e973db | 454 | res_humidity->methods(M2MMethod::GET); |
screamer | 12:1f1a50e973db | 455 | res_humidity->observable(true); |
screamer | 10:b27c962b3c3f | 456 | |
screamer | 32:2871fbeb627d | 457 | res_adc_temp = client.create_resource("3303/2/5700", "Temperature ADC (C)"); |
screamer | 28:0e774865873d | 458 | res_adc_temp->set_value(0); |
screamer | 28:0e774865873d | 459 | res_adc_temp->methods(M2MMethod::GET); |
screamer | 28:0e774865873d | 460 | res_adc_temp->observable(true); |
screamer | 28:0e774865873d | 461 | |
screamer | 32:2871fbeb627d | 462 | res_pressure = client.create_resource("3323/0/5700", "Pressure"); |
screamer | 28:0e774865873d | 463 | res_pressure->set_value(0); |
screamer | 28:0e774865873d | 464 | res_pressure->methods(M2MMethod::GET); |
screamer | 28:0e774865873d | 465 | res_pressure->observable(true); |
screamer | 28:0e774865873d | 466 | |
screamer | 13:42b49a0caade | 467 | #endif /* SEND_ALL_SENSORS */ |
screamer | 11:8df4529f060d | 468 | |
MarceloSalazar | 9:265744785d33 | 469 | printf("Initialized Pelion Client. Registering...\n"); |
adustm | 1:e86b1cffc402 | 470 | |
adustm | 4:cf7342047b4d | 471 | // Callback that fires when registering is complete |
adustm | 4:cf7342047b4d | 472 | client.on_registered(®istered); |
adustm | 1:e86b1cffc402 | 473 | |
MarceloSalazar | 9:265744785d33 | 474 | // Register with Pelion DM |
adustm | 4:cf7342047b4d | 475 | client.register_and_connect(); |
adustm | 1:e86b1cffc402 | 476 | |
screamer | 17:fc98adcf835a | 477 | int i = 600; // wait up 60 seconds before attaching sensors and button events |
screamer | 12:1f1a50e973db | 478 | while (i-- > 0 && !client.is_client_registered()) { |
screamer | 12:1f1a50e973db | 479 | wait_ms(100); |
screamer | 12:1f1a50e973db | 480 | } |
screamer | 12:1f1a50e973db | 481 | |
screamer | 11:8df4529f060d | 482 | button.fall(eventQueue.event(&button_press)); |
screamer | 10:b27c962b3c3f | 483 | |
screamer | 15:a0430d40a918 | 484 | // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations |
adustm | 4:cf7342047b4d | 485 | Ticker timer; |
screamer | 11:8df4529f060d | 486 | timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL); |
adustm | 1:e86b1cffc402 | 487 | |
adustm | 4:cf7342047b4d | 488 | // You can easily run the eventQueue in a separate thread if required |
adustm | 4:cf7342047b4d | 489 | eventQueue.dispatch_forever(); |
adustm | 1:e86b1cffc402 | 490 | } |
screamer | 28:0e774865873d | 491 | |
MarceloSalazar | 9:265744785d33 | 492 | #endif |