Mauricio Parra
/
Pluviometro
Version 1 Pluviometro - Junio20-2020
main.cpp@2:27c367a27239, 2020-06-20 (annotated)
- Committer:
- mauro1019
- Date:
- Sat Jun 20 02:51:35 2020 +0000
- Revision:
- 2:27c367a27239
- Parent:
- 1:ff1093db32ed
Version 1 - Junio20
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mauro1019 | 0:1e3ed371dacf | 1 | /* mbed Microcontroller Library |
mauro1019 | 0:1e3ed371dacf | 2 | * Copyright (c) 2006-2013 ARM Limited |
mauro1019 | 0:1e3ed371dacf | 3 | * |
mauro1019 | 0:1e3ed371dacf | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mauro1019 | 0:1e3ed371dacf | 5 | * you may not use this file except in compliance with the License. |
mauro1019 | 0:1e3ed371dacf | 6 | * You may obtain a copy of the License at |
mauro1019 | 0:1e3ed371dacf | 7 | * |
mauro1019 | 0:1e3ed371dacf | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mauro1019 | 0:1e3ed371dacf | 9 | * |
mauro1019 | 0:1e3ed371dacf | 10 | * Unless required by applicable law or agreed to in writing, software |
mauro1019 | 0:1e3ed371dacf | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mauro1019 | 0:1e3ed371dacf | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mauro1019 | 0:1e3ed371dacf | 13 | * See the License for the specific language governing permissions and |
mauro1019 | 0:1e3ed371dacf | 14 | * limitations under the License. |
mauro1019 | 0:1e3ed371dacf | 15 | */ |
mauro1019 | 0:1e3ed371dacf | 16 | #include "mbed.h" |
mauro1019 | 0:1e3ed371dacf | 17 | #include "rtos.h" |
mauro1019 | 0:1e3ed371dacf | 18 | #include <errno.h> |
mauro1019 | 0:1e3ed371dacf | 19 | #include <stdio.h> |
mauro1019 | 0:1e3ed371dacf | 20 | #include <vector> |
mauro1019 | 0:1e3ed371dacf | 21 | |
mauro1019 | 0:1e3ed371dacf | 22 | // Block devices |
mauro1019 | 0:1e3ed371dacf | 23 | //#include "SPIFBlockDevice.h" |
mauro1019 | 0:1e3ed371dacf | 24 | //#include "DataFlashBlockDevice.h" |
mauro1019 | 0:1e3ed371dacf | 25 | #include "SDBlockDevice.h" |
mauro1019 | 0:1e3ed371dacf | 26 | //#include "HeapBlockDevice.h" |
mauro1019 | 0:1e3ed371dacf | 27 | |
mauro1019 | 0:1e3ed371dacf | 28 | // File systems |
mauro1019 | 0:1e3ed371dacf | 29 | |
mauro1019 | 0:1e3ed371dacf | 30 | //#include "LittleFileSystem.h" |
mauro1019 | 0:1e3ed371dacf | 31 | #include "FATFileSystem.h" |
mauro1019 | 0:1e3ed371dacf | 32 | |
mauro1019 | 0:1e3ed371dacf | 33 | Thread thread; |
mauro1019 | 0:1e3ed371dacf | 34 | #define STOP_FLAG 1 |
mauro1019 | 0:1e3ed371dacf | 35 | DigitalOut led1(PC_13); |
mauro1019 | 0:1e3ed371dacf | 36 | |
mauro1019 | 0:1e3ed371dacf | 37 | // Physical block device, can be any device that supports the BlockDevice API |
mauro1019 | 0:1e3ed371dacf | 38 | SDBlockDevice blockDevice(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sck, cs |
mauro1019 | 0:1e3ed371dacf | 39 | Serial pc(PB_10, PB_11); |
mauro1019 | 0:1e3ed371dacf | 40 | // File system declaration |
mauro1019 | 0:1e3ed371dacf | 41 | FATFileSystem fileSystem("fs"); |
mauro1019 | 0:1e3ed371dacf | 42 | |
mauro1019 | 2:27c367a27239 | 43 | //AnalogIn pot(PA_3); // PB_1 |
mauro1019 | 2:27c367a27239 | 44 | DigitalIn mypin(PB_1); // PA_2 |
mauro1019 | 2:27c367a27239 | 45 | //InterruptIn mypin(D1); // PA_2 |
mauro1019 | 0:1e3ed371dacf | 46 | |
mauro1019 | 0:1e3ed371dacf | 47 | static bool myflag = false; |
mauro1019 | 0:1e3ed371dacf | 48 | static bool rain_flag = false; // inicia False - No esta lloviendo |
mauro1019 | 0:1e3ed371dacf | 49 | static bool timer_rain = false; // inicia False - No esta lloviendo |
mauro1019 | 0:1e3ed371dacf | 50 | static bool tick_count = false; // bandera para indicar que ha pasado un minuto de medicion |
mauro1019 | 0:1e3ed371dacf | 51 | static bool timer60s = false; |
mauro1019 | 0:1e3ed371dacf | 52 | // LowPowerTicker flipper; |
mauro1019 | 0:1e3ed371dacf | 53 | Timeout flipper; |
mauro1019 | 0:1e3ed371dacf | 54 | Timeout flipper2; |
mauro1019 | 0:1e3ed371dacf | 55 | |
mauro1019 | 0:1e3ed371dacf | 56 | long pos; |
mauro1019 | 0:1e3ed371dacf | 57 | FILE *f; |
mauro1019 | 0:1e3ed371dacf | 58 | int err; |
mauro1019 | 0:1e3ed371dacf | 59 | |
mauro1019 | 0:1e3ed371dacf | 60 | int number1; |
mauro1019 | 0:1e3ed371dacf | 61 | |
mauro1019 | 0:1e3ed371dacf | 62 | void flip() |
mauro1019 | 0:1e3ed371dacf | 63 | { |
mauro1019 | 0:1e3ed371dacf | 64 | tick_count = true; |
mauro1019 | 0:1e3ed371dacf | 65 | } |
mauro1019 | 0:1e3ed371dacf | 66 | void flip2() |
mauro1019 | 0:1e3ed371dacf | 67 | { |
mauro1019 | 0:1e3ed371dacf | 68 | timer_rain = true; |
mauro1019 | 0:1e3ed371dacf | 69 | } |
mauro1019 | 0:1e3ed371dacf | 70 | |
mauro1019 | 0:1e3ed371dacf | 71 | void timer60seg() |
mauro1019 | 0:1e3ed371dacf | 72 | { |
mauro1019 | 0:1e3ed371dacf | 73 | timer60s = true; |
mauro1019 | 0:1e3ed371dacf | 74 | } |
mauro1019 | 0:1e3ed371dacf | 75 | |
mauro1019 | 0:1e3ed371dacf | 76 | // Entry point for the example |
mauro1019 | 0:1e3ed371dacf | 77 | int main() |
mauro1019 | 0:1e3ed371dacf | 78 | { |
mauro1019 | 2:27c367a27239 | 79 | char command[8]; |
mauro1019 | 0:1e3ed371dacf | 80 | int rain; |
mauro1019 | 0:1e3ed371dacf | 81 | int estado; |
mauro1019 | 0:1e3ed371dacf | 82 | uint16_t analog_sensor; |
mauro1019 | 2:27c367a27239 | 83 | rain = 0; |
mauro1019 | 0:1e3ed371dacf | 84 | estado = 3; |
mauro1019 | 2:27c367a27239 | 85 | |
mauro1019 | 2:27c367a27239 | 86 | |
mauro1019 | 2:27c367a27239 | 87 | //mypin.rise(&tip_interrup); // attach the address of the flip function to the rising edge |
mauro1019 | 0:1e3ed371dacf | 88 | |
mauro1019 | 0:1e3ed371dacf | 89 | pc.printf("--- Mbed OS filesystem example ---\n"); |
mauro1019 | 0:1e3ed371dacf | 90 | |
mauro1019 | 0:1e3ed371dacf | 91 | // Try to mount the filesystem |
mauro1019 | 0:1e3ed371dacf | 92 | pc.printf("Mounting the filesystem... "); |
mauro1019 | 0:1e3ed371dacf | 93 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 94 | |
mauro1019 | 0:1e3ed371dacf | 95 | err = fileSystem.mount(&blockDevice); |
mauro1019 | 0:1e3ed371dacf | 96 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 97 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 98 | // Reformat if we can't mount the filesystem |
mauro1019 | 0:1e3ed371dacf | 99 | // this should only happen on the first boot |
mauro1019 | 0:1e3ed371dacf | 100 | pc.printf("No filesystem found, formatting... "); |
mauro1019 | 0:1e3ed371dacf | 101 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 102 | err = fileSystem.reformat(&blockDevice); |
mauro1019 | 0:1e3ed371dacf | 103 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 104 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 105 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 106 | } |
mauro1019 | 0:1e3ed371dacf | 107 | } |
mauro1019 | 0:1e3ed371dacf | 108 | |
mauro1019 | 0:1e3ed371dacf | 109 | // Open the numbers file |
mauro1019 | 0:1e3ed371dacf | 110 | pc.printf("Opening \"/fs/pluv.txt\"... "); |
mauro1019 | 0:1e3ed371dacf | 111 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 112 | |
mauro1019 | 0:1e3ed371dacf | 113 | f = fopen("/fs/pluv.txt", "r+"); |
mauro1019 | 0:1e3ed371dacf | 114 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 115 | if (!f) { |
mauro1019 | 0:1e3ed371dacf | 116 | // Create the .txt file if it doesn't exist |
mauro1019 | 0:1e3ed371dacf | 117 | pc.printf("No file found, creating a new file... "); |
mauro1019 | 0:1e3ed371dacf | 118 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 119 | f = fopen("/fs/pluv.txt", "w+"); |
mauro1019 | 0:1e3ed371dacf | 120 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 121 | if (!f) { |
mauro1019 | 0:1e3ed371dacf | 122 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 123 | } |
mauro1019 | 0:1e3ed371dacf | 124 | //////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 125 | err = fprintf(f, "\nInciando SD por primera ves\n"); |
mauro1019 | 0:1e3ed371dacf | 126 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 127 | pc.printf("Fail :(\n"); |
mauro1019 | 0:1e3ed371dacf | 128 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 129 | } |
mauro1019 | 0:1e3ed371dacf | 130 | /////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 131 | /* |
mauro1019 | 0:1e3ed371dacf | 132 | for (int i = 0; i < 5; i++) { |
mauro1019 | 0:1e3ed371dacf | 133 | pc.printf("\rWriting pluv (%d/%d)... ", i, 5); |
mauro1019 | 0:1e3ed371dacf | 134 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 135 | err = fprintf(f, "Saving test %d\n", i); |
mauro1019 | 0:1e3ed371dacf | 136 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 137 | pc.printf("Fail :(\n"); |
mauro1019 | 0:1e3ed371dacf | 138 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 139 | } |
mauro1019 | 0:1e3ed371dacf | 140 | } |
mauro1019 | 0:1e3ed371dacf | 141 | */ |
mauro1019 | 0:1e3ed371dacf | 142 | |
mauro1019 | 0:1e3ed371dacf | 143 | //pc.printf("\rSaving Test (%d/%d)... OK\n", 5, 5); |
mauro1019 | 0:1e3ed371dacf | 144 | pc.printf("\rSaving Test ... OK\n"); |
mauro1019 | 0:1e3ed371dacf | 145 | /* |
mauro1019 | 0:1e3ed371dacf | 146 | pc.printf("Seeking file... "); |
mauro1019 | 0:1e3ed371dacf | 147 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 148 | err = fseek(f, 0, SEEK_SET); |
mauro1019 | 0:1e3ed371dacf | 149 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 150 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 151 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 152 | }*/ |
mauro1019 | 0:1e3ed371dacf | 153 | } |
mauro1019 | 0:1e3ed371dacf | 154 | |
mauro1019 | 0:1e3ed371dacf | 155 | /////////////Prueba Mauro\\\\\\\\\\\\\\\\\\\\\\\\ |
mauro1019 | 0:1e3ed371dacf | 156 | |
mauro1019 | 0:1e3ed371dacf | 157 | //fgetpos (f, &position); |
mauro1019 | 0:1e3ed371dacf | 158 | // Get current stream position |
mauro1019 | 0:1e3ed371dacf | 159 | //pos = ftell(f); |
mauro1019 | 0:1e3ed371dacf | 160 | //fseek(f, pos, SEEK_SET); |
mauro1019 | 0:1e3ed371dacf | 161 | err = fprintf(f, "\nInit SD\n"); |
mauro1019 | 0:1e3ed371dacf | 162 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 163 | pc.printf("Fail :(\n"); |
mauro1019 | 0:1e3ed371dacf | 164 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 165 | } |
mauro1019 | 0:1e3ed371dacf | 166 | |
mauro1019 | 0:1e3ed371dacf | 167 | |
mauro1019 | 0:1e3ed371dacf | 168 | ///////SI EL ARCHIVO EXISTE///////////////////////// |
mauro1019 | 0:1e3ed371dacf | 169 | |
mauro1019 | 0:1e3ed371dacf | 170 | // Go through and increment the numbers |
mauro1019 | 0:1e3ed371dacf | 171 | /* |
mauro1019 | 0:1e3ed371dacf | 172 | for (int i = 0; i < 5; i++) { |
mauro1019 | 0:1e3ed371dacf | 173 | pc.printf("\rIncrementing numbers (%d/%d)... ", i, 5); |
mauro1019 | 0:1e3ed371dacf | 174 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 175 | |
mauro1019 | 0:1e3ed371dacf | 176 | // Get current stream position |
mauro1019 | 0:1e3ed371dacf | 177 | long pos = ftell(f); |
mauro1019 | 0:1e3ed371dacf | 178 | |
mauro1019 | 0:1e3ed371dacf | 179 | // Parse out the number and increment |
mauro1019 | 0:1e3ed371dacf | 180 | int32_t number; |
mauro1019 | 0:1e3ed371dacf | 181 | fscanf(f, "%d", &number); |
mauro1019 | 0:1e3ed371dacf | 182 | number += 1; |
mauro1019 | 0:1e3ed371dacf | 183 | |
mauro1019 | 0:1e3ed371dacf | 184 | // Seek to beginning of number |
mauro1019 | 0:1e3ed371dacf | 185 | fseek(f, pos, SEEK_SET); |
mauro1019 | 0:1e3ed371dacf | 186 | |
mauro1019 | 0:1e3ed371dacf | 187 | // Store number |
mauro1019 | 0:1e3ed371dacf | 188 | fprintf(f, "inc %ld\n", number); |
mauro1019 | 0:1e3ed371dacf | 189 | |
mauro1019 | 0:1e3ed371dacf | 190 | // Flush between write and read on same file |
mauro1019 | 0:1e3ed371dacf | 191 | fflush(f); |
mauro1019 | 0:1e3ed371dacf | 192 | } |
mauro1019 | 0:1e3ed371dacf | 193 | |
mauro1019 | 0:1e3ed371dacf | 194 | pc.printf("\rIncrementing pluv (%d/%d)... OK\n", 5, 5); |
mauro1019 | 0:1e3ed371dacf | 195 | */ |
mauro1019 | 0:1e3ed371dacf | 196 | /////////////////////////////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 197 | |
mauro1019 | 0:1e3ed371dacf | 198 | /////////////////CERRAR ARCHIVO/////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 199 | |
mauro1019 | 0:1e3ed371dacf | 200 | // Close the file which also flushes any cached writes |
mauro1019 | 0:1e3ed371dacf | 201 | pc.printf("Closing \"/fs/pluv.txt\"... "); |
mauro1019 | 0:1e3ed371dacf | 202 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 203 | err = fclose(f); |
mauro1019 | 0:1e3ed371dacf | 204 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 205 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 206 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 207 | } |
mauro1019 | 0:1e3ed371dacf | 208 | |
mauro1019 | 0:1e3ed371dacf | 209 | //////////////////////ARCHIVO CERRADO////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 210 | |
mauro1019 | 0:1e3ed371dacf | 211 | // Display the root directory |
mauro1019 | 0:1e3ed371dacf | 212 | pc.printf("Opening the root directory... "); |
mauro1019 | 0:1e3ed371dacf | 213 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 214 | |
mauro1019 | 0:1e3ed371dacf | 215 | DIR *d = opendir("/fs/"); |
mauro1019 | 0:1e3ed371dacf | 216 | pc.printf("%s\n", (!d ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 217 | if (!d) { |
mauro1019 | 0:1e3ed371dacf | 218 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 219 | } |
mauro1019 | 0:1e3ed371dacf | 220 | |
mauro1019 | 0:1e3ed371dacf | 221 | pc.printf("root directory:\n"); |
mauro1019 | 0:1e3ed371dacf | 222 | while (true) { |
mauro1019 | 0:1e3ed371dacf | 223 | struct dirent *e = readdir(d); |
mauro1019 | 0:1e3ed371dacf | 224 | if (!e) { |
mauro1019 | 0:1e3ed371dacf | 225 | break; |
mauro1019 | 0:1e3ed371dacf | 226 | } |
mauro1019 | 0:1e3ed371dacf | 227 | |
mauro1019 | 0:1e3ed371dacf | 228 | printf(" %s\n", e->d_name); |
mauro1019 | 0:1e3ed371dacf | 229 | } |
mauro1019 | 0:1e3ed371dacf | 230 | |
mauro1019 | 0:1e3ed371dacf | 231 | pc.printf("Closing the root directory... "); |
mauro1019 | 0:1e3ed371dacf | 232 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 233 | err = closedir(d); |
mauro1019 | 0:1e3ed371dacf | 234 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 235 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 236 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 237 | } |
mauro1019 | 0:1e3ed371dacf | 238 | |
mauro1019 | 0:1e3ed371dacf | 239 | // Display the numbers file |
mauro1019 | 0:1e3ed371dacf | 240 | pc.printf("Opening \"/fs/pluv.txt\"... "); |
mauro1019 | 0:1e3ed371dacf | 241 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 242 | f = fopen("/fs/pluv.txt", "r"); |
mauro1019 | 0:1e3ed371dacf | 243 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 244 | if (!f) { |
mauro1019 | 0:1e3ed371dacf | 245 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 246 | } |
mauro1019 | 0:1e3ed371dacf | 247 | |
mauro1019 | 0:1e3ed371dacf | 248 | pc.printf("numbers:\n"); |
mauro1019 | 0:1e3ed371dacf | 249 | while (!feof(f)) { |
mauro1019 | 0:1e3ed371dacf | 250 | int c = fgetc(f); |
mauro1019 | 0:1e3ed371dacf | 251 | printf("%c", c); |
mauro1019 | 0:1e3ed371dacf | 252 | } |
mauro1019 | 0:1e3ed371dacf | 253 | |
mauro1019 | 0:1e3ed371dacf | 254 | pc.printf("\rClosing \"/fs/pluv.txt\"... "); |
mauro1019 | 0:1e3ed371dacf | 255 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 256 | err = fclose(f); |
mauro1019 | 0:1e3ed371dacf | 257 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 258 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 259 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 260 | } |
mauro1019 | 0:1e3ed371dacf | 261 | |
mauro1019 | 0:1e3ed371dacf | 262 | // Tidy up |
mauro1019 | 0:1e3ed371dacf | 263 | pc.printf("Unmounting... "); |
mauro1019 | 0:1e3ed371dacf | 264 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 265 | err = fileSystem.unmount(); |
mauro1019 | 0:1e3ed371dacf | 266 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 267 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 268 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 269 | } |
mauro1019 | 0:1e3ed371dacf | 270 | |
mauro1019 | 0:1e3ed371dacf | 271 | pc.printf("Initializing the block device... "); |
mauro1019 | 0:1e3ed371dacf | 272 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 273 | |
mauro1019 | 0:1e3ed371dacf | 274 | err = blockDevice.init(); |
mauro1019 | 0:1e3ed371dacf | 275 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 276 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 277 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 278 | } |
mauro1019 | 0:1e3ed371dacf | 279 | |
mauro1019 | 0:1e3ed371dacf | 280 | pc.printf("Erasing the block device... "); |
mauro1019 | 0:1e3ed371dacf | 281 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 282 | err = blockDevice.erase(0, blockDevice.size()); |
mauro1019 | 0:1e3ed371dacf | 283 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 284 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 285 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 286 | } |
mauro1019 | 0:1e3ed371dacf | 287 | |
mauro1019 | 0:1e3ed371dacf | 288 | pc.printf("Deinitializing the block device... "); |
mauro1019 | 0:1e3ed371dacf | 289 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 290 | err = blockDevice.deinit(); |
mauro1019 | 0:1e3ed371dacf | 291 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 292 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 293 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 294 | } |
mauro1019 | 0:1e3ed371dacf | 295 | |
mauro1019 | 0:1e3ed371dacf | 296 | pc.printf("\r\n"); |
mauro1019 | 0:1e3ed371dacf | 297 | |
mauro1019 | 0:1e3ed371dacf | 298 | pc.printf("Mbed OS filesystem example done!\n"); |
mauro1019 | 0:1e3ed371dacf | 299 | |
mauro1019 | 2:27c367a27239 | 300 | pc.printf("Configurar la Hora\n"); |
mauro1019 | 2:27c367a27239 | 301 | if (pc.readable()) { |
mauro1019 | 2:27c367a27239 | 302 | pc.gets(command, 8); |
mauro1019 | 2:27c367a27239 | 303 | for (int i=0; i<8; i++) { |
mauro1019 | 2:27c367a27239 | 304 | pc.printf("%x ", command[i]); |
mauro1019 | 2:27c367a27239 | 305 | } |
mauro1019 | 2:27c367a27239 | 306 | pc.printf("\n"); |
mauro1019 | 2:27c367a27239 | 307 | } |
mauro1019 | 2:27c367a27239 | 308 | |
mauro1019 | 2:27c367a27239 | 309 | /* |
mauro1019 | 2:27c367a27239 | 310 | |
mauro1019 | 2:27c367a27239 | 311 | while() { |
mauro1019 | 2:27c367a27239 | 312 | |
mauro1019 | 2:27c367a27239 | 313 | } |
mauro1019 | 2:27c367a27239 | 314 | set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
mauro1019 | 2:27c367a27239 | 315 | */ |
mauro1019 | 0:1e3ed371dacf | 316 | ///////////////////////////////////////////////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 317 | /////LOOP PRINCIPAL/////// |
mauro1019 | 0:1e3ed371dacf | 318 | while (true) { |
mauro1019 | 0:1e3ed371dacf | 319 | |
mauro1019 | 0:1e3ed371dacf | 320 | std::vector<uint8_t> tx_data; |
mauro1019 | 0:1e3ed371dacf | 321 | |
mauro1019 | 0:1e3ed371dacf | 322 | /////////////ESTADO - SLEEP |
mauro1019 | 0:1e3ed371dacf | 323 | if (estado == 0) { |
mauro1019 | 0:1e3ed371dacf | 324 | pc.printf("modo sleep\n"); |
mauro1019 | 2:27c367a27239 | 325 | |
mauro1019 | 0:1e3ed371dacf | 326 | //rutina modo sleep |
mauro1019 | 2:27c367a27239 | 327 | while (!mypin) { // Si no hay tips el micro se queda en este while |
mauro1019 | 2:27c367a27239 | 328 | pc.printf("sleep\n"); |
mauro1019 | 2:27c367a27239 | 329 | ThisThread::sleep_for(5000); //Dormir por 5 segundos |
mauro1019 | 2:27c367a27239 | 330 | } |
mauro1019 | 0:1e3ed371dacf | 331 | |
mauro1019 | 0:1e3ed371dacf | 332 | estado = 3; |
mauro1019 | 2:27c367a27239 | 333 | |
mauro1019 | 0:1e3ed371dacf | 334 | |
mauro1019 | 0:1e3ed371dacf | 335 | ////////////ESTADO - SENSAR |
mauro1019 | 0:1e3ed371dacf | 336 | } else if (estado == 1) { |
mauro1019 | 0:1e3ed371dacf | 337 | |
mauro1019 | 0:1e3ed371dacf | 338 | pc.printf("modo sensor\n"); |
mauro1019 | 0:1e3ed371dacf | 339 | rain = 0; |
mauro1019 | 0:1e3ed371dacf | 340 | |
mauro1019 | 0:1e3ed371dacf | 341 | // get some dummy data and send it to the gateway |
mauro1019 | 2:27c367a27239 | 342 | //analog_sensor = pot.read_u16(); // leo sensor analogico |
mauro1019 | 0:1e3ed371dacf | 343 | |
mauro1019 | 0:1e3ed371dacf | 344 | flipper.attach(&flip, 10.0); // 60 // Tiempo de muestreo de pluviometro |
mauro1019 | 0:1e3ed371dacf | 345 | tick_count = false; |
mauro1019 | 0:1e3ed371dacf | 346 | |
mauro1019 | 0:1e3ed371dacf | 347 | // Mientras tick_count=false se queda en el while contando ticks que se |
mauro1019 | 0:1e3ed371dacf | 348 | // alamacenan en rain |
mauro1019 | 0:1e3ed371dacf | 349 | pc.printf("\n Registrando lluvia"); // 0x%04X |
mauro1019 | 0:1e3ed371dacf | 350 | while (!tick_count) { |
mauro1019 | 0:1e3ed371dacf | 351 | if (mypin) { |
mauro1019 | 0:1e3ed371dacf | 352 | //pc.printf("\nticks %d", rain); // 0x%04X |
mauro1019 | 0:1e3ed371dacf | 353 | led1=!led1; |
mauro1019 | 0:1e3ed371dacf | 354 | rain += 1; |
mauro1019 | 0:1e3ed371dacf | 355 | } |
mauro1019 | 0:1e3ed371dacf | 356 | wait(0.2); |
mauro1019 | 0:1e3ed371dacf | 357 | } |
mauro1019 | 0:1e3ed371dacf | 358 | estado = 2; // ir a envio de datos |
mauro1019 | 0:1e3ed371dacf | 359 | pc.printf("Estado %d", estado); |
mauro1019 | 0:1e3ed371dacf | 360 | pc.printf("Counter: %d [0x%04X]", rain, rain); // 0x%04X |
mauro1019 | 0:1e3ed371dacf | 361 | pc.printf("Sensor: %d [0x%04X]", analog_sensor, analog_sensor); // 0x%04X |
mauro1019 | 0:1e3ed371dacf | 362 | |
mauro1019 | 0:1e3ed371dacf | 363 | ////////////ESTADO GUARDAR Y ENVIAR DATOS |
mauro1019 | 0:1e3ed371dacf | 364 | } else if (estado == 2) { |
mauro1019 | 0:1e3ed371dacf | 365 | |
mauro1019 | 0:1e3ed371dacf | 366 | pc.printf("Mounting the filesystem... "); |
mauro1019 | 0:1e3ed371dacf | 367 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 368 | /////////////////MONTO EL SISTEMA DE ARCHIVOS/////////////////////// |
mauro1019 | 0:1e3ed371dacf | 369 | err = fileSystem.mount(&blockDevice); |
mauro1019 | 0:1e3ed371dacf | 370 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 371 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 372 | // Reformat if we can't mount the filesystem |
mauro1019 | 0:1e3ed371dacf | 373 | // this should only happen on the first boot |
mauro1019 | 0:1e3ed371dacf | 374 | pc.printf("No filesystem found, formatting... "); |
mauro1019 | 0:1e3ed371dacf | 375 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 376 | err = fileSystem.reformat(&blockDevice); |
mauro1019 | 0:1e3ed371dacf | 377 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 378 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 379 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 380 | } |
mauro1019 | 0:1e3ed371dacf | 381 | } |
mauro1019 | 0:1e3ed371dacf | 382 | |
mauro1019 | 0:1e3ed371dacf | 383 | //////////////////Abro el archivo pluv.txt//////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 384 | |
mauro1019 | 0:1e3ed371dacf | 385 | pc.printf("Opening \"/fs/pluv.txt\"... "); |
mauro1019 | 0:1e3ed371dacf | 386 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 387 | f = fopen("/fs/pluv.txt", "r+"); |
mauro1019 | 0:1e3ed371dacf | 388 | pc.printf("%s\n", (!f ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 389 | // pc.printf("modo LoRa_TX\n"); |
mauro1019 | 0:1e3ed371dacf | 390 | |
mauro1019 | 0:1e3ed371dacf | 391 | /////////////////////////////////////////////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 392 | pc.printf("Guardando datos en SD"); |
mauro1019 | 0:1e3ed371dacf | 393 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 394 | //////////////////////////////////////////////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 395 | pos = ftell(f); |
mauro1019 | 0:1e3ed371dacf | 396 | err = fseek(f, pos, SEEK_END); |
mauro1019 | 0:1e3ed371dacf | 397 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 398 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 399 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 400 | } |
mauro1019 | 0:1e3ed371dacf | 401 | err = fprintf(f, "\n;Rain: %d", rain); |
mauro1019 | 0:1e3ed371dacf | 402 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 403 | pc.printf("Fail :(\n"); |
mauro1019 | 0:1e3ed371dacf | 404 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 405 | } |
mauro1019 | 0:1e3ed371dacf | 406 | //////////////////////////////////////////////////////////////////////////////// |
mauro1019 | 0:1e3ed371dacf | 407 | |
mauro1019 | 0:1e3ed371dacf | 408 | /* |
mauro1019 | 0:1e3ed371dacf | 409 | pc.printf("\nSeeking file... "); |
mauro1019 | 0:1e3ed371dacf | 410 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 411 | err = fseek(f, 0, SEEK_SET); |
mauro1019 | 0:1e3ed371dacf | 412 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 413 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 414 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 415 | } |
mauro1019 | 0:1e3ed371dacf | 416 | */ |
mauro1019 | 0:1e3ed371dacf | 417 | pc.printf("\rSaving OK... OK\n"); |
mauro1019 | 0:1e3ed371dacf | 418 | pc.printf("Closing \"/fs/pluv.txt\"... "); |
mauro1019 | 0:1e3ed371dacf | 419 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 420 | err = fclose(f); |
mauro1019 | 0:1e3ed371dacf | 421 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 422 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 423 | error("error: %s (%d)\n", strerror(errno), -errno); |
mauro1019 | 0:1e3ed371dacf | 424 | } |
mauro1019 | 0:1e3ed371dacf | 425 | |
mauro1019 | 0:1e3ed371dacf | 426 | pc.printf("Unmounting... "); |
mauro1019 | 0:1e3ed371dacf | 427 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 428 | err = fileSystem.unmount(); |
mauro1019 | 0:1e3ed371dacf | 429 | pc.printf("%s\n", (err < 0 ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 430 | if (err < 0) { |
mauro1019 | 0:1e3ed371dacf | 431 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 432 | } |
mauro1019 | 0:1e3ed371dacf | 433 | |
mauro1019 | 0:1e3ed371dacf | 434 | pc.printf("Initializing the block device... "); |
mauro1019 | 0:1e3ed371dacf | 435 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 436 | |
mauro1019 | 0:1e3ed371dacf | 437 | err = blockDevice.init(); |
mauro1019 | 0:1e3ed371dacf | 438 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 439 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 440 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 441 | } |
mauro1019 | 0:1e3ed371dacf | 442 | |
mauro1019 | 0:1e3ed371dacf | 443 | pc.printf("Erasing the block device... "); |
mauro1019 | 0:1e3ed371dacf | 444 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 445 | err = blockDevice.erase(0, blockDevice.size()); |
mauro1019 | 0:1e3ed371dacf | 446 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 447 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 448 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 449 | } |
mauro1019 | 0:1e3ed371dacf | 450 | |
mauro1019 | 0:1e3ed371dacf | 451 | pc.printf("Deinitializing the block device... "); |
mauro1019 | 0:1e3ed371dacf | 452 | fflush(stdout); |
mauro1019 | 0:1e3ed371dacf | 453 | err = blockDevice.deinit(); |
mauro1019 | 0:1e3ed371dacf | 454 | pc.printf("%s\n", (err ? "Fail :(" : "OK")); |
mauro1019 | 0:1e3ed371dacf | 455 | if (err) { |
mauro1019 | 0:1e3ed371dacf | 456 | error("error: %s (%d)\n", strerror(-err), err); |
mauro1019 | 0:1e3ed371dacf | 457 | } |
mauro1019 | 0:1e3ed371dacf | 458 | |
mauro1019 | 0:1e3ed371dacf | 459 | pc.printf("\r\n"); |
mauro1019 | 0:1e3ed371dacf | 460 | |
mauro1019 | 0:1e3ed371dacf | 461 | pc.printf("Mbed OS filesystem example done!\n"); |
mauro1019 | 0:1e3ed371dacf | 462 | |
mauro1019 | 0:1e3ed371dacf | 463 | // Acomodo la trama |
mauro1019 | 0:1e3ed371dacf | 464 | // tx_data.push_back((analog_sensor >> 8) & 0xFF); |
mauro1019 | 0:1e3ed371dacf | 465 | // tx_data.push_back(analog_sensor & 0xFF); |
mauro1019 | 0:1e3ed371dacf | 466 | // tx_data.push_back((rain >> 8) & 0xFF); |
mauro1019 | 0:1e3ed371dacf | 467 | // tx_data.push_back(rain & 0xFF); |
mauro1019 | 0:1e3ed371dacf | 468 | |
mauro1019 | 0:1e3ed371dacf | 469 | // Envio Data |
mauro1019 | 0:1e3ed371dacf | 470 | // send_data(tx_data); |
mauro1019 | 0:1e3ed371dacf | 471 | |
mauro1019 | 0:1e3ed371dacf | 472 | estado = 3; |
mauro1019 | 0:1e3ed371dacf | 473 | pc.printf("Estado %d", estado); |
mauro1019 | 0:1e3ed371dacf | 474 | |
mauro1019 | 0:1e3ed371dacf | 475 | } else if (estado == 3) { |
mauro1019 | 0:1e3ed371dacf | 476 | |
mauro1019 | 0:1e3ed371dacf | 477 | flipper.attach(&timer60seg, 10.0); // 60 // setup flipper to call timer60seg after 60 seconds |
mauro1019 | 0:1e3ed371dacf | 478 | pc.printf("\nVerifiacando si esta lloviendo"); |
mauro1019 | 0:1e3ed371dacf | 479 | while (!timer60s) { |
mauro1019 | 0:1e3ed371dacf | 480 | if (mypin) { |
mauro1019 | 0:1e3ed371dacf | 481 | pc.printf("..."); // 0x%04X |
mauro1019 | 0:1e3ed371dacf | 482 | rain += 1; |
mauro1019 | 0:1e3ed371dacf | 483 | } |
mauro1019 | 0:1e3ed371dacf | 484 | if (rain > 2) { |
mauro1019 | 0:1e3ed371dacf | 485 | estado = 1; |
mauro1019 | 0:1e3ed371dacf | 486 | break; |
mauro1019 | 0:1e3ed371dacf | 487 | } |
mauro1019 | 0:1e3ed371dacf | 488 | wait(0.2); |
mauro1019 | 0:1e3ed371dacf | 489 | } |
mauro1019 | 0:1e3ed371dacf | 490 | if (timer60s && rain == 0) { |
mauro1019 | 0:1e3ed371dacf | 491 | estado = 0; // Ir a modo Sleep |
mauro1019 | 0:1e3ed371dacf | 492 | } else { |
mauro1019 | 0:1e3ed371dacf | 493 | estado = estado; |
mauro1019 | 0:1e3ed371dacf | 494 | } |
mauro1019 | 0:1e3ed371dacf | 495 | |
mauro1019 | 0:1e3ed371dacf | 496 | timer60s = false; |
mauro1019 | 0:1e3ed371dacf | 497 | } |
mauro1019 | 0:1e3ed371dacf | 498 | } |
mauro1019 | 0:1e3ed371dacf | 499 | |
mauro1019 | 0:1e3ed371dacf | 500 | return 0; |
mauro1019 | 0:1e3ed371dacf | 501 | } |