Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.73 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <WebServer.h>
  5. #include <Wire.h>
  6. #include <SPI.h>
  7. #include <DHT.h>
  8. #include "CORE_BME280_SPI.h"
  9.  
  10.  
  11. //BME280
  12. //#define SENSE_SCK 5;// Set Serial Clock
  13. //#define SENSE_MISO 2;// Set Serial Data Out
  14. //#define SENSE_MOSI 4;// Set Serial Data In
  15. //#define SENSE_CS 19 ;// Set Chip Select
  16.  BME280_SPI bme(19,4,2,5);
  17.  
  18.  
  19. // SHT25 I2C address is 0x40(64)
  20. #define sht 0x40
  21.  
  22. //DHT11
  23. #define DHTPIN 16
  24. DHT dht11(DHTPIN,DHT11,15);
  25.  
  26. //DHT22
  27. #define DHTPIN 17
  28. DHT dht22(DHTPIN,DHT22,15);
  29.  
  30. WebServer webServer(80);
  31. const char* ssid1 ;
  32. const char* password1 ;
  33. String apiKey ;
  34. const char* server = "api.thingspeak.com";
  35. bool connectedWifi = false;
  36.  
  37. WiFiClient client;
  38.  
  39.  
  40.  
  41.  
  42. //main page
  43. void handleLogin(){
  44.   //String msg;
  45.   if (webServer.hasArg("USERNAME") && webServer.hasArg("PASSWORD")){
  46.     if (webServer.arg("USERNAME") == "admin" &&  webServer.arg("PASSWORD") == "12345678" ){
  47.       webServer.sendHeader("Location","/");
  48.       webServer.sendHeader("Cache-Control","no-cache");
  49.       webServer.sendHeader("Set-Cookie","ESPSESSIONID=1");
  50.       webServer.send(301);
  51.       Serial.println("Log in Successful");
  52.       String ssidL = webServer.arg("SSID");
  53.       String passwordL = webServer.arg("PASSWORD");
  54.       apiKey = webServer.arg("API");
  55.       ssid1 = ssidL.c_str();
  56.       password1 =passwordL.c_str();
  57.   Serial.println("Data recived ");
  58.   Serial.println(ssid1);
  59.   Serial.println(password1);
  60.   Serial.println(apiKey);
  61.   WiFi.mode(WIFI_STA);
  62.   WiFi.begin(ssid1, password1);
  63.   while ( WiFi.status() != WL_CONNECTED )
  64.   {
  65.     vTaskDelay( 250 /portTICK_PERIOD_MS );
  66.     Serial.print( "." );
  67.   }
  68.   Serial.println("Connected to Wifi" );
  69.   connectedWifi=true;
  70.       return;
  71.     }
  72.   //msg = "Wrong username/password! try again.";
  73.   Serial.println("Log in Failed");
  74.   }
  75.   String content = "<html><body><form action='/' method='POST'>Put wifi creditials here and thingSprark Api<br>";
  76.   content += "Ssid:<input type='text' name='SSID' placeholder='ssid'><br>";
  77.   content += "User:<input type='text' name='USERNAME' placeholder='username'><br>";
  78.   content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
  79.   content += "Api:<input type='text' name='API' placeholder='api'><br>";
  80.   content += "<input type='submit' name='SUBMIT' value='Submit'></form><br>";
  81.   //content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
  82.   content += "You also can go <a href='/inline'>here</a></body></html>";
  83.   webServer.send(200, "text/html", content);
  84.    
  85. }
  86.  
  87. //no need authentification
  88. void handleNotFound(){
  89.   String message = "Page Not Found\n\n";
  90.   webServer.send(404, "text/plain", message);
  91. }
  92. const char *ssid = "ESP32ap";
  93. const char *password = "12345678";
  94.  
  95. void setup() {
  96.   Wire.begin();
  97.   Serial.begin(115200);
  98.   //sht
  99.   Wire.beginTransmission(sht);
  100.   Wire.endTransmission();
  101.   delay(300);
  102.   //dht
  103.   dht11.begin();
  104.   dht22.begin();
  105.   //bme
  106.   if (!bme.begin()) {
  107. Serial.println("Error: BME280 sensor, Check Wiring > ");
  108. while (1);
  109. }
  110.  
  111. bme.setTempCal(-1);//Set Sensor was reading high so offset by 1 degree C
  112.  
  113. //wifi
  114.   Serial.println();
  115.   Serial.print("Configuring access point...");
  116.   /* You can remove the password parameter if you want the AP to be open. */
  117.   WiFi.softAP(ssid, password);
  118.  
  119.   IPAddress myIP = WiFi.softAPIP();
  120.   Serial.print("AP IP address: ");
  121.   Serial.println(myIP);
  122.    
  123.  
  124.   webServer.on("/", handleLogin);
  125.   ;
  126.  
  127.   webServer.onNotFound(handleNotFound);
  128.   webServer.begin();
  129.   Serial.println("HTTP server started");
  130.    
  131.    
  132. }
  133. // webServer.handleClient();
  134. // if(connectedWifi)
  135.  
  136.  
  137.  
  138. void loop()
  139. {
  140.   //read dht21
  141.   float hum11 = dht11.readHumidity();
  142.   float temp11 = dht11.readTemperature();
  143.   if (isnan(hum11) || isnan(temp11)) {
  144.   Serial.println("Failed to read from DHT11 sensor!");  
  145.   }
  146.   else{
  147.     Serial.print("DHT11 temp= ");
  148.     Serial.println(temp11);
  149.     Serial.print("DHT11 hum= ");
  150.     Serial.println(hum11);
  151.   }
  152.   //read dht22
  153.   float hum22 = dht22.readHumidity();
  154.   float temp22 = dht22.readTemperature();
  155.   if (isnan(hum22) || isnan(temp22)) {
  156.   Serial.println("Failed to read from DHT22 sensor!");  
  157.   }
  158.   else{
  159.     Serial.print("DHT22 temp= ");
  160.     Serial.println(temp22);
  161.     Serial.print("DHT22 hum= ");
  162.     Serial.println(temp22);
  163.   }
  164.   //read bme
  165.   bme.readSensor();
  166.    Serial.print("Bme280 hum= ");  
  167. Serial.println(bme.getHumidity()); Serial.print(" %\t\t");
  168. Serial.print("Bme280 temp= ");
  169. Serial.println(bme.getTemperature_C()); Serial.print(" *C\t");
  170.  
  171. //sht 25
  172.  unsigned int data[2];
  173.  
  174.   Wire.beginTransmission(sht);
  175.   //Send humidity measurement command
  176.   Wire.write(0xF5);
  177.   Wire.endTransmission();
  178.   delay(500);
  179.  
  180.   // Request 2 bytes of data
  181.   Wire.requestFrom(sht, 2);
  182.   // Read 2 bytes of data to get humidity
  183.   if(Wire.available() == 2)
  184.   {
  185.     data[0] = Wire.read();
  186.     data[1] = Wire.read();
  187.   }
  188.  
  189.   // Convert the data
  190.   float humidity  = ((data[0] * 256.0) + data[1]);
  191.   humidity = ((125 * humidity) / 65536.0) - 6;
  192.  
  193.   Wire.beginTransmission(sht);
  194.   // Send temperature measurement command
  195.   Wire.write(0xF3);
  196.   Wire.endTransmission();
  197.   delay(500);
  198.  
  199.   // Request 2 bytes of data
  200.   Wire.requestFrom(sht, 2);
  201.  
  202.   // Read 2 bytes of data for temperature
  203.   if(Wire.available() == 2)
  204.   {
  205.     data[0] = Wire.read();
  206.     data[1] = Wire.read();
  207.   }
  208.  
  209.   // Convert the data
  210.   float temp  = ((data[0] * 256.0) + data[1]);
  211.   float celsTemp = ((175.72 * temp) / 65536.0) - 46.85;
  212.   float fahrTemp = celsTemp * 1.8 + 32;
  213.  
  214.   // Output data to serial monitor
  215.    Serial.print("SHT25 Humidity : ");
  216.   Serial.println(humidity);
  217.   Serial.print("SHT25 Celsius : ");
  218.   Serial.println(celsTemp);
  219.   delay(30000);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement