Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Thingspeak
- Cange SSID and PASSWORD and ThingSpeakKEY
- */
- #define SSID "Public"
- #define PASSWORD ""
- #define ThingSpeakKEY "LC4YYR86SQA3S57N"
- //#define SENSOR A0
- #define LED_WLAN 13
- #define DEBUG true
- #include <SoftwareSerial.h>
- SoftwareSerial esp8266(11, 12); // RX, TX
- #define DEBUG 0
- const int pinRx = 7;
- const int pinTx = 8;
- SoftwareSerial sensor(pinTx,pinRx);
- const unsigned char cmd_get_sensor[] =
- {
- 0xff, 0x01, 0x86, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x79
- };
- unsigned char dataRevice[9];
- int temperature;
- int CO2PPM;
- const byte thingPost[] PROGMEM = {
- 80, 79, 83, 84, 32, 42, 85, 82, 76, 42, 32, 72, 84, 84, 80, 47, 49, 46, 49, 10, 72, 111, 115, 116, 58, 32, 97, 112, 105, 46, 116, 104, 105, 110, 103, 115, 112, 101, 97, 107, 46, 99, 111, 109, 10, 67, 111, 110, 110, 101, 99, 116, 105, 111, 110, 58, 32, 99, 108, 111, 115, 101, 10, 67, 111, 110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 58, 32, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47, 120, 45, 119, 119, 119, 45, 102, 111, 114, 109, 45, 117, 114, 108, 101, 110, 99, 111, 100, 101, 100, 10, 67, 111, 110, 116, 101, 110, 116, 45, 76, 101, 110, 103, 116, 104, 58, 32, 42, 76, 69, 78, 42, 10, 10, 42, 65, 80, 80, 69, 78, 68, 42, 10
- };
- void setup() {
- Serial.begin(115200);
- esp8266.begin(19200);
- sensor.begin(9600);
- Serial.println("test");
- Serial.println("SETOC meting");
- Serial.println();
- if (!espConfig()) serialDebug();
- else digitalWrite(LED_WLAN, HIGH);
- }
- void loop() {
- if(dataRecieve())
- {
- // Serial.print("Temperature: ");
- // Serial.print(temperature);
- Serial.print(" CO2: ");
- Serial.print(CO2PPM);
- Serial.println("");
- }
- delay(1000);
- if (sendThingPost(ThingSpeakKEY, CO2PPM)) debug("Update Send");
- delay(15000);
- }
- //-----------------CO2 functions---------------------------
- bool dataRecieve(void)
- {
- byte data[9];
- int i = 0;
- //transmit command data
- for(i=0; i<sizeof(cmd_get_sensor); i++)
- {
- sensor.write(cmd_get_sensor[i]);
- }
- delay(10);
- //begin reveiceing data
- if(sensor.available())
- {
- while(sensor.available())
- {
- for(int i=0;i<9; i++)
- {
- data[i] = sensor.read();
- }
- }
- }
- #if DEBUG
- for(int j=0; j<9; j++)
- {
- Serial.print(data[j]);
- Serial.print(" ");
- }
- Serial.println("");
- #endif
- if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3]
- + data[4] + data[5] + data[6] + data[7]))) != data[8])
- {
- return false;
- }
- CO2PPM = (int)data[2] * 256 + (int)data[3];
- temperature = (int)data[4] - 40;
- return true;
- }
- //-----------------------------------------ThingsSpeak Functions------------------------------------
- boolean sendThingPost(String key, int value)
- {
- boolean succes = true;
- String Host = "api.thingspeak.com";
- String msg = "field1=" + String(value);
- succes &= sendCom("AT+CIPSTART=\"TCP\",\"" + Host + "\",80", "OK");
- String postRequest = createThingPost("/update", key, msg);
- if (sendCom("AT+CIPSEND=" + String(postRequest.length()), ">"))
- {
- esp8266.print(postRequest);
- esp8266.find("SEND OK");
- if (!esp8266.find("CLOSED")) succes &= sendCom("AT+CIPCLOSE", "OK");
- }
- else
- {
- succes = false;
- }
- return succes;
- }
- String createThingPost(String url, String key, String msg)
- {
- String xBuffer;
- for (int i = 0; i <= sizeof(thingPost); i++)
- {
- char myChar = pgm_read_byte_near(thingPost + i);
- xBuffer += myChar;
- }
- String append = "api_key=" + key + "&" + msg;
- xBuffer.replace("*URL*", url);
- xBuffer.replace("*LEN*", String( append.length()));
- xBuffer.replace("*APPEND*", append);
- return xBuffer;
- }
- String createThingGet(String url, String key)
- {
- String xBuffer;
- for (int i = 0; i <= sizeof(thingPost); i++)
- {
- char myChar = pgm_read_byte_near(thingPost + i);
- xBuffer += myChar;
- }
- String append = "api_key=" + key;
- xBuffer.replace("POST", "GET");
- xBuffer.replace("*URL*", url);
- xBuffer.replace("*LEN*", String( append.length()));
- xBuffer.replace("*APPEND*", append);
- return xBuffer;
- }
- String createThingGet(String url, String key, String msg)
- {
- String xBuffer;
- for (int i = 0; i <= sizeof(thingPost); i++)
- {
- char myChar = pgm_read_byte_near(thingPost + i);
- xBuffer += myChar;
- }
- String append = "api_key=" + key + "&" + msg;
- xBuffer.replace("POST", "GET");
- xBuffer.replace("*URL*", url);
- xBuffer.replace("*LEN*", String( append.length()));
- xBuffer.replace("*APPEND*", append);
- return xBuffer;
- }
- //-----------------------------------------Config ESP8266------------------------------------
- boolean espConfig()
- {
- boolean succes = true;
- esp8266.setTimeout(5000);
- succes &= sendCom("AT+RST", "ready");
- esp8266.setTimeout(1000);
- if (configStation(SSID, PASSWORD)) {
- succes &= true;
- debug("WLAN Connected");
- debug("My IP is:");
- debug(sendCom("AT+CIFSR"));
- }
- else
- {
- succes &= false;
- }
- //shorter Timeout for faster wrong UPD-Comands handling
- succes &= sendCom("AT+CIPMUX=0", "OK");
- succes &= sendCom("AT+CIPMODE=0", "OK");
- return succes;
- }
- boolean configTCPServer()
- {
- boolean succes = true;
- succes &= (sendCom("AT+CIPMUX=1", "OK"));
- succes &= (sendCom("AT+CIPSERVER=1,80", "OK"));
- return succes;
- }
- boolean configTCPClient()
- {
- boolean succes = true;
- succes &= (sendCom("AT+CIPMUX=0", "OK"));
- //succes &= (sendCom("AT+CIPSERVER=1,80", "OK"));
- return succes;
- }
- boolean configStation(String vSSID, String vPASSWORT)
- {
- boolean succes = true;
- succes &= (sendCom("AT+CWMODE=1", "OK"));
- esp8266.setTimeout(20000);
- succes &= (sendCom("AT+CWJAP=\"" + String(vSSID) + "\",\"" + String(vPASSWORT) + "\"", "OK"));
- esp8266.setTimeout(1000);
- return succes;
- }
- boolean configAP()
- {
- boolean succes = true;
- succes &= (sendCom("AT+CWMODE=2", "OK"));
- succes &= (sendCom("AT+CWSAP=\"NanoESP\",\"\",5,0", "OK"));
- return succes;
- }
- boolean configUDP()
- {
- boolean succes = true;
- succes &= (sendCom("AT+CIPMODE=0", "OK"));
- succes &= (sendCom("AT+CIPMUX=0", "OK"));
- succes &= sendCom("AT+CIPSTART=\"UDP\",\"192.168.255.255\",90,91,2", "OK"); //Importand Boradcast...Reconnect IP
- return succes;
- }
- //-----------------------------------------------Controll ESP-----------------------------------------------------
- boolean sendUDP(String Msg)
- {
- boolean succes = true;
- succes &= sendCom("AT+CIPSEND=" + String(Msg.length() + 2), ">"); //+",\"192.168.4.2\",90", ">");
- if (succes)
- {
- succes &= sendCom(Msg, "OK");
- }
- return succes;
- }
- boolean sendCom(String command, char respond[])
- {
- esp8266.println(command);
- if (esp8266.findUntil(respond, "ERROR"))
- {
- return true;
- }
- else
- {
- debug("ESP SEND ERROR: " + command);
- return false;
- }
- }
- String sendCom(String command)
- {
- esp8266.println(command);
- return esp8266.readString();
- }
- //-------------------------------------------------Debug Functions------------------------------------------------------
- void serialDebug() {
- while (true)
- {
- if (esp8266.available())
- Serial.write(esp8266.read());
- if (Serial.available())
- esp8266.write(Serial.read());
- }
- }
- void debug(String Msg)
- {
- if (DEBUG)
- {
- Serial.println(Msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement