embeded iot project smart farm
Dependencies: C12832 smartfarm1 DHT
Revision 1:f82f3ec0b1ca, committed 2022-06-13
- Comitter:
- s1710879
- Date:
- Mon Jun 13 09:02:07 2022 +0000
- Parent:
- 0:f31836d48420
- Commit message:
- project
Changed in this revision
diff -r f31836d48420 -r f82f3ec0b1ca C12832.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Mon Jun 13 09:02:07 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/components/code/C12832/#03069e3deaa4
diff -r f31836d48420 -r f82f3ec0b1ca DHT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT.lib Mon Jun 13 09:02:07 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Wimpie/code/DHT/#9b5b3200688f
diff -r f31836d48420 -r f82f3ec0b1ca ESP-call.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP-call.lib Mon Jun 13 09:02:07 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/s1710879/code/smartfarm1/#8ea9795d19b5
diff -r f31836d48420 -r f82f3ec0b1ca main.cpp --- a/main.cpp Sun Mar 29 19:41:28 2020 +0000 +++ b/main.cpp Mon Jun 13 09:02:07 2022 +0000 @@ -1,18 +1,348 @@ #include "mbed.h" +#include "DHT.h" +#include "ESP-call.h" +#include "C12832.h" +#include "pitches.h" +#include "time_struct.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +Mutex mutex; + + +char rain[10]; +char time1[11]; + +TIME current_time = {0,0,0}; +TIME alarm_time = {0,0,0}; + + +DHT dht_sensor(A0, SEN11301P); +AnalogIn illu_sensor(A1); +AnalogIn flame_sensor(A2); +C12832 lcd(D11, D13, D12, D7, D10); // lcd = (MOSI, SCK, RESET, A0, nCS) +PwmOut buzzer(PTA1); // buzzer = PTA1 +DigitalOut red(D4); +DigitalOut yellow(D5); +DigitalOut blue(D6); + + + +void wifiConnect(void) +{ + NodeMCU_PGM http_client[] = { + "", 1, + "wifi.setmode(wifi.STATION)", 1, + "", 1, + "station_cfg={}", 1, + "station_cfg.ssid=\"in\"", 1, + "station_cfg.pwd=\"87654321\"", 1, + "station_cfg.save=false", 1, + "wifi.sta.config(station_cfg)", 1, + "", 1, + "wifi.sta.connect()", 80, // long delay here! + "", 1, + "print(wifi.sta.status())", 1, + "print(wifi.sta.getip())", 1, + NULL, 0, + }; + ESP_call_multi(http_client); +} +int t; +void getWeather(void) +{ + NodeMCU_PGM weather[] = { + "", 1, + "socket=net.createConnection(net.TCP, 0)", 1, + "socket:on(\"receive\", function(sck, c) print(c) end)", 1, + "socket:connect(80,\"api.openweathermap.org\")", 1, + "socket:send(\"GET /data/2.5/weather?id=1835848&APPID=683a6b20feaa45aa163c4b290a4c4110 HTTP/1.1\\r\\nHost: api.openweathermap.org\\r\\nConnection: close\\r\\nAccept: */*\\r\\n\\r\\n\")", 50, + NULL, 0, + }; + ESP_call_multi(weather); + char *plp[2] ={ESP_recv_buffer, 0}; + + plp[1] = strstr(plp[0], "dt\":"); + for(int i =0; i<10; i++) + { + time1[i] = *(plp[1] + i+4); + } + time1[10] = '\0'; + set_time(atoi(time1)); + PC.printf("\r\ntime: %s\r\n", time1); + plp[1] = strstr(plp[0], "main\":\""); + for(int i =0; i<10; i++) + { + if(*(plp[1] + i+7) == '\"') + { + rain[i] = '\0'; + break; + } + rain[i] = *(plp[1] + i+7); + } + ESP.printf("\r\nweather=\"%s\"\r\n", rain); + +} + +void thread_clock(void) +{ + while(true) + { + mutex.lock(); + time_t s = time(NULL); + current_time.s = s%60; + current_time.m = s/60%60; + current_time.h = (s/60/60%24 + 9)%24; + mutex.unlock(); + ThisThread::sleep_for(2000); + } +} +NodeMCU_PGM http_server[] = { + "", 1, + "socket:on(\"receive\", function(sck, c) sck:close() end)", 1, + "temp=0", 1, + "humi=0", 1, + "illu=0", 1, + "flame=0", 1, + "http_resp=\"HTTP/1.0 200 OK\\r\\nContent-Type: text/html\\r\\n\\r\\n\"", 1, + "html_main1 = \"<h1>Embeded project</h1>\"", 1, + "html_weather = \"<h1>weather:%s</h1>\"", 1, + "html_temp = \"<h1>Temperature(C): %4.2f</h1>\"", 1, + "html_humi = \"<h1>Humidity : %4.2f</h1>\"", 1, + "html_illu = \"<h1>Illuminance : %.2f</h1>\"", 1, + "html_flame = \"<h1>Flame : %.2f</h1>\"", 1, + "html_main2 = \"<h1>ALARM</h1><form>\ + <input type=\\\"number\\\" name=\\\"hour\\\" min=\\\"0\\\" max=\\\"23\\\" step=\\\"1\\\">h\ + <input type=\\\"number\\\" name=\\\"min\\\" min=\\\"0\\\" max=\\\"59\\\" step=\\\"1\\\">m\ + <input type=\\\"submit\\\">\ + </form>\"", 1, + "srv = net.createServer(net.TCP)", 1, + "srv:listen(80, function(conn)", 1, + " conn:on(\"receive\", function(sck, payload)", 1, + " print(payload)", 1, + " print(\"~\")", 1, + " sck:send(http_resp)", 1, + " sck:send(html_main1)", 1, + " sck:send(string.format(html_weather, weather))", 1, + " sck:send(html_main2)", 1, + " sck:send(string.format(html_temp, temp))", 1, + " sck:send(string.format(html_humi, humi))", 1, + " sck:send(string.format(html_illu, illu))", 1, + " sck:send(string.format(html_flame, flame))", 1, + " end)", 1, + " conn:on(\"sent\", function(sck) sck:close() end)", 1, + "end)", 1, + NULL, 0, +}; + + + -// https://os.mbed.com/docs/mbed-os/v5.15/tools/creating-a-new-program.html +typedef struct info{ + //DHT.h DHT ReadTemperature(FARENHEIT), ReadHumidity() + float temp; //온도 + float humi; //습도 + //AnalogIn read() + float illu; //조도 + float flame; //불꽃 감지 +}INFO; + +INFO info = {0,0,0,0}; + + +int length = 5; + +float frequency[] = { + NOTE_E6, NOTE_E6, 0, NOTE_E6, 0, NOTE_C6, NOTE_E6, 0, NOTE_G6, 0, 0, + 0, NOTE_G5, 0, 0, 0, NOTE_C6, 0, 0, NOTE_G5, 0, 0, NOTE_E5, 0, 0, + NOTE_A5, 0, NOTE_B5, 0, NOTE_AS5, NOTE_A5, 0, NOTE_G5, NOTE_E6, NOTE_G6, + NOTE_A6, 0, NOTE_F6, NOTE_G6, 0, NOTE_E6, 0, NOTE_C6, NOTE_D6, NOTE_B5, + 0, 0, NOTE_C6, 0, 0, NOTE_G5, 0, 0, NOTE_E5, 0, 0, NOTE_A5, 0, NOTE_B5, + 0, NOTE_AS5, NOTE_A5, 0, NOTE_G5, NOTE_E6, NOTE_G6, NOTE_A6, 0, NOTE_F6, + NOTE_G6, 0, NOTE_E6, 0, NOTE_C6, NOTE_D6, NOTE_B5, 0, 0 +}; + +float beat[] = { + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, + 12, 12, 12, 12, 12, +}; +Thread th1, th2, th3, th4, th5, th6; +void thread_illu(void) +{ + while(true){ + int count = 0; + if(info.temp > 25.0 || info.temp < 10.0) count++; + if(info.humi < 50.0 || info.humi > 90.0) count++; + if(info.illu > 0.8) count++; + + if(count >= 3){ blue = 0.0; yellow = 0.0; red = 1.0; } + else if (count >= 2){ blue = 0.0; yellow = 1.0; red = 0.0; } + else if (count >= 1){ blue = 1.0; yellow = 0.0; red = 0.0; } + else{ blue = 0.0; yellow = 0.0; red = 0.0; } + ThisThread::sleep_for(2000); + } +} + + + +void thread_get_and_print_info(void) +{ -Serial pc(USBTX, USBRX, 115200); // baud rate 115200 -DigitalOut led(LED1); // led = LED1 + int error; + + while(true) + { + mutex.lock(); + error = dht_sensor.readData(); + if(error == 0) + { + info.temp = dht_sensor.ReadTemperature(CELCIUS); + info.humi = dht_sensor.ReadHumidity(); + info.illu = illu_sensor.read(); + info.flame = flame_sensor.read(); + ESP.printf("temp=%4.2f\r\n", info.temp); + ESP.printf("humi=%4.2f\r\n", info.humi); + ESP.printf("illu=%.2f\r\n", info.illu); + ESP.printf("flame=%.2f\r\n", info.flame); + mutex.unlock(); + ThisThread::sleep_for(2000); + } + else + { + info.illu = illu_sensor.read(); + info.flame = flame_sensor.read(); + PC.printf("ERROR\r\n"); + ESP.printf("illu=%.2f\r\n", info.illu); + ESP.printf("flame=%.2f\r\n", info.flame); + mutex.unlock(); + ThisThread::sleep_for(2000); + } + + } +} + +void thread_flame_alarm(void) +{ + while(true) + { + mutex.lock(); + if(info.flame > 0.8) + { + for(int i = 0; i <= length; i++) { + if(frequency[i] == 0) { + buzzer = 0.0; + } else { + buzzer.period(1.0 / frequency[i]); // period = (1.0 / frequency) + buzzer = 0.5; // duty cycle = 50% + } + thread_sleep_for(2500.0 / beat[i]); // duration = (C / beat) ms + } + } + else + buzzer = 0.0; + mutex.unlock(); + + ThisThread::sleep_for(2000); + } +} +void thread_print_lcd(void) +{ + while(true) + { + mutex.lock(); + lcd.locate(0,10); + lcd.printf("C:%d:%d:%d A:%d:%d:%d\r\n", + current_time.h, + current_time.m, + current_time.s, + alarm_time.h, + alarm_time.m, + alarm_time.s); + mutex.unlock(); + ThisThread::sleep_for(1000); + } +} +int length1 = 62; +void thread_water_alarm(void) +{ + while(true) + { + mutex.lock(); + if((alarm_time.h == current_time.h) && (alarm_time.m == current_time.m)) + { + for(int i = 0; i <= length1; i++) { + if(frequency[i] == 0) { + buzzer = 0.0; + } else { + buzzer.period(1.0 / frequency[i]); // period = (1.0 / frequency) + buzzer = 0.5; // duty cycle = 50% + } + thread_sleep_for(2500.0 / beat[i]); // duration = (C / beat) ms + } + } + else + buzzer = 0.0; + mutex.unlock(); + + ThisThread::sleep_for(2000); + } +} int main() { - while (true) { - led = 0; - printf("LED1 is ON\r\n"); - thread_sleep_for(500); - led = 1; - printf("LED1 is OFF\r\n"); - thread_sleep_for(500); + // Config baudrate of PC and ESP + PC.baud(115200); + ESP.baud(115200); + // Reset ESP + PC.printf("\r\nReset ESP...\r\n"); + ESP_reset(); + // Setup ESP noecho mode + PC.printf("Setup ESP noecho...\r\n"); + ESP_noecho(); + // Execute a NodeMCU program + PC.printf("Execute a NodeMCU program...\r\n"); + wifiConnect(); + getWeather(); + ESP_call_multi(http_server); + // Config ESP to PC receive mode + PC.printf("\r\nESP receive mode...\r\n"); + //ESP.attach(&ISR_ESP_to_PC_new, Serial::RxIrq); + + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Embeded project!"); + //thread_sleep_for(5000); + th1.start(thread_get_and_print_info); + th2.start(thread_flame_alarm); + th3.start(thread_clock); + th4.start(thread_print_lcd); + th5.start(thread_illu); + th6.start(thread_water_alarm); + while(1) { + char c; + char *s1; + ESP_recv_buffer_index = 0; + for(;;) { + if(ESP.readable()) { + c = ESP.getc(); + if(c == '~') break; + ESP_recv_buffer[ESP_recv_buffer_index] = c; + ESP_recv_buffer_index++; + } + } + ESP_recv_buffer[ESP_recv_buffer_index] = '\0'; + PC.printf("%s\r\n", ESP_recv_buffer); + if(ESP_recv_buffer_index != 0) { + if((s1 = strstr(ESP_recv_buffer, "hour")) != NULL ) { + alarm_time.h = atoi(s1+5); + } + if((s1 = strstr(ESP_recv_buffer, "min")) != NULL ) { + alarm_time.m = atoi(s1+4); + } + } } } \ No newline at end of file
diff -r f31836d48420 -r f82f3ec0b1ca pitches.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pitches.h Mon Jun 13 09:02:07 2022 +0000 @@ -0,0 +1,89 @@ +// pitches.h +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4434 +#define NOTE_D8 4698 +#define NOTE_DS8 4978
diff -r f31836d48420 -r f82f3ec0b1ca time_struct.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/time_struct.h Mon Jun 13 09:02:07 2022 +0000 @@ -0,0 +1,10 @@ +#ifndef _TIME_STRUCT_H_ +#define _TIME_STRUCT_H_ + +typedef struct time{ + int h; + int m; + int s; +}TIME; + +#endif \ No newline at end of file