Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //### DHT11 #################################################################################################################################################
- #include <dht.h>
- #define DHT11_PIN 7 // Defines pin number to which the sensor is connected
- dht DHT; // Creates a DHT object
- //### BMP085 ################################################################################################################################################
- #include <Wire.h>
- #include <Adafruit_BMP085.h>
- Adafruit_BMP085 bmp; //TODO whats the difference between BMP085 and BMP180?
- void configureBMP085(void)
- {
- Serial.print("Initialising BMP085...");
- if (bmp.begin())
- Serial.println("OK");
- else
- {
- Serial.println("Fail!");
- while (1) {} //TODO Shut down device
- }
- }
- //### TSL2561 ###############################################################################################################################################
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_TSL2561_U.h>
- //This library assumes the addres is 0x39 which is TSL2561_ADDR_FLOAT
- Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
- void configureTSL2561(void)
- {
- /* Initialise the sensor */
- //use tsl.begin() to default to Wire,
- //tsl.begin(&Wire2) directs api to use Wire2, etc.
- Serial.print("Initialising TSL2561...");
- if (tsl.begin())
- Serial.println("OK");
- else
- {
- Serial.println("Fail!");
- while (1) {} //TODO Shut down device
- }
- tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
- //tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
- //tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
- tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
- }
- //### ESP-01 ESP8266 WIFI ###################################################################################################################################
- #include <SoftwareSerial.h>
- #define RX 3
- #define TX 2
- String AP = "?????????????????";
- String PASS = "???????????????????";
- int countTimeCommand;
- SoftwareSerial esp8266(RX, TX);
- void sendCommand(String command, int maxTime, char readReplay[]) {
- Serial.print(command);
- boolean found = false;
- while (countTimeCommand < maxTime)
- {
- esp8266.println(command);//at+cipsend //It seems we need to send a command multiple times until it "sticks"
- if (esp8266.find(readReplay))//ok
- {
- found = true;
- break;
- }
- //TODO Put a pause between each retry?
- countTimeCommand++;
- }
- if (found)
- Serial.println(" - OK");
- else
- Serial.println(" - Fail");
- countTimeCommand = 0;
- found = false;
- /*
- delay(1000); //TODO Do we need this delay?
- Serial.print("RESPONSE [");
- while (esp8266.available()) {
- //Serial.write(esp8266.read());
- Serial.print(esp8266.readString());
- }
- Serial.println("]");
- */
- }
- void InitESP8266()
- {
- Serial.print("Initialising ESP8266...");
- esp8266.begin(9600); //115200 is too fast
- sendCommand("AT", 5, "OK");
- sendCommand("AT+RST", 5, "OK");
- sendCommand("AT+CWMODE=1", 5, "OK");
- sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
- sendCommand("AT+CWDHCP=1,1", 5, "OK");
- Serial.println("Complete.");
- //AT+CIFSR - Displays the IP address
- }
- //### ThingSpeak ############################################################################################################################################
- String TS_C1_API = "???????????????????";
- String TS_C2_API = "???????????????????";
- #define field1_1 "field1" //BMP085_Temperature
- #define field1_2 "field2" //BMP085_Pressure
- #define field1_3 "field3" //TSL2561_Lux
- #define field1_4 "field4" //TSL2561_Broadband
- #define field1_5 "field5" //TSL2561_InfraRed
- #define field1_6 "field6" //5V PV Cell
- #define field1_7 "field7" //MQ-135 Gas Sensor
- #define field1_8 "field8" //DHT11_Humidity
- #define field2_1 "field1" //DHT11_Temperature
- void SendThingSpeakCommand(String commandString)
- {
- sendCommand("AT+CIPMUX=1", 5, "OK");
- sendCommand("AT+CIPSTART=0,\"TCP\",\"api.thingspeak.com\",80", 15, "OK");
- sendCommand("AT+CIPSEND=0," + String(commandString.length() + 4), 4, ">");
- Serial.println(commandString);
- esp8266.println(commandString);
- sendCommand("AT+CIPCLOSE=0", 5, "OK"); //TODO Do we no pause AFTER the close?
- }
- //### MAIN ##################################################################################################################################################
- #define ONE_SECOND 1000UL
- #define FIVE_SECONDS 5*ONE_SECOND
- #define FIFTEEN_SECONDS 15*ONE_SECOND
- #define HALF_MINUTE 30*ONE_SECOND
- #define ONE_MINUTE 60*ONE_SECOND
- #define FIVE_MINUTES 5*ONE_MINUTE
- #define TEN_MINUTES 10*ONE_MINUTE
- #define FIFTEEN_MINUTES 15*ONE_MINUTE
- #define UPDATE_INTERVAL ONE_MINUTE
- unsigned long previousMillis = 0; // will store last time updated
- void setup() {
- //### Arduino
- Serial.begin(9600);
- Serial.println("Update interval : " + String(UPDATE_INTERVAL/1000) + "s");
- //### BMP085
- configureBMP085();
- //### TSL2561
- configureTSL2561();
- //### ESP-01 ESP8266
- InitESP8266();
- Serial.println("Initialisation complete.");
- //Make it start working immediately
- previousMillis = millis() - UPDATE_INTERVAL;
- }
- void loop() {
- String getData;
- unsigned long currentMillis = millis();
- //On each loop test to see if the interval has passed, otherwise skip passed
- if (currentMillis - previousMillis >= UPDATE_INTERVAL)
- {
- previousMillis = currentMillis;
- //### ESP-01 ESP8266
- //InitESP8266();
- //### TSL2561 //TODO It seems this sensor needs a lot of setup
- sensors_event_t event;
- tsl.getEvent(&event); //This function will read a single sample from the sensor and return it in a generic sensors_event_t object.
- uint16_t broadband = 0;
- uint16_t infrared = 0;
- tsl.getLuminosity(&broadband, &infrared); //can be used to read either the broadband (visible+IR), or the IR. It will return the raw 16-bit sensor value for the specified channel, as shown in the code below:
- //### DHT11
- int chk = DHT.read11(DHT11_PIN); // Reads the data from the sensor
- //### Send the data to TeamSpeak Channel 1
- SendThingSpeakCommand("GET /update?api_key=" + TS_C1_API + "&" +
- field1_1 + "=" + bmp.readTemperature() + "&" +
- field1_2 + "=" + bmp.readPressure() + "&" +
- field1_3 + "=" + event.light + "&" +
- field1_4 + "=" + broadband + "&" +
- field1_5 + "=" + infrared + "&" +
- field1_6 + "=" + analogRead(1) + "&" + //MQ-135 Gas Sensor
- field1_7 + "=" + analogRead(0) + "&" + //5V PV Cell
- field1_8 + "=" + DHT.humidity); //DHT11_Humidity
- //### Send the data to TeamSpeak Channel 2
- SendThingSpeakCommand("GET /update?api_key=" + TS_C2_API + "&" + field2_1 + "=" + DHT.temperature); //DHT11_Temperature
- //### Sleep
- Serial.println("Sleeping...");
- sendCommand("AT+SLEEP=2", 5, "OK"); //2 is better than 1
- //sendCommand("AT+GSLP=40000", 5, "OK"); // This works, we see it in the scope, but we need to attatch wire to wake it up. We can also have the arduino wake it up too! - https://github.com/esp8266/Arduino/issues/1488
- }
- //https://forum.arduino.cc/index.php?topic=468726.0
- //AT+GSLP – Enter deep - sleep mode
- //AT+SLEEP – sleep mode
- //https://www.itead.cc/wiki/images/5/53/Esp8266_at_instruction_set_en_v1.5.4_0.pdf
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement