Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define SSID "xxxxxx"
- #define PASS "xxxxxx"
- #define HOST "192.168.30.48" // Webhost
- SoftwareSerial dbgSerial(10, 11); // RX, TX
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <SFE_BMP180.h>
- #include <Wire.h>
- #define ONE_WIRE_BUS 2 // Data wire is plugged into pin 2 on the Arduino
- #define ALTITUDE 112.0 //altitude in meters
- //Password to connect
- #define PASSCODE "Va2MagITGb"
- // Set up DHT22 library.
- #include "DHT.h"
- #define DHTPIN A1
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- SFE_BMP180 pressure;
- int brightness; // Variable for LDR
- float temp;
- float frac = 9.0 / 5.0;
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);//9600 (mine), 57600, 115200
- delay(1000);
- while(!Serial);
- dht.begin(); // DHT22
- pinMode(A0, INPUT); // LDR
- dbgSerial.begin(19200); //can't be faster than 19200 for softserial
- dbgSerial.println("Temp/Humid/Pressure mysql");
- if (pressure.begin())
- dbgSerial.println("Sensors initiated.");
- else
- {
- dbgSerial.println("Sensor init fail\n\n");
- while(1); // Pause forever.
- }
- while(Serial.available()>0)
- Serial.read();
- delay(1000);
- //test if the module is ready
- Serial.println("AT+RST");
- //delay(1000);
- Serial.flush();
- //if(Serial2.find("ready"))
- if(Serial.find("Ready")||Serial.find("ready"))
- {
- dbgSerial.println("Module is ready");
- }
- else
- {
- dbgSerial.println("Module have no response.");
- while(1);
- }
- delay(1000);
- //connect to the wifi
- boolean connected=false;
- for(int i=0;i<5;i++)
- {
- if(connectWiFi())
- {
- connected = true;
- break;
- }
- }
- if (!connected){while(1);}
- delay(5000);
- Serial.println("AT+CIPMUX=0");
- }
- void loop()
- {
- char status;
- double T,P,p0,a;
- float dhtTemp = dht.readTemperature(true); // Get temperature (in C) from DHT22 sensor
- float dhtHumid = dht.readHumidity(); // Get humidity (in %) from DHT22 sensor
- float hi = dht.computeHeatIndex(dhtTemp, dhtHumid);
- //pull from the waterproof sensor first
- sensors.requestTemperatures();
- dbgSerial.print("Outside temperature is: ");
- temp = sensors.getTempCByIndex(0);
- temp = ((temp * frac) + 32.0);
- dbgSerial.print(temp);
- dbgSerial.println(" deg F");
- //print from the DHT22
- dbgSerial.print("Current Indoor Temp/Humidity: ");
- dbgSerial.print("Temp: ");
- dbgSerial.print(dhtTemp);
- dbgSerial.print(" Humidity: ");
- dbgSerial.print(dhtHumid);
- dbgSerial.print(" Heat Index: ");
- dbgSerial.println(hi);
- //end print from the DHT22
- brightness = analogRead(A0); // Get LDR reading
- //dbgSerial.print("Current light reading: ");
- //dbgSerial.println(brightness);
- //begin the pressure reading
- status = pressure.startTemperature();
- if (status != 0)
- {
- // Wait for the measurement to complete:
- delay(status);
- // Retrieve the completed temperature measurement:
- // Note that the measurement is stored in the variable T.
- // Function returns 1 if successful, 0 if failure.
- status = pressure.getTemperature(T);
- if (status != 0)
- {
- // Print out the measurement:
- //dbgSerial.print("Inside temperature is: ");
- //dbgSerial.print((9.0/5.0)*T+32.0,2);
- //dbgSerial.println(" deg F");
- status = pressure.startPressure(3);
- if (status != 0)
- {
- // Wait for the measurement to complete:
- delay(status);
- status = pressure.getPressure(P,T);
- if (status != 0)
- {
- // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
- // Result: p0 = sea-level compensated pressure in mb
- p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
- //dbgSerial.print("relative (sea-level) pressure: ");
- //dbgSerial.print(p0,2);
- //dbgSerial.print(" mb, ");
- //dbgSerial.print(p0*0.0295333727,2);
- //dbgSerial.println(" inHg");
- //dbgSerial.println("-------------------------------------------------------------------------");
- }
- else dbgSerial.println("error retrieving pressure measurement\n");
- }
- else dbgSerial.println("error starting pressure measurement\n");
- }
- else dbgSerial.println("error retrieving temperature measurement\n");
- }
- else dbgSerial.println("error starting temperature measurement\n");
- sendData(String(brightness), String(dhtTemp), String(temp), String(dhtHumid), String(p0));
- delay(30000);
- }
- void sendData(String tBrightness, String tTemp, String oTemp, String tHum, String tPres){
- // Set up TCP connection.
- String cmd = "AT+CIPSTART=\"TCP\",\"";
- cmd += HOST;
- cmd += "\",80";
- Serial.println(cmd);
- dbgSerial.print("Starting TCP: ");
- dbgSerial.println(cmd);
- Serial.print("AT+CIPMUX=0");
- delay(2000);
- if(Serial.find("Error")){
- dbgSerial.println("THERE WAS A PROBLEM STARTING TCP");
- return;
- }
- // Send data.
- cmd = "GET /data.php?code=";
- cmd += PASSCODE;
- cmd += "&l=";
- cmd += tBrightness;
- cmd += "&t=";
- cmd += tTemp;
- cmd += "&ot=";
- cmd += oTemp;
- cmd += "&h=";
- cmd += tHum;
- cmd += "&p=";
- cmd += tPres;
- //cmd += " HTTP/1.1\r\nHost: 192.168.30.48\r\n\r\n\r\n";
- Serial.print("AT+CIPSEND=");
- Serial.println(cmd.length());
- if(Serial.find(">")){
- Serial.print(cmd);
- dbgSerial.print("THIS IS THE FULL COMMAND: ");
- dbgSerial.println(cmd);
- }
- else{
- dbgSerial.println("SOMETHING DIDNT WORK SENDING DATA");
- Serial.println("AT+CIPCLOSE");
- }
- }
- boolean connectWiFi()
- {
- Serial.println("AT+CWMODE=1");
- String cmd="AT+CWJAP=\"";
- cmd+=SSID;
- cmd+="\",\"";
- cmd+=PASS;
- cmd+="\"";
- dbgSerial.println(cmd);
- Serial.println(cmd);
- delay(5000);
- if(Serial.find("OK"))
- {
- dbgSerial.println("OK, Connected to WiFi.");
- return true;
- }else
- {
- dbgSerial.println("Can not connect to the WiFi.");
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement