Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.41 KB | None | 0 0
  1. //### DHT11 #################################################################################################################################################
  2. #include <dht.h>
  3. #define DHT11_PIN 7 // Defines pin number to which the sensor is connected
  4. dht DHT; // Creates a DHT object
  5.  
  6. //### BMP085 ################################################################################################################################################
  7. #include <Wire.h>
  8. #include <Adafruit_BMP085.h>
  9. Adafruit_BMP085 bmp; //TODO whats the difference between BMP085 and BMP180?
  10.  
  11. void configureBMP085(void)
  12. {
  13.     Serial.print("Initialising BMP085...");
  14.     if (bmp.begin())
  15.         Serial.println("OK");
  16.     else
  17.     {
  18.         Serial.println("Fail!");
  19.         while (1) {} //TODO Shut down device
  20.     }
  21. }
  22. //### TSL2561 ###############################################################################################################################################
  23.  
  24. #include <Wire.h>
  25. #include <Adafruit_Sensor.h>
  26. #include <Adafruit_TSL2561_U.h>
  27.  
  28. //This library assumes the addres is 0x39 which is TSL2561_ADDR_FLOAT
  29. Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
  30.  
  31. void configureTSL2561(void)
  32. {
  33.     /* Initialise the sensor */
  34.     //use tsl.begin() to default to Wire,
  35.     //tsl.begin(&Wire2) directs api to use Wire2, etc.
  36.  
  37.     Serial.print("Initialising TSL2561...");
  38.     if (tsl.begin())
  39.         Serial.println("OK");
  40.     else
  41.     {
  42.         Serial.println("Fail!");
  43.         while (1) {} //TODO Shut down device
  44.     }
  45.  
  46.     tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  47.                                        //tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  48.                                        //tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  49.     tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */
  50.  
  51. }
  52.  
  53. //### ESP-01 ESP8266 WIFI ###################################################################################################################################
  54.  
  55. #include <SoftwareSerial.h>
  56.  
  57. #define RX 3
  58. #define TX 2
  59.  
  60. String AP = "?????????????????";      
  61. String PASS = "???????????????????";
  62.  
  63. int countTimeCommand;
  64.  
  65. SoftwareSerial esp8266(RX, TX);
  66.  
  67. void sendCommand(String command, int maxTime, char readReplay[]) {
  68.    
  69.     Serial.print(command);
  70.    
  71.     boolean found = false;
  72.     while (countTimeCommand < maxTime)
  73.     {
  74.         esp8266.println(command);//at+cipsend //It seems we need to send a command multiple times until it "sticks"
  75.    
  76.         if (esp8266.find(readReplay))//ok
  77.         {
  78.             found = true;
  79.             break;
  80.         }
  81.         //TODO Put a pause between each retry?
  82.         countTimeCommand++;
  83.     }
  84.    
  85.     if (found)
  86.         Serial.println(" - OK");
  87.     else
  88.         Serial.println(" - Fail");
  89.  
  90.     countTimeCommand = 0;
  91.     found = false;
  92.     /*
  93.     delay(1000); //TODO Do we need this delay?
  94.     Serial.print("RESPONSE [");
  95.     while (esp8266.available()) {
  96.         //Serial.write(esp8266.read());
  97.         Serial.print(esp8266.readString());
  98.     }
  99.     Serial.println("]");
  100.     */
  101. }
  102.  
  103. void InitESP8266()
  104. {
  105.     Serial.print("Initialising ESP8266...");
  106.     esp8266.begin(9600); //115200 is too fast
  107.  
  108.     sendCommand("AT", 5, "OK");
  109.     sendCommand("AT+RST", 5, "OK");
  110.     sendCommand("AT+CWMODE=1", 5, "OK");
  111.     sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
  112.     sendCommand("AT+CWDHCP=1,1", 5, "OK");
  113.     Serial.println("Complete.");
  114.  
  115.     //AT+CIFSR - Displays the IP address
  116. }
  117.  
  118. //### ThingSpeak ############################################################################################################################################
  119. String TS_C1_API = "???????????????????";
  120. String TS_C2_API = "???????????????????";
  121.  
  122. #define field1_1 "field1" //BMP085_Temperature
  123. #define field1_2 "field2" //BMP085_Pressure
  124. #define field1_3 "field3" //TSL2561_Lux
  125. #define field1_4 "field4" //TSL2561_Broadband
  126. #define field1_5 "field5" //TSL2561_InfraRed
  127. #define field1_6 "field6" //5V PV Cell
  128. #define field1_7 "field7" //MQ-135 Gas Sensor
  129. #define field1_8 "field8" //DHT11_Humidity
  130. #define field2_1 "field1" //DHT11_Temperature
  131.  
  132. void SendThingSpeakCommand(String commandString)
  133. {
  134.     sendCommand("AT+CIPMUX=1", 5, "OK");
  135.     sendCommand("AT+CIPSTART=0,\"TCP\",\"api.thingspeak.com\",80", 15, "OK");
  136.     sendCommand("AT+CIPSEND=0," + String(commandString.length() + 4), 4, ">");
  137.     Serial.println(commandString);
  138.     esp8266.println(commandString);
  139.     sendCommand("AT+CIPCLOSE=0", 5, "OK"); //TODO Do we no pause AFTER the close?
  140. }
  141.  
  142. //### MAIN ##################################################################################################################################################
  143.  
  144. #define ONE_SECOND 1000UL
  145. #define FIVE_SECONDS 5*ONE_SECOND
  146. #define FIFTEEN_SECONDS 15*ONE_SECOND
  147. #define HALF_MINUTE 30*ONE_SECOND
  148. #define ONE_MINUTE 60*ONE_SECOND
  149. #define FIVE_MINUTES 5*ONE_MINUTE
  150. #define TEN_MINUTES 10*ONE_MINUTE
  151. #define FIFTEEN_MINUTES 15*ONE_MINUTE
  152.  
  153. #define UPDATE_INTERVAL ONE_MINUTE
  154.  
  155. unsigned long previousMillis = 0;        // will store last time updated
  156.  
  157. void setup() {
  158.  
  159.     //### Arduino
  160.     Serial.begin(9600);
  161.     Serial.println("Update interval : " + String(UPDATE_INTERVAL/1000) + "s");
  162.  
  163.     //### BMP085
  164.     configureBMP085();
  165.  
  166.     //### TSL2561
  167.     configureTSL2561();
  168.  
  169.     //### ESP-01 ESP8266
  170.     InitESP8266();
  171.  
  172.     Serial.println("Initialisation complete.");
  173.  
  174.     //Make it start working immediately
  175.     previousMillis = millis() - UPDATE_INTERVAL;
  176. }
  177.  
  178. void loop() {
  179.  
  180.     String getData;
  181.     unsigned long currentMillis = millis();
  182.  
  183.     //On each loop test to see if the interval has passed, otherwise skip passed
  184.     if (currentMillis - previousMillis >= UPDATE_INTERVAL)
  185.     {
  186.         previousMillis = currentMillis;
  187.  
  188.         //### ESP-01 ESP8266
  189.         //InitESP8266();
  190.  
  191.         //### TSL2561 //TODO It seems this sensor needs a lot of setup
  192.         sensors_event_t event;
  193.         tsl.getEvent(&event); //This function will read a single sample from the sensor and return it in a generic sensors_event_t object.
  194.         uint16_t broadband = 0;
  195.         uint16_t infrared = 0;
  196.         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:
  197.  
  198.         //### DHT11
  199.         int chk = DHT.read11(DHT11_PIN); // Reads the data from the sensor
  200.  
  201.         //### Send the data to TeamSpeak Channel 1
  202.         SendThingSpeakCommand("GET /update?api_key=" + TS_C1_API + "&" +
  203.             field1_1 + "=" + bmp.readTemperature() + "&" +
  204.             field1_2 + "=" + bmp.readPressure() + "&" +
  205.             field1_3 + "=" + event.light + "&" +
  206.             field1_4 + "=" + broadband + "&" +
  207.             field1_5 + "=" + infrared + "&" +
  208.             field1_6 + "=" + analogRead(1) + "&" + //MQ-135 Gas Sensor
  209.             field1_7 + "=" + analogRead(0) + "&" + //5V PV Cell
  210.             field1_8 + "=" + DHT.humidity); //DHT11_Humidity
  211.        
  212.         //### Send the data to TeamSpeak Channel 2
  213.         SendThingSpeakCommand("GET /update?api_key=" + TS_C2_API + "&" + field2_1 + "=" + DHT.temperature); //DHT11_Temperature
  214.  
  215.         //### Sleep
  216.         Serial.println("Sleeping...");
  217.         sendCommand("AT+SLEEP=2", 5, "OK"); //2 is better than 1
  218.         //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
  219.     }
  220.  
  221.     //https://forum.arduino.cc/index.php?topic=468726.0
  222.     //AT+GSLP – Enter deep - sleep mode
  223.     //AT+SLEEP – sleep mode
  224.     //https://www.itead.cc/wiki/images/5/53/Esp8266_at_instruction_set_en_v1.5.4_0.pdf
  225.    
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement