Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.18 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <WebServer.h>
  4. #include <Wire.h>
  5. #include <SPI.h>
  6. #include <DHT.h>
  7. #include <ESP.h>
  8. #include "CORE_BME280_SPI.h"
  9. #include "OneWire.h"
  10. #define ONEWIRE_PIN           16    // OneWire Dallas sensors are connected to this pin
  11. #define MAX_NUMBER_OF_SENSORS 1    // maximum number of Dallas sensors
  12. OneWire  ds( ONEWIRE_PIN );    
  13. struct sensorStruct
  14. {
  15.   byte addr[8];
  16.   float temp;
  17.   String name;
  18. } sensor[MAX_NUMBER_OF_SENSORS];
  19.  
  20. byte numberOfFoundSensors;
  21. int shtcount=0;
  22.  
  23. //BME280
  24. //#define SENSE_SCK 5;// Set Serial Clock
  25. //#define SENSE_MISO 2;// Set Serial Data Out
  26. //#define SENSE_MOSI 4;// Set Serial Data In
  27. //#define SENSE_CS 19 ;// Set Chip Select
  28.  BME280_SPI bme(19,4,2,5);
  29.  
  30.  
  31. // SHT25 I2C address is 0x40(64)
  32. #define sht 0x40
  33.  
  34. //DHT11
  35. #define DHTPIN 17
  36. DHT dht11(DHTPIN,DHT11,15);
  37.  
  38.  
  39. WebServer webServer(80);
  40. const char* ssid1 ="Multiplay_9014_2";
  41. const char* password1 ="wolfenstein";
  42. String apiKey ="MOBU2YTSKA0KEOY3";
  43. const char* server = "api.thingspeak.com";
  44. bool connectedWifi = false;
  45.  
  46. WiFiClient client;
  47.  
  48. //main page
  49. void handleLogin(){
  50.   //String msg;
  51.   if (webServer.hasArg("USERNAME") && webServer.hasArg("PASSWORD")){
  52.     //if (webServer.arg("USERNAME") == "admin" &&  webServer.arg("PASSWORD") == "12345678" ){
  53.       webServer.sendHeader("Location","/");
  54.       webServer.sendHeader("Cache-Control","no-cache");
  55.       webServer.sendHeader("Set-Cookie","ESPSESSIONID=1");
  56.       webServer.send(301);
  57.       Serial.println("Log in Successful");
  58.       String ssidL = webServer.arg("SSID");
  59.       String passwordL = webServer.arg("PASSWORD");
  60.       apiKey = webServer.arg("API");
  61.       ssid1 = ssidL.c_str();
  62.       password1 =passwordL.c_str();
  63.   Serial.println("Data recived ");
  64.   Serial.println(ssid1);
  65.   Serial.println(password1);
  66.   Serial.println(apiKey);
  67.   WiFi.mode(WIFI_STA);
  68.   WiFi.begin(ssid1, password1);
  69.   while ( WiFi.status() != WL_CONNECTED )
  70.   {
  71.     vTaskDelay( 250 /portTICK_PERIOD_MS );
  72.     Serial.print( "." );
  73.   }
  74.   Serial.println("Connected to Wifi" );
  75.   connectedWifi=true;
  76.       return;
  77.    
  78.   //msg = "Wrong username/password! try again.";
  79.   //Serial.println("Log in Failed");
  80.   }
  81.   String content = "<html><body><form action='/' method='POST'>Put wifi creditials here and thingSprark Api<br>";
  82.   content += "Ssid:<input type='text' name='SSID' placeholder='ssid'><br>";
  83.   content += "User:<input type='text' name='USERNAME' placeholder='username'><br>";
  84.   content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
  85.   content += "Api:<input type='text' name='API' placeholder='api'><br>";
  86.   content += "<input type='submit' name='SUBMIT' value='Submit'></form><br>";
  87.   //content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
  88.   content += "You also can go <a href='/inline'>here</a></body></html>";
  89.   webServer.send(200, "text/html", content);
  90.    
  91. }
  92.  
  93. //no need authentification
  94. void handleNotFound(){
  95.   String message = "Page Not Found\n\n";
  96.   webServer.send(404, "text/plain", message);
  97. }
  98. const char *ssid = "ESP32ap";
  99. const char *password = "12345678";
  100.  
  101. void setup() {
  102.   Wire.begin();
  103.   Serial.begin(115200);
  104.   //d18b20
  105.     xTaskCreatePinnedToCore(
  106.     tempTask,                       /* Function to implement the task */
  107.     "tempTask ",                    /* Name of the task */
  108.     4000,                           /* Stack size in words */
  109.     NULL,                           /* Task input parameter */
  110.     5,                              /* Priority of the task */
  111.     NULL,                           /* Task handle. */
  112.     1);                             /* Core where the task should run */
  113.   //sht
  114.   Wire.beginTransmission(sht);
  115.   Wire.endTransmission();
  116.   delay(300);
  117.   //dht
  118.   dht11.begin();
  119.   //bme
  120.   if (!bme.begin()) {
  121. Serial.println("Error: BME280 sensor, Check Wiring > ");
  122. while (1);
  123. }
  124.  
  125. bme.setTempCal(-1);//Set Sensor was reading high so offset by 1 degree C
  126.  
  127. //wifi
  128.   Serial.println();
  129.   Serial.print("Configuring access point...");
  130.   /* You can remove the password parameter if you want the AP to be open. */
  131.   //WiFi.softAP(ssid, password);
  132.  
  133.   //IPAddress myIP = WiFi.softAPIP();
  134.  // Serial.print("AP IP address: ");
  135.   //Serial.println(myIP);
  136.    
  137.  
  138.   //webServer.on("/", handleLogin);
  139.  
  140.  
  141.   //webServer.onNotFound(handleNotFound);
  142.   //webServer.begin();
  143.   //Serial.println("HTTP server started");
  144.    WiFi.begin(ssid1, password1);
  145.   while ( WiFi.status() != WL_CONNECTED )
  146.   {
  147.     vTaskDelay( 250 /portTICK_PERIOD_MS );
  148.     Serial.print( "." );
  149.   }
  150.    
  151. }
  152. // webServer.handleClient();
  153. // if(connectedWifi)
  154.  
  155. void loop()
  156. {
  157.   webServer.handleClient();
  158.   if(WL_CONNECTED){
  159.  
  160.       if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
  161.         float t;
  162.         //d18b20
  163.         if ( numberOfFoundSensors ){
  164.    
  165.         t =sensor[0].temp / 16.0 ;
  166.  
  167.       Serial.print("Temperature d18b20: ");
  168.       Serial.print(t);
  169.       }
  170.       else
  171.       {
  172.         Serial.println( "No Dallas sensors." );
  173.       }
  174.  
  175. //read dht11
  176.   float hum11 = dht11.readHumidity();
  177.   float temp11 = dht11.readTemperature();
  178.   if (isnan(hum11)  ) {
  179.   hum11=255;
  180.   }
  181.   if(isnan(temp11)){
  182.     temp11=255;
  183.   }
  184. Serial.print("Temperature dht11: ");
  185.       Serial.print(temp11);
  186.       Serial.print("Humidity dht11: ");
  187.       Serial.print(hum11);
  188. //read bme
  189.   bme.readSensor();
  190.    
  191.    float humBme=bme.getHumidity();
  192.   float tempBme=bme.getTemperature_C();
  193.   Serial.print("Temperature Bme: ");
  194.       Serial.print(tempBme);
  195.       Serial.print("Humidity Bme: ");
  196.       Serial.print(humBme);
  197.  
  198.   //sht 25
  199.  unsigned int data[2];
  200.  
  201.   Wire.beginTransmission(sht);
  202.   //Send humidity measurement command
  203.   Wire.write(0xF5);
  204.   Wire.endTransmission();
  205.   delay(500);
  206.  
  207.   // Request 2 bytes of data
  208.   Wire.requestFrom(sht, 2);
  209.   // Read 2 bytes of data to get humidity
  210.   if(Wire.available() == 2)
  211.   {
  212.     data[0] = Wire.read();
  213.     data[1] = Wire.read();
  214.   }
  215.  
  216.   // Convert the data
  217.   float humidity  = ((data[0] * 256.0) + data[1]);
  218.   humidity = ((125 * humidity) / 65536.0) - 6;
  219.  
  220.   Wire.beginTransmission(sht);
  221.   // Send temperature measurement command
  222.   Wire.write(0xF3);
  223.   Wire.endTransmission();
  224.   delay(500);
  225.  
  226.   // Request 2 bytes of data
  227.   Wire.requestFrom(sht, 2);
  228.  
  229.   // Read 2 bytes of data for temperature
  230.   if(Wire.available() == 2)
  231.   {
  232.     data[0] = Wire.read();
  233.     data[1] = Wire.read();
  234.   }
  235.  
  236.   // Convert the data
  237.   float temp  = ((data[0] * 256.0) + data[1]);
  238.   float celsTemp = ((175.72 * temp) / 65536.0) - 46.85;
  239. Serial.print("Temperature sht: ");
  240.       Serial.print(celsTemp);
  241.       Serial.print("Humidity sht: ");
  242.       Serial.print(humidity);
  243.       if(shtcount==5){
  244.         shtcount=0;
  245.          ESP.restart() ;        
  246.       }
  247.       if(celsTemp>500 || humidity>500){
  248.        shtcount++;
  249.       }
  250.       else{
  251.         shtcount=0;
  252.       }
  253.  
  254. String postStr1 = apiKey;
  255. postStr1 +="&field1=";
  256. postStr1 += String(temp11);
  257. postStr1 += "\r\n\r\n";
  258. String postStr2 = apiKey;
  259. postStr2 +="&field2=";
  260. postStr2 += String(hum11);
  261. postStr2 += "\r\n\r\n";
  262. String postStr3 = apiKey;
  263. postStr3 +="&field3=";
  264. postStr3 += String(t);
  265. postStr3 += "\r\n\r\n";
  266. String postStr4 = apiKey;
  267. postStr4 +="&field4=";
  268. postStr4 += String(celsTemp);
  269. postStr4 += "\r\n\r\n";
  270. String postStr5 = apiKey;
  271. postStr5 +="&field5=";
  272. postStr5 += String(humidity);
  273. postStr5 += "\r\n\r\n";
  274. String postStr6 = apiKey;
  275. postStr6 +="&field6=";
  276. postStr6 += String(tempBme);
  277. postStr6 += "\r\n\r\n";
  278. String postStr7 = apiKey;
  279. postStr7 +="&field7=";
  280. postStr7 += String(humBme);
  281. postStr7 += "\r\n\r\n";
  282. String postStr8 = apiKey;
  283. postStr8 +="&field7=";
  284. postStr8 += String(humBme);
  285. postStr8 += "\r\n\r\n";
  286.  
  287. client.print("POST /update HTTP/1.1\n");
  288. client.print("Host: api.thingspeak.com\n");
  289. client.print("Connection: close\n");
  290. client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  291. client.print("Content-Type: application/x-www-form-urlencoded\n");
  292. client.print("Content-Length: ");
  293. client.print(postStr1.length()+postStr2.length()+postStr3.length()+postStr4.length()+postStr5.length()+postStr6.length()+postStr7.length()+postStr8.length());
  294. client.print("\n\n");
  295. client.print(postStr1);
  296. client.print("\n\n");
  297. client.print(postStr2);
  298. client.print("\n\n");
  299. client.print(postStr3);
  300. client.print("\n\n");
  301. client.print(postStr4);
  302. client.print("\n\n");
  303. client.print(postStr5);
  304. client.print("\n\n");
  305. client.print(postStr6);
  306. client.print("\n\n");
  307. client.print(postStr7);
  308. client.print("\n\n");
  309. client.print(postStr8);
  310. client.print("\n\n");
  311.  
  312. Serial.println("% send to Thingspeak");
  313. }
  314. client.stop();
  315.  
  316. Serial.println("Waiting?");
  317. // thingspeak needs minimum 15 sec delay between updates
  318. delay(20000);
  319.  
  320.  
  321.   //vTaskDelay( 500 / portTICK_PERIOD_MS );
  322. }
  323. else{
  324.  ESP.restart() ;  
  325. }
  326. }
  327.  
  328. void tempTask( void * pvParameters )
  329. {
  330.   numberOfFoundSensors = 0;
  331.   byte currentAddr[8];
  332.   while ( ds.search( currentAddr ) && numberOfFoundSensors < MAX_NUMBER_OF_SENSORS )
  333.   {
  334.     //Serial.write( "Sensor "); Serial.print( counter ); Serial.print( ":" );
  335.     for ( byte i = 0; i < 8; i++)
  336.     {
  337.       //Serial.write(' ');
  338.       //Serial.print( currentAddr[i], HEX );
  339.       sensor[numberOfFoundSensors].addr[i] = currentAddr[i];
  340.     }
  341.     //sensor[numberOfFoundSensors].name = 'T ' + char( numberOfFoundSensors );
  342.     numberOfFoundSensors++;
  343.   }
  344.   Serial.printf( "%i Dallas sensors found.\n", numberOfFoundSensors );
  345.  
  346.   if ( !numberOfFoundSensors )
  347.   {
  348.     vTaskDelete( NULL );
  349.   }
  350.  
  351.   /* main temptask loop */
  352.  
  353.   while (1)
  354.   {
  355.     for ( byte thisSensor = 0; thisSensor < numberOfFoundSensors; thisSensor++)
  356.     {
  357.       ds.reset();
  358.       ds.select( sensor[thisSensor].addr );
  359.       ds.write( 0x44, 0);        // start conversion, with parasite power off at the end
  360.     }
  361.  
  362.     vTaskDelay( 750 / portTICK_PERIOD_MS); //wait for conversion ready
  363.  
  364.     for ( byte thisSensor = 0; thisSensor < numberOfFoundSensors; thisSensor++)
  365.     {
  366.       byte data[12];
  367.       ds.reset();
  368.       ds.select( sensor[thisSensor].addr );
  369.       ds.write( 0xBE );         // Read Scratchpad
  370.  
  371.       //Serial.print( "Sensor " );Serial.print( thisSensor ); Serial.print("  Data = ");
  372.       //Serial.println( present, HEX );
  373.       //Serial.print(" ");
  374.       for ( byte i = 0; i < 9; i++)
  375.       { // we need 9 bytes
  376.         data[i] = ds.read(  );
  377.         //Serial.print(data[i], HEX);
  378.         //Serial.print(" ");
  379.       }
  380.       //Serial.println();
  381.  
  382.       byte type_s;
  383.       // the first ROM byte indicates which chip
  384.       switch ( sensor[thisSensor].addr[0] )
  385.       {
  386.         case 0x10:
  387.           //Serial.println("  Chip = DS18S20");  // or old DS1820
  388.           type_s = 1;
  389.           break;
  390.         case 0x28:
  391.           //Serial.println("  Chip = DS18B20");
  392.           type_s = 0;
  393.           break;
  394.         case 0x22:
  395.           //Serial.println("  Chip = DS1822");
  396.           type_s = 0;
  397.           break;
  398.         default:
  399. #ifdef SHOW_DALLAS_ERROR
  400.           Serial.println("Device is not a DS18x20 family device.");
  401. #endif
  402.           return;
  403.       }
  404.  
  405.       int16_t raw;
  406.       if ( OneWire::crc8(data, 8) != data[8])
  407.       {
  408. #ifdef SHOW_DALLAS_ERROR
  409.         // CRC of temperature reading indicates an error, so we print a error message and discard this reading
  410.         Serial.print( millis() / 1000.0 ); Serial.print( " - CRC error from device " ); Serial.println( thisSensor );
  411. #endif
  412.       }
  413.       else
  414.       {
  415.         raw = (data[1] << 8) | data[0];
  416.         if (type_s)
  417.         {
  418.           raw = raw << 3; // 9 bit resolution default
  419.           if (data[7] == 0x10)
  420.           {
  421.             // "count remain" gives full 12 bit resolution
  422.             raw = (raw & 0xFFF0) + 12 - data[6];
  423.           }
  424.         }
  425.         else
  426.         {
  427.           byte cfg = (data[4] & 0x60);
  428.           // at lower res, the low bits are undefined, so let's zero them
  429.           if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  430.           else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  431.           else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  432.           //// default is 12 bit resolution, 750 ms conversion time
  433.         }
  434.         sensor[thisSensor].temp = raw;
  435.       }
  436.     }
  437.   }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement