![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
laskee ihmisiä
Dependencies: Adafruit-GFX-MbedOS6 Adafruit_SSD1331_MbedOS6
main.cpp@15:9099657f0505, 2020-10-20 (annotated)
- Committer:
- koodimuikku
- Date:
- Tue Oct 20 09:19:09 2020 +0000
- Revision:
- 15:9099657f0505
- Parent:
- 14:7e18419e3bc2
jee
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
koodimuikku | 0:8d549d45642e | 1 | #include "mbed.h" |
koodimuikku | 3:55afdc7fb4f2 | 2 | #include "ESP8266Interface.h" |
koodimuikku | 3:55afdc7fb4f2 | 3 | #include <MQTTClientMbedOs.h> |
koodimuikku | 7:d6cbb33f21fd | 4 | #include "Adafruit_SSD1331.h" |
koodimuikku | 7:d6cbb33f21fd | 5 | #include "Adafruit_GFX.h" |
vaeries | 6:e5fdc96a8310 | 6 | |
koodimuikku | 3:55afdc7fb4f2 | 7 | //Miten pistän kiinni |
vaeries | 6:e5fdc96a8310 | 8 | |
koodimuikku | 3:55afdc7fb4f2 | 9 | //sensori |
koodimuikku | 3:55afdc7fb4f2 | 10 | //r = virta 5v |
koodimuikku | 3:55afdc7fb4f2 | 11 | //b = ground |
koodimuikku | 3:55afdc7fb4f2 | 12 | //w = D6 |
vaeries | 6:e5fdc96a8310 | 13 | |
koodimuikku | 3:55afdc7fb4f2 | 14 | //wifi |
koodimuikku | 3:55afdc7fb4f2 | 15 | //L432KC D5=PB6=UART1TX --- 3 RXD |
koodimuikku | 3:55afdc7fb4f2 | 16 | //L432KC D4=PB7=UART1RX --- 4 TXD |
koodimuikku | 3:55afdc7fb4f2 | 17 | //L432KC 3V3 --- 1 3.3V |
koodimuikku | 3:55afdc7fb4f2 | 18 | //L432KC GND --- 2 GND |
vaeries | 6:e5fdc96a8310 | 19 | |
koodimuikku | 0:8d549d45642e | 20 | DigitalOut led1(LED1); |
koodimuikku | 3:55afdc7fb4f2 | 21 | InterruptIn alarm(D6, PullUp); |
koodimuikku | 7:d6cbb33f21fd | 22 | Adafruit_SSD1331 OLED(D9, D8, D10, D11, NC, D13); |
koodimuikku | 7:d6cbb33f21fd | 23 | DigitalOut VCCEN(D3); |
koodimuikku | 7:d6cbb33f21fd | 24 | DigitalOut PMODEN(D5); |
koodimuikku | 7:d6cbb33f21fd | 25 | #define Black 0x0000 |
koodimuikku | 7:d6cbb33f21fd | 26 | #define Blue 0x001F |
koodimuikku | 7:d6cbb33f21fd | 27 | #define Red 0xF800 |
koodimuikku | 7:d6cbb33f21fd | 28 | #define Green 0x07E0 |
koodimuikku | 7:d6cbb33f21fd | 29 | #define Cyan 0x07FF |
koodimuikku | 7:d6cbb33f21fd | 30 | #define Magenta 0xF81F |
koodimuikku | 7:d6cbb33f21fd | 31 | #define Yellow 0xFFE0 |
koodimuikku | 7:d6cbb33f21fd | 32 | #define White 0xFFFF |
vaeries | 6:e5fdc96a8310 | 33 | |
vaeries | 6:e5fdc96a8310 | 34 | |
koodimuikku | 4:a814f6735023 | 35 | int main() { |
koodimuikku | 0:8d549d45642e | 36 | |
koodimuikku | 8:5aa78bacd78f | 37 | //Tässtä led näyttö kun ohjelma käynnistyy |
koodimuikku | 8:5aa78bacd78f | 38 | OLED.begin(); |
koodimuikku | 8:5aa78bacd78f | 39 | OLED.clearScreen(); |
koodimuikku | 8:5aa78bacd78f | 40 | OLED.fillScreen(Black); |
koodimuikku | 8:5aa78bacd78f | 41 | OLED.setTextColor(White); |
koodimuikku | 8:5aa78bacd78f | 42 | OLED.setCursor(30,25); |
koodimuikku | 8:5aa78bacd78f | 43 | OLED.setTextSize(1); |
koodimuikku | 8:5aa78bacd78f | 44 | OLED.printf("Booting up"); |
koodimuikku | 8:5aa78bacd78f | 45 | |
koodimuikku | 4:a814f6735023 | 46 | //Tästä alkaa wifi ja mqtt hörjöilyt |
koodimuikku | 4:a814f6735023 | 47 | ESP8266Interface esp(MBED_CONF_APP_ESP_TX_PIN, MBED_CONF_APP_ESP_RX_PIN); |
koodimuikku | 3:55afdc7fb4f2 | 48 | |
vaeries | 9:b18fe306d952 | 49 | //Store device and broker IP |
koodimuikku | 3:55afdc7fb4f2 | 50 | SocketAddress deviceIP; |
koodimuikku | 3:55afdc7fb4f2 | 51 | SocketAddress MQTTBroker; |
koodimuikku | 3:55afdc7fb4f2 | 52 | |
koodimuikku | 3:55afdc7fb4f2 | 53 | TCPSocket socket; |
koodimuikku | 3:55afdc7fb4f2 | 54 | MQTTClient client(&socket); |
koodimuikku | 3:55afdc7fb4f2 | 55 | |
vaeries | 9:b18fe306d952 | 56 | printf("[ESP8266Interface.h] Connecting to network interface..."); |
koodimuikku | 3:55afdc7fb4f2 | 57 | int ret = esp.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
koodimuikku | 3:55afdc7fb4f2 | 58 | |
vaeries | 9:b18fe306d952 | 59 | if (ret != 0) { |
vaeries | 9:b18fe306d952 | 60 | printf("\n[ESP8266Interface.h] Connection error!"); |
vaeries | 9:b18fe306d952 | 61 | } else { |
vaeries | 9:b18fe306d952 | 62 | esp.get_ip_address(&deviceIP); |
vaeries | 9:b18fe306d952 | 63 | printf("\n[ESP8266Interface.h] Connected to IP via DHCP: %s", deviceIP.get_ip_address()); |
vaeries | 9:b18fe306d952 | 64 | } |
koodimuikku | 3:55afdc7fb4f2 | 65 | |
koodimuikku | 3:55afdc7fb4f2 | 66 | esp.gethostbyname(MBED_CONF_APP_MQTT_BROKER_HOSTNAME, &MQTTBroker); |
koodimuikku | 3:55afdc7fb4f2 | 67 | MQTTBroker.set_port(MBED_CONF_APP_MQTT_BROKER_PORT); |
vaeries | 6:e5fdc96a8310 | 68 | |
koodimuikku | 3:55afdc7fb4f2 | 69 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
koodimuikku | 3:55afdc7fb4f2 | 70 | data.MQTTVersion = 3; |
koodimuikku | 3:55afdc7fb4f2 | 71 | char *id = MBED_CONF_APP_MQTT_ID; |
koodimuikku | 3:55afdc7fb4f2 | 72 | data.clientID.cstring = id; |
vaeries | 9:b18fe306d952 | 73 | printf("\n[MQTTBroker] ID: %d", id); |
koodimuikku | 3:55afdc7fb4f2 | 74 | |
koodimuikku | 12:91b77eb22511 | 75 | int counter; |
koodimuikku | 12:91b77eb22511 | 76 | counter = 0; |
koodimuikku | 12:91b77eb22511 | 77 | |
koodimuikku | 3:55afdc7fb4f2 | 78 | char buffer[64]; |
koodimuikku | 15:9099657f0505 | 79 | sprintf(buffer, "{\"Count\": \"%d\"}", counter); |
koodimuikku | 13:f4aab60fe740 | 80 | printf("\n[MQTTBroker] Buffer: %u", buffer); |
vaeries | 6:e5fdc96a8310 | 81 | |
koodimuikku | 3:55afdc7fb4f2 | 82 | MQTT::Message msg; |
koodimuikku | 3:55afdc7fb4f2 | 83 | msg.qos = MQTT::QOS0; |
koodimuikku | 3:55afdc7fb4f2 | 84 | msg.retained = false; |
koodimuikku | 3:55afdc7fb4f2 | 85 | msg.dup = false; |
koodimuikku | 13:f4aab60fe740 | 86 | |
vaeries | 6:e5fdc96a8310 | 87 | |
vaeries | 11:0058006732a7 | 88 | printf("Opening socket..."); |
koodimuikku | 3:55afdc7fb4f2 | 89 | socket.open(&esp); |
vaeries | 11:0058006732a7 | 90 | printf("Opened!"); |
koodimuikku | 3:55afdc7fb4f2 | 91 | socket.connect(MQTTBroker); |
vaeries | 11:0058006732a7 | 92 | printf("Opening to MQTT..."); |
vaeries | 9:b18fe306d952 | 93 | client.connect(data); |
vaeries | 11:0058006732a7 | 94 | printf("Opened!"); |
vaeries | 11:0058006732a7 | 95 | |
vaeries | 11:0058006732a7 | 96 | printf("\nReporting the following warnings:\n"); |
vaeries | 11:0058006732a7 | 97 | MBED_WARNING1(MBED_ERROR_TIME_OUT, "Timeout error", 1); |
vaeries | 11:0058006732a7 | 98 | printf(" MBED_ERROR_TIME_OUT Status: 0x%x Value: 0x%x\n", MBED_ERROR_TIME_OUT, 1); |
vaeries | 11:0058006732a7 | 99 | |
koodimuikku | 7:d6cbb33f21fd | 100 | |
koodimuikku | 7:d6cbb33f21fd | 101 | //Tässtä led näyttö alku |
koodimuikku | 12:91b77eb22511 | 102 | |
koodimuikku | 7:d6cbb33f21fd | 103 | OLED.clearScreen(); |
koodimuikku | 7:d6cbb33f21fd | 104 | OLED.fillScreen(Black); |
koodimuikku | 8:5aa78bacd78f | 105 | OLED.setTextColor(Red); |
koodimuikku | 8:5aa78bacd78f | 106 | OLED.setCursor(30,25); |
koodimuikku | 8:5aa78bacd78f | 107 | OLED.setTextSize(3); |
koodimuikku | 8:5aa78bacd78f | 108 | OLED.printf("%d", counter); |
vaeries | 6:e5fdc96a8310 | 109 | |
vaeries | 9:b18fe306d952 | 110 | //Tästä alkaa sensori sensuroinnit |
koodimuikku | 4:a814f6735023 | 111 | while(true) { |
koodimuikku | 4:a814f6735023 | 112 | if (alarm){ |
koodimuikku | 5:fbb8f4417298 | 113 | counter++; |
koodimuikku | 15:9099657f0505 | 114 | printf("\nToimii! Jippii! '%d' \r\n", counter); |
koodimuikku | 7:d6cbb33f21fd | 115 | OLED.fillScreen(Black); |
koodimuikku | 8:5aa78bacd78f | 116 | OLED.setCursor(30,25); |
koodimuikku | 8:5aa78bacd78f | 117 | OLED.printf("%d", counter); |
koodimuikku | 15:9099657f0505 | 118 | sprintf(buffer, "{\"Count\": \"%d\"}", counter); |
koodimuikku | 13:f4aab60fe740 | 119 | msg.payload = (void*)buffer; |
koodimuikku | 13:f4aab60fe740 | 120 | msg.payloadlen = strlen(buffer); |
koodimuikku | 14:7e18419e3bc2 | 121 | ThisThread::sleep_for(1000); |
vaeries | 9:b18fe306d952 | 122 | |
vaeries | 9:b18fe306d952 | 123 | //Tässä laite lähettää mqtt paketin |
vaeries | 9:b18fe306d952 | 124 | client.publish(MBED_CONF_APP_MQTT_TOPIC, msg); |
vaeries | 9:b18fe306d952 | 125 | //client.yield(100); |
koodimuikku | 13:f4aab60fe740 | 126 | //client.disconnect(); |
koodimuikku | 4:a814f6735023 | 127 | } |
koodimuikku | 4:a814f6735023 | 128 | } |
koodimuikku | 0:8d549d45642e | 129 | } |