Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Arduino+esp8266 thingSpeak example
- * Example name = "Write temperature and humidity to Thingspeak channel"
- * Created by Ilias Lamprou
- * Updated Oct 30 2016
- *
- * Download latest Virtuino android app from the link:https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino&hl
- * Video tutorial link: https://youtu.be/4XEe0HY0j6k
- * Contact address for questions or comments: iliaslampr@gmail.com
- */
- // Code to use SoftwareSerial
- #include <SoftwareSerial.h>
- 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
- #define anInput A0 //analog feed from MQ135
- #define co2Zero 55 //calibrated CO2 0 level
- String apiKey = "WR24HU4MCYDRHBQH"; // replace with your channel's thingspeak WRITE API key
- String ssid="Honor"; // Wifi network SSID
- String password ="qwertyuiop"; // Wifi network password
- boolean DEBUG=true;
- //======================================================================== showResponce
- void showResponse(int waitTime){
- long t=millis();
- char c;
- while (t+waitTime>millis()){
- if (espSerial.available()){
- c=espSerial.read();
- if (DEBUG) Serial.print(c);
- }
- }
- }
- //========================================================================
- boolean thingSpeakWrite(float value1){
- String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
- cmd += "184.106.153.149"; // api.thingspeak.com
- cmd += "\",80";
- espSerial.println(cmd);
- if (DEBUG) Serial.println(cmd);
- if(espSerial.find("Error")){
- if (DEBUG) Serial.println("AT+CIPSTART error");
- return false;
- }
- String getStr = "GET /update?api_key="; // prepare GET string
- getStr += apiKey;
- getStr +="&field1=";
- getStr += String(value1);
- //getStr +="&field2=";
- //getStr += String(value2);
- //getStr +="&field3=";
- //getStr += String(value3);
- //getStr +="&field4=";
- //getStr += String(value4);
- // ...
- getStr += "\r\n";
- // send data length
- cmd = "AT+CIPSEND=";
- cmd += String(getStr.length());
- espSerial.println(cmd);
- if (DEBUG) Serial.println(cmd);
- delay(100);
- if(espSerial.find(">")){
- espSerial.print(getStr);
- if (DEBUG) Serial.print(getStr);
- }
- else{
- espSerial.println("AT+CIPCLOSE");
- // alert user
- if (DEBUG) Serial.println("AT+CIPCLOSE");
- return false;
- }
- return true;
- }
- //================================================================================ setup
- void setup() {
- DEBUG=true; // enable debug serial
- Serial.begin(9600);
- pinMode(anInput,INPUT); //MQ135 analog feed set for input
- espSerial.begin(9600); // enable software serial
- // Your esp8266 module's speed is probably at 115200.
- // For this reason the first time set the speed to 115200 or to your esp8266 configured speed
- // and upload. Then change to 9600 and upload again
- //espSerial.println("AT+RST"); // Enable this line to reset the module;
- //showResponse(1000);
- //espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
- //showResponse(1000);
- espSerial.println("AT+CWMODE=1"); // set esp8266 as client
- showResponse(1000);
- espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
- showResponse(5000);
- if (DEBUG) Serial.println("Setup completed");
- }
- // ====================================================================== loop
- void loop() {
- //+++++++++++++++++++++++++++MQ135+++++++++++++++++++++++
- int co2now[10]; //int array for co2 readings
- int co2raw = 0; //int for raw value of co2
- int co2comp = 0; //int for compensated co2
- int co2ppm = 0; //int for calculated ppm
- int zzz = 0; //int for averaging
- int grafX = 0; //int for x value of graph
- for (int x = 0;x<10;x++){ //samplpe co2 10x over 2 seconds
- co2now[x]=analogRead(A0);
- delay(200);
- }
- for (int x = 0;x<10;x++){ //add samples together
- zzz=zzz + co2now[x];
- }
- co2raw = zzz/10; //divide samples by 10
- co2comp = co2raw - co2Zero; //get compensated value
- co2ppm = map(co2comp,0,1023,400,5000); //map value for atmospheric levels
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- thingSpeakWrite(co2ppm);
- //============================================================
- // thingspeak needs 15 sec delay between updates,
- delay(20000);
- }
Add Comment
Please, Sign In to add comment