Guest User

Untitled

a guest
May 2nd, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1.  
  2. /* Arduino+esp8266 thingSpeak example
  3. * Example name = "Write temperature and humidity to Thingspeak channel"
  4. * Created by Ilias Lamprou
  5. * Updated Oct 30 2016
  6. *
  7. * Download latest Virtuino android app from the link:https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl
  8. * Video tutorial link: https://youtu.be/4XEe0HY0j6k
  9. * Contact address for questions or comments: iliaslampr@gmail.com
  10. */
  11.  
  12. // Code to use SoftwareSerial
  13. #include <SoftwareSerial.h>
  14. SoftwareSerial espSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
  15.  
  16. #define anInput A0 //analog feed from MQ135
  17. #define co2Zero 55 //calibrated CO2 0 level
  18.  
  19. String apiKey = "WR24HU4MCYDRHBQH"; // replace with your channel's thingspeak WRITE API key
  20.  
  21. String ssid="Honor"; // Wifi network SSID
  22. String password ="qwertyuiop"; // Wifi network password
  23.  
  24. boolean DEBUG=true;
  25.  
  26. //======================================================================== showResponce
  27. void showResponse(int waitTime){
  28. long t=millis();
  29. char c;
  30. while (t+waitTime>millis()){
  31. if (espSerial.available()){
  32. c=espSerial.read();
  33. if (DEBUG) Serial.print(c);
  34. }
  35. }
  36.  
  37. }
  38.  
  39. //========================================================================
  40. boolean thingSpeakWrite(float value1){
  41. String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
  42. cmd += "184.106.153.149"; // api.thingspeak.com
  43. cmd += "\",80";
  44. espSerial.println(cmd);
  45. if (DEBUG) Serial.println(cmd);
  46. if(espSerial.find("Error")){
  47. if (DEBUG) Serial.println("AT+CIPSTART error");
  48. return false;
  49. }
  50.  
  51.  
  52. String getStr = "GET /update?api_key="; // prepare GET string
  53. getStr += apiKey;
  54.  
  55. getStr +="&field1=";
  56. getStr += String(value1);
  57. //getStr +="&field2=";
  58. //getStr += String(value2);
  59. //getStr +="&field3=";
  60. //getStr += String(value3);
  61. //getStr +="&field4=";
  62. //getStr += String(value4);
  63. // ...
  64. getStr += "\r\n";
  65.  
  66. // send data length
  67. cmd = "AT+CIPSEND=";
  68. cmd += String(getStr.length());
  69. espSerial.println(cmd);
  70. if (DEBUG) Serial.println(cmd);
  71.  
  72. delay(100);
  73. if(espSerial.find(">")){
  74. espSerial.print(getStr);
  75. if (DEBUG) Serial.print(getStr);
  76. }
  77. else{
  78. espSerial.println("AT+CIPCLOSE");
  79. // alert user
  80. if (DEBUG) Serial.println("AT+CIPCLOSE");
  81. return false;
  82. }
  83. return true;
  84. }
  85. //================================================================================ setup
  86. void setup() {
  87. DEBUG=true; // enable debug serial
  88. Serial.begin(9600);
  89.  
  90. pinMode(anInput,INPUT); //MQ135 analog feed set for input
  91.  
  92.  
  93. espSerial.begin(9600); // enable software serial
  94. // Your esp8266 module's speed is probably at 115200.
  95. // For this reason the first time set the speed to 115200 or to your esp8266 configured speed
  96. // and upload. Then change to 9600 and upload again
  97.  
  98. //espSerial.println("AT+RST"); // Enable this line to reset the module;
  99. //showResponse(1000);
  100.  
  101. //espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
  102. //showResponse(1000);
  103.  
  104.  
  105.  
  106. espSerial.println("AT+CWMODE=1"); // set esp8266 as client
  107. showResponse(1000);
  108.  
  109. espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
  110. showResponse(5000);
  111.  
  112. if (DEBUG) Serial.println("Setup completed");
  113.  
  114.  
  115. }
  116.  
  117.  
  118. // ====================================================================== loop
  119. void loop() {
  120. //+++++++++++++++++++++++++++MQ135+++++++++++++++++++++++
  121. int co2now[10]; //int array for co2 readings
  122. int co2raw = 0; //int for raw value of co2
  123. int co2comp = 0; //int for compensated co2
  124. int co2ppm = 0; //int for calculated ppm
  125. int zzz = 0; //int for averaging
  126. int grafX = 0; //int for x value of graph
  127.  
  128.  
  129. for (int x = 0;x<10;x++){ //samplpe co2 10x over 2 seconds
  130. co2now[x]=analogRead(A0);
  131. delay(200);
  132. }
  133.  
  134. for (int x = 0;x<10;x++){ //add samples together
  135. zzz=zzz + co2now[x];
  136.  
  137. }
  138. co2raw = zzz/10; //divide samples by 10
  139. co2comp = co2raw - co2Zero; //get compensated value
  140. co2ppm = map(co2comp,0,1023,400,5000); //map value for atmospheric levels
  141. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  142.  
  143. thingSpeakWrite(co2ppm);
  144. //============================================================
  145. // thingspeak needs 15 sec delay between updates,
  146. delay(20000);
  147. }
Add Comment
Please, Sign In to add comment