Advertisement
QwertyAvatar

Internet rzeczy

Apr 27th, 2024 (edited)
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 14.57 KB | Software | 0 0
  1. //1
  2.  
  3. #include <SPI.h>
  4. #include <TFT_eSPI.h>
  5. TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
  6. #define COLORT1 TFT_BLACK
  7. #define COLORT2 TFT_GREEN
  8. #define COLORT3 TFT_RED
  9. float bmpT,bmpP,bmpH;
  10. #include <Wire.h>
  11. //I2C
  12. #include <Adafruit_BME280.h>
  13. Adafruit_BME280 bme;
  14. void setup() {
  15.    Serial.begin(115200);
  16.    delay(1000);
  17.    tft.init();
  18.    tft.setRotation(0);
  19.    tft.fillScreen(COLORT1);
  20.    tft.setTextSize(2);
  21.    Serial.println();
  22.    Serial.print("TFT Init OK");
  23.  
  24.    Serial.begin(115200);
  25.    delay(1000);
  26.    if (!bme.begin()) {
  27.    Serial.println("Could not find a valid BME280 sensor, check wiring!");
  28.    }
  29.    else {
  30.    Serial.println("BME280 sensor OK");
  31.    bmpT=bme.readTemperature();
  32.    bmpH=bme.readHumidity();
  33.    bmpP=bme.readPressure()/100.0;
  34.    Serial.print("Temperature : "); Serial.println(bmpT);
  35.    Serial.print("Humidity = "); Serial.println(bmpH);
  36.    Serial.print("Pressure = "); Serial.println(bmpP);
  37.  }
  38. }
  39.  
  40. void loop() {
  41.   uint16_t counter = 0;
  42. while(1) {
  43.     tft.drawRect(-5,-5, 200, 20, COLORT2);
  44.     tft.drawRect(195,-5, 50, 20, COLORT2);
  45.  
  46.     // POMIARY
  47.     tft.drawRect(-5,15, 130, 30, COLORT2);
  48.     tft.drawRect(125,15, 130, 30, COLORT2);
  49.     // CONTENT
  50.     tft.drawRect(-5,45, 130, 220, COLORT2);
  51.     tft.drawRect(125,45, 130, 220, COLORT2);
  52.     tft.setTextColor(COLORT3);
  53.     tft.drawString("Laboratorium 2",30,280, 2);
  54.  
  55.     tft.setTextColor(COLORT2);
  56.     tft.drawString("Pomiar 1",10,23, 1);
  57.  
  58.       counter++;
  59.       bmpT=bme.readTemperature();
  60.      bmpH=bme.readHumidity();
  61.      bmpP=bme.readPressure()/100.0;
  62.       char napis[20];
  63.       sprintf(napis, "%.2f", bmpT);
  64.       char napis2[20];
  65.       sprintf(napis2, "%.2f", bmpH);
  66.       char napis3[20];
  67.       sprintf(napis3, "%.2f", bmpP);
  68.       tft.drawString(napis,10,60, 1);
  69.       tft.drawString("C",70,60, 1);
  70.       tft.drawString(napis2,10,80, 1);
  71.       tft.drawString("%",70,80, 1);
  72.       tft.drawString(napis3,10,100, 1);
  73.       tft.drawString("hPa",85,100, 1);
  74.       char nap[20];
  75.       sprintf(nap, "%d", counter);
  76.       tft.drawString(nap,135,23, 1);
  77.       delay(2000);
  78.       tft.fillScreen(COLORT1);
  79.     }
  80. }
  81.  
  82. //2.1
  83.  
  84. #include <SPI.h>
  85. #include <TFT_eSPI.h>
  86. TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
  87. #define COLORT1 TFT_BLACK
  88. #define COLORT2 TFT_GREEN
  89. #define COLORT3 TFT_RED
  90. float bmpT,bmpP,bmpH;
  91. #include <Wire.h>
  92. //I2C
  93. #include <Adafruit_BME280.h>
  94. Adafruit_BME280 bme;
  95.  
  96. #include <DS18B20.h> //https://github.com/matmunk/DS18B20
  97. DS18B20 ds(14); // pin GPIO14
  98. uint8_t address[8];
  99. void setup() {
  100.    Serial.begin(115200);
  101.    delay(1000);
  102.    tft.init();
  103.    tft.setRotation(0);
  104.    tft.fillScreen(COLORT1);
  105.    tft.setTextSize(2);
  106.    Serial.println();
  107.    Serial.print("TFT Init OK");
  108.  
  109.    Serial.begin(115200);
  110.    delay(1000);
  111.    if (!bme.begin()) {
  112.    Serial.println("Could not find a valid BME280 sensor, check wiring!");
  113.    }
  114.    else {
  115.    Serial.println("BME280 sensor OK");
  116.    bmpT=bme.readTemperature();
  117.    bmpH=bme.readHumidity();
  118.    bmpP=bme.readPressure()/100.0;
  119.    Serial.print("Temperature : "); Serial.println(bmpT);
  120.    Serial.print("Humidity = "); Serial.println(bmpH);
  121.    Serial.print("Pressure = "); Serial.println(bmpP);
  122.     }
  123. }
  124.  
  125. void loop() {
  126.   uint16_t counter = 0;
  127.   while(1) {
  128.     tft.drawRect(-5,-5, 200, 20, COLORT2);
  129.     tft.drawRect(195,-5, 50, 20, COLORT2);
  130.  
  131.     // POMIARY
  132.     tft.drawRect(-5,15, 130, 30, COLORT2);
  133.     tft.drawRect(125,15, 130, 30, COLORT2);
  134.     // CONTENT
  135.     tft.drawRect(-5,45, 260, 220, COLORT2);
  136.  
  137.     tft.setTextColor(COLORT3);
  138.     tft.drawString("Laboratorium 4",30,280, 2);
  139.     counter++;
  140.     tft.setTextColor(COLORT2);
  141.     tft.drawString("Pomiar:",10,23, 1);
  142.     char nap[20];
  143.     sprintf(nap, "%d", counter);
  144.     tft.drawString(nap,95,23, 1);
  145.     float temp = ds.getTempC();
  146.     char tempS[20];
  147.     sprintf(tempS, "%.2f", temp);
  148.     tft.drawString(tempS,20,60, 1);
  149.     tft.drawString("C",85,60, 1);
  150.     uint8_t add[8];
  151.  
  152.     ds.getAddress(add);
  153.  
  154.     tft.setCursor(20, 80, 1);
  155.     for (byte i = 0; i < 8; i++) {
  156.      tft.print(add[i],HEX);
  157.      }
  158.     delay(2000);
  159.     tft.fillScreen(COLORT1);
  160.     }
  161. }
  162.  
  163. //2.2
  164.  
  165. #include <SPI.h>
  166. #include <TFT_eSPI.h>
  167. TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
  168. #define COLORT1 TFT_BLACK
  169. #define COLORT2 TFT_GREEN
  170. #define COLORT3 TFT_RED
  171. float bmpT,bmpP,bmpH;
  172. #include <Wire.h>
  173. //I2C
  174. #include <Adafruit_BME280.h>
  175. Adafruit_BME280 bme;
  176.  
  177. #include <DS18B20.h> //https://github.com/matmunk/DS18B20
  178. DS18B20 ds(14); // pin GPIO14
  179. uint8_t address[8];
  180. void setup() {
  181.    Serial.begin(115200);
  182.    delay(1000);
  183.    tft.init();
  184.    tft.setRotation(0);
  185.    tft.fillScreen(COLORT1);
  186.    tft.setTextSize(2);
  187.    Serial.println();
  188.    Serial.print("TFT Init OK");
  189.  
  190.    Serial.begin(115200);
  191.    delay(1000);
  192.    if (!bme.begin()) {
  193.    Serial.println("Could not find a valid BME280 sensor, check wiring!");
  194.    }
  195.    else {
  196.    Serial.println("BME280 sensor OK");
  197.    bmpT=bme.readTemperature();
  198.    bmpH=bme.readHumidity();
  199.    bmpP=bme.readPressure()/100.0;
  200.    Serial.print("Temperature : "); Serial.println(bmpT);
  201.    Serial.print("Humidity = "); Serial.println(bmpH);
  202.    Serial.print("Pressure = "); Serial.println(bmpP);
  203.     }
  204. }
  205.  
  206. void loop() {
  207.   uint16_t counter = 0;
  208.   while(1) {
  209.     tft.drawRect(-5,-5, 200, 20, COLORT2);
  210.     tft.drawRect(195,-5, 50, 20, COLORT2);
  211.  
  212.     // POMIARY
  213.     tft.drawRect(-5,15, 130, 30, COLORT2);
  214.     tft.drawRect(125,15, 130, 30, COLORT2);
  215.     // CONTENT
  216.     tft.drawRect(-5,45, 260, 220, COLORT2);
  217.  
  218.     tft.setTextColor(COLORT3);
  219.     tft.drawString("Laboratorium 4",30,280, 2);
  220.     counter++;
  221.     tft.setTextColor(COLORT2);
  222.     tft.drawString("Pomiar:",10,23, 1);
  223.     char nap[20];
  224.     sprintf(nap, "%d", counter);
  225.     tft.drawString(nap,95,23, 1);
  226.     float temp = ds.getTempC();
  227.     char tempS[20];
  228.     sprintf(tempS, "%.2f", temp);
  229.     tft.drawString(tempS,20,60, 1);
  230.     tft.drawString("C",85,60, 1);
  231.     uint8_t add[8];
  232.  
  233.     ds.getAddress(add);
  234.  
  235.     tft.setCursor(20, 80, 1);
  236.     for (byte i = 0; i < 8; i++) {
  237.      tft.print(add[i],HEX);
  238.      }
  239.  
  240.      ds.selectNext();
  241.     ds.getAddress(add);
  242.      tft.setCursor(20, 130, 1);
  243.     for (byte i = 0; i < 8; i++) {
  244.      tft.print(add[i],HEX);
  245.      }
  246.     delay(2000);
  247.     tft.fillScreen(COLORT1);
  248.     }
  249. }
  250.  
  251. //3
  252.  
  253. #define tms 10000000
  254. RTC_DATA_ATTR int bootCount = 0;
  255.  
  256. void setup(){
  257.  Serial.begin(115200);
  258.  delay(1000); //Take some time to open up the Serial Monitor
  259.  ++bootCount;
  260.  Serial.println("Boot number: " + String(bootCount));
  261.  print_wakeup_reason();
  262.  esp_sleep_enable_ext0_wakeup(GPIO_NUM_2,1);
  263.  esp_sleep_enable_ext0_wakeup(GPIO_NUM_15,1);//1 = High, 0 = Low
  264.   esp_sleep_enable_timer_wakeup(tms);
  265.  Serial.println("Going to sleep now");
  266.  delay(1000);
  267.  Serial.flush();
  268.  esp_deep_sleep_start();
  269.  Serial.println("This will never be printed");
  270. }
  271. void loop(){
  272.  //This is not going to be called
  273. }
  274.  
  275. void print_wakeup_reason(){
  276.  esp_sleep_wakeup_cause_t w_r;
  277.  w_r = esp_sleep_get_wakeup_cause();
  278.  switch(w_r)
  279.  {
  280.  case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup using RTC_GPIO");
  281. break;
  282.  case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup using RTC_CNTL");
  283. break;
  284.  case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer");
  285. break;
  286.  case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
  287.  case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program");
  288. break;
  289.  default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",w_r);
  290. break;
  291.  }
  292. }
  293.  
  294. //4
  295.  
  296. #include <WiFi.h>
  297. #include <ESPAsyncWebServer.h>
  298. #include <ArduinoJson.h>
  299. #include <SPIFFS.h>
  300.  
  301. AsyncWebServer server(80);
  302.  
  303. const char* ssid = "realme GT 5G _";
  304. const char* password = "essa42069";
  305.  
  306. void setup() {
  307.   Serial.begin(115200);
  308.   WiFi.mode(WIFI_STA);
  309.   WiFi.begin(ssid, password);
  310.   if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  311.     Serial.printf("WiFi Failed!\n");
  312.     return;
  313.   }
  314.   if (!SPIFFS.begin()) {
  315.     Serial.println("SPIFFS Mount Failed, formatting!");
  316.     SPIFFS.format();
  317.     if (!SPIFFS.begin()) {
  318.       Serial.println("SPIFFS Mount Failed after formatting, throw.");
  319.       return;
  320.     }
  321.   }
  322.  
  323.   Serial.print("IP Address: ");
  324.   Serial.println(WiFi.localIP());
  325.  
  326.   server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  327.     request->send(200, "text/html", listFiles());
  328.   });
  329.  
  330.  
  331.   server.on("/update", HTTP_GET, [](AsyncWebServerRequest * request) {
  332.     DynamicJsonDocument doc(512);
  333.     File f;
  334.  
  335.     if (SPIFFS.exists("/data.json")) {
  336.       f = SPIFFS.open("/data.json", "r");
  337.       deserializeJson(doc, f);
  338.       f.close();
  339.     }
  340.  
  341.     JsonObject e = doc.createNestedObject();
  342.     e["timestamp"] = millis();
  343.     e["data"] = random(0, 100);
  344.  
  345.     f = SPIFFS.open("/data.json", "w");
  346.     serializeJson(doc, f);
  347.     f.close();
  348.  
  349.     request->send(200, "text/plain", "OK");
  350.   });
  351.  
  352.   server.on("/upload", HTTP_GET, [](AsyncWebServerRequest * request) {
  353.     request->send(200, "text/html", "<form method='POST' action='/upload' enctype='multipart/form-data'><input type='file' name='upload'><input type='submit' value='upload'></form>");
  354.   });
  355.  
  356.   server.on("/upload", HTTP_POST, [](AsyncWebServerRequest * request) {
  357.     request->send(200);
  358.   }, handleUpload);
  359.  
  360.   server.on("/file", HTTP_GET, [](AsyncWebServerRequest * request) {
  361.     request->send(SPIFFS, "/data.json", "application/json");
  362.   });
  363.  
  364.   server.serveStatic("/www/", SPIFFS, "/");
  365.   server.begin();
  366. }
  367.  
  368. void loop() {
  369. }
  370.  
  371. void appendFile(fs::FS &fs, const char * path, const char * message) {
  372.   Serial.printf("Appending to file: %s\r\n", path);
  373.  
  374.   File file = fs.open(path, FILE_APPEND);
  375.   if (!file) {
  376.     Serial.println("- failed to open file for appending");
  377.     return;
  378.   }
  379.   if (file.print(message)) {
  380.     Serial.println("- message appended");
  381.   } else {
  382.     Serial.println("- append failed");
  383.   }
  384.   file.close();
  385. }
  386.  
  387. void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  388.   String logmessage = "Client:" + request->client()->remoteIP().toString() + " " + request->url();
  389.   Serial.println(logmessage);
  390.  
  391.   if (!index) {
  392.     logmessage = "Upload Start: " + String(filename);
  393.     // open the file on first call and store the file handle in the request object
  394.     request->_tempFile = SPIFFS.open("/" + filename, "w");
  395.     Serial.println(logmessage);
  396.   }
  397.  
  398.   if (len) {
  399.     // stream the incoming chunk to the opened file
  400.     request->_tempFile.write(data, len);
  401.     logmessage = "Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
  402.     Serial.println(logmessage);
  403.   }
  404.  
  405.   if (final) {
  406.     logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
  407.     // close the file handle as the upload is now done
  408.     request->_tempFile.close();
  409.     Serial.println(logmessage);
  410.     request->redirect("/");
  411.   }
  412. }
  413.  
  414. // Make size of files human readable
  415. // source: https://github.com/CelliesProjects/minimalUploadAuthESP32
  416. String humanReadableSize(const size_t bytes) {
  417.   if (bytes < 1024) return String(bytes) + " B";
  418.   else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
  419.   else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
  420.   else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
  421. }
  422.  
  423. String listFiles() {
  424.   String returnText = "";
  425.   Serial.println("Listing files stored on SPIFFS");
  426.   File root = SPIFFS.open("/");
  427.   File foundfile = root.openNextFile();
  428.   returnText += "<table><tr><th align='left'>Name</th><th align='left'>Size</th></tr>";
  429.  
  430.   while (foundfile) {
  431.     returnText += "<tr align='left'><td><a href=\"/www/" + String(foundfile.name()) + "\">" + String(foundfile.name()) + "</a></td><td>" + humanReadableSize(foundfile.size()) + "</td></tr>";
  432.     foundfile = root.openNextFile();
  433.   }
  434.  
  435.   returnText += "</table>";
  436.  
  437.   returnText += "<br><a href=\"/upload\">Upload new file...</a>";
  438.  
  439.   root.close();
  440.   foundfile.close();
  441.   return returnText;
  442. }
  443.  
  444. //5.2
  445. #include <BLEDevice.h>
  446. #include <BLEUtils.h>
  447. #include <BLEServer.h>
  448. #include <BLE2902.h>
  449.  
  450. #define SERVICE_UUID BLEUUID((uint16_t)0x180F)
  451. #define CHARACTERISTIC_UUID BLEUUID((uint16_t)0x2A19)
  452. #define DESCRIPTOR_UUID BLEUUID((uint16_t)0x2901)
  453.  
  454. BLECharacteristic BatteryLevelCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
  455. BLEDescriptor BatteryLevelDescriptor(DESCRIPTOR_UUID);
  456.  
  457. void setup()
  458. {
  459.   Serial.begin(115200);
  460.   Serial.println("Starting BLE work!");
  461.  
  462.   BLEDevice::init("QwertyAvatar");
  463.  
  464.   BLEServer *pServer = BLEDevice::createServer();
  465.   BLEService *pService = pServer->createService(SERVICE_UUID);
  466.   pService->addCharacteristic(&BatteryLevelCharacteristic);
  467.   BatteryLevelDescriptor.setValue("Percentage 0 - 100");
  468.   BatteryLevelCharacteristic.addDescriptor(&BatteryLevelDescriptor);
  469.   BatteryLevelCharacteristic.addDescriptor(new BLE2902());
  470.   pService->start();
  471.  
  472.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  473.   pAdvertising->addServiceUUID(SERVICE_UUID);
  474.   pAdvertising->setScanResponse(true);
  475.   pAdvertising->setMinPreferred(0x06);
  476.   pAdvertising->setMinPreferred(0x12);
  477.  
  478.   BLEDevice::startAdvertising();
  479.  
  480.   Serial.println("Advertisement started!");
  481. }
  482.  
  483. uint8_t level = 3;
  484.  
  485. void loop()
  486. {
  487.   BatteryLevelCharacteristic.setValue(&level, 1);
  488.   BatteryLevelCharacteristic.notify();
  489.  
  490.   delay(5000);
  491.   level -= 1;
  492.   if(level == 0) {
  493.     level = 100;
  494.   }
  495.  
  496. }
  497.  
  498.  
  499. /// 5.3
  500.  
  501.  
  502. #include <BLEDevice.h>
  503. #include <BLEUtils.h>
  504. #include <BLEScan.h>
  505. #include <BLEAdvertisedDevice.h>
  506.  
  507. int scanTime = 5; // In seconds
  508.  
  509. BLEScan *pBLEScan;
  510.  
  511. class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
  512. {
  513.   void onResult(BLEAdvertisedDevice advertisedDevice)
  514.   {
  515.     Serial.printf("Advertised Device: %s, address: %s, signal strength: %s dBm\n", advertisedDevice.getName().c_str(), advertisedDevice.getAddress().toString().c_str(), String(advertisedDevice.getRSSI()));
  516.   }
  517. };
  518.  
  519. void setup()
  520. {
  521.   Serial.begin(115200);
  522.   Serial.println("Scanning...");
  523.  
  524.   BLEDevice::init("QwertyAvatar");
  525.   pBLEScan = BLEDevice::getScan();
  526.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  527.   pBLEScan->setActiveScan(true);
  528.  
  529.   pBLEScan->setInterval(100);
  530.   pBLEScan->setWindow(99);
  531. }
  532.  
  533. void loop()
  534. {
  535.   BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  536.  
  537.   Serial.print("Devices found: ");
  538.   Serial.println(foundDevices.getCount());
  539.   Serial.println("Scan done!");
  540.  
  541.   pBLEScan->clearResults();
  542.  
  543.   delay(2000);
  544. }
  545.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement