Advertisement
Guest User

esp8266-mysql

a guest
May 24th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.29 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #define SSID "xxxxxx"
  3. #define PASS "xxxxxx"
  4. #define HOST "192.168.30.48" // Webhost
  5. SoftwareSerial dbgSerial(10, 11); // RX, TX
  6. #include <OneWire.h>
  7. #include <DallasTemperature.h>
  8. #include <SFE_BMP180.h>
  9. #include <Wire.h>  
  10. #define ONE_WIRE_BUS 2     // Data wire is plugged into pin 2 on the Arduino
  11. #define ALTITUDE 112.0 //altitude in meters  
  12. //Password to connect
  13. #define PASSCODE "Va2MagITGb"
  14. // Set up DHT22 library.
  15. #include "DHT.h"
  16. #define DHTPIN A1
  17. #define DHTTYPE DHT22
  18. DHT dht(DHTPIN, DHTTYPE);
  19.    
  20. OneWire oneWire(ONE_WIRE_BUS);
  21. DallasTemperature sensors(&oneWire);
  22. SFE_BMP180 pressure;
  23. int brightness;     // Variable for LDR
  24. float temp;
  25. float frac = 9.0 / 5.0;
  26.  
  27. void setup()
  28. {
  29.      // Open serial communications and wait for port to open:
  30.      Serial.begin(9600);//9600 (mine), 57600, 115200
  31.      delay(1000);
  32.      while(!Serial);
  33.      dht.begin(); // DHT22
  34.      pinMode(A0, INPUT);    // LDR
  35.    
  36.      dbgSerial.begin(19200); //can't be faster than 19200 for softserial
  37.      dbgSerial.println("Temp/Humid/Pressure mysql");
  38.      
  39.      
  40.        if (pressure.begin())
  41.         dbgSerial.println("Sensors initiated.");
  42.        else
  43.         {
  44.         dbgSerial.println("Sensor init fail\n\n");
  45.         while(1); // Pause forever.
  46.         }
  47.    
  48.     while(Serial.available()>0)
  49.     Serial.read();
  50.      
  51.      delay(1000);
  52.        //test if the module is ready
  53.      Serial.println("AT+RST");
  54.      //delay(1000);
  55.  
  56.      Serial.flush();
  57.      
  58.      //if(Serial2.find("ready"))
  59.      if(Serial.find("Ready")||Serial.find("ready"))
  60.      {
  61.        dbgSerial.println("Module is ready");
  62.      }
  63.      else
  64.      {
  65.        dbgSerial.println("Module have no response.");
  66.        while(1);
  67.      }
  68.      delay(1000);
  69.      //connect to the wifi
  70.      boolean connected=false;
  71.      for(int i=0;i<5;i++)
  72.      {
  73.        if(connectWiFi())
  74.        {
  75.          connected = true;
  76.          break;
  77.        }
  78.      }
  79.      if (!connected){while(1);}
  80.      delay(5000);
  81.  
  82.      Serial.println("AT+CIPMUX=0");
  83.    }
  84.    
  85.    
  86.    
  87. void loop()
  88. {
  89.          
  90.   char status;
  91.   double T,P,p0,a;
  92.   float dhtTemp = dht.readTemperature(true);        // Get temperature (in C) from DHT22 sensor
  93.   float dhtHumid = dht.readHumidity();          // Get humidity (in %) from DHT22 sensor
  94.   float hi = dht.computeHeatIndex(dhtTemp, dhtHumid);
  95.    
  96.   //pull from the waterproof sensor first
  97.   sensors.requestTemperatures();
  98.   dbgSerial.print("Outside temperature is: ");
  99.   temp = sensors.getTempCByIndex(0);
  100.   temp = ((temp * frac) + 32.0);
  101.   dbgSerial.print(temp);
  102.   dbgSerial.println(" deg F");
  103.   //print from the DHT22
  104.   dbgSerial.print("Current Indoor Temp/Humidity:  ");
  105.   dbgSerial.print("Temp: ");
  106.   dbgSerial.print(dhtTemp);
  107.   dbgSerial.print("  Humidity: ");
  108.   dbgSerial.print(dhtHumid);
  109.   dbgSerial.print("  Heat Index:  ");
  110.   dbgSerial.println(hi);
  111.   //end print from the DHT22
  112.   brightness = analogRead(A0);          // Get LDR reading
  113.   //dbgSerial.print("Current light reading: ");
  114.   //dbgSerial.println(brightness);
  115.  
  116.   //begin the pressure reading
  117.   status = pressure.startTemperature();
  118.   if (status != 0)
  119.   {
  120.     // Wait for the measurement to complete:
  121.     delay(status);
  122.  
  123.     // Retrieve the completed temperature measurement:
  124.     // Note that the measurement is stored in the variable T.
  125.     // Function returns 1 if successful, 0 if failure.
  126.  
  127.     status = pressure.getTemperature(T);
  128.     if (status != 0)
  129.     {
  130.       // Print out the measurement:
  131.       //dbgSerial.print("Inside temperature is: ");
  132.       //dbgSerial.print((9.0/5.0)*T+32.0,2);
  133.       //dbgSerial.println(" deg F");
  134.  
  135.       status = pressure.startPressure(3);
  136.       if (status != 0)
  137.       {
  138.         // Wait for the measurement to complete:
  139.         delay(status);
  140.         status = pressure.getPressure(P,T);
  141.         if (status != 0)
  142.         {
  143.  
  144.           // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
  145.           // Result: p0 = sea-level compensated pressure in mb
  146.  
  147.           p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
  148.           //dbgSerial.print("relative (sea-level) pressure: ");
  149.           //dbgSerial.print(p0,2);
  150.           //dbgSerial.print(" mb, ");
  151.           //dbgSerial.print(p0*0.0295333727,2);
  152.           //dbgSerial.println(" inHg");
  153.           //dbgSerial.println("-------------------------------------------------------------------------");
  154.         }
  155.         else dbgSerial.println("error retrieving pressure measurement\n");
  156.       }
  157.       else dbgSerial.println("error starting pressure measurement\n");
  158.     }
  159.     else dbgSerial.println("error retrieving temperature measurement\n");
  160.   }
  161.   else dbgSerial.println("error starting temperature measurement\n");
  162.  
  163.   sendData(String(brightness), String(dhtTemp), String(temp), String(dhtHumid), String(p0));
  164.   delay(30000);
  165. }
  166.      
  167. void sendData(String tBrightness, String tTemp, String oTemp, String tHum, String tPres){
  168.   // Set up TCP connection.
  169.  
  170.   String cmd = "AT+CIPSTART=\"TCP\",\"";
  171.   cmd += HOST;
  172.   cmd += "\",80";
  173.   Serial.println(cmd);
  174.   dbgSerial.print("Starting TCP:  ");
  175.   dbgSerial.println(cmd);
  176.   Serial.print("AT+CIPMUX=0");
  177.   delay(2000);
  178.   if(Serial.find("Error")){
  179.     dbgSerial.println("THERE WAS A PROBLEM STARTING TCP");
  180.     return;
  181.   }
  182.  
  183.   // Send data.
  184.   cmd = "GET /data.php?code=";
  185.   cmd += PASSCODE;
  186.   cmd += "&l=";
  187.   cmd += tBrightness;
  188.   cmd += "&t=";
  189.   cmd += tTemp;
  190.   cmd += "&ot=";
  191.   cmd += oTemp;
  192.   cmd += "&h=";
  193.   cmd += tHum;
  194.   cmd += "&p=";
  195.   cmd += tPres;
  196.   //cmd += " HTTP/1.1\r\nHost: 192.168.30.48\r\n\r\n\r\n";
  197.   Serial.print("AT+CIPSEND=");
  198.   Serial.println(cmd.length());
  199.   if(Serial.find(">")){
  200.     Serial.print(cmd);
  201.     dbgSerial.print("THIS IS THE FULL COMMAND:    ");
  202.     dbgSerial.println(cmd);
  203.   }
  204.   else{
  205.     dbgSerial.println("SOMETHING DIDNT WORK SENDING DATA");
  206.     Serial.println("AT+CIPCLOSE");
  207.   }
  208. }    
  209.      
  210. boolean connectWiFi()
  211. {
  212.   Serial.println("AT+CWMODE=1");
  213.   String cmd="AT+CWJAP=\"";
  214.   cmd+=SSID;
  215.   cmd+="\",\"";
  216.   cmd+=PASS;
  217.   cmd+="\"";
  218.   dbgSerial.println(cmd);
  219.   Serial.println(cmd);
  220.   delay(5000);
  221.  
  222.   if(Serial.find("OK"))
  223.   {
  224.     dbgSerial.println("OK, Connected to WiFi.");
  225.     return true;
  226.   }else
  227.   {
  228.     dbgSerial.println("Can not connect to the WiFi.");
  229.     return false;
  230.   }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement