Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. /*
  2. Thingspeak
  3. Cange SSID and PASSWORD and ThingSpeakKEY
  4. */
  5.  
  6. #define SSID "Public"
  7. #define PASSWORD ""
  8.  
  9. #define ThingSpeakKEY "LC4YYR86SQA3S57N"
  10.  
  11. //#define SENSOR A0
  12.  
  13. #define LED_WLAN 13
  14.  
  15. #define DEBUG true
  16.  
  17. #include <SoftwareSerial.h>
  18. SoftwareSerial esp8266(11, 12); // RX, TX
  19.  
  20. #define DEBUG 0
  21.  
  22. const int pinRx = 7;
  23. const int pinTx = 8;
  24.  
  25. SoftwareSerial sensor(pinTx,pinRx);
  26.  
  27. const unsigned char cmd_get_sensor[] =
  28. {
  29. 0xff, 0x01, 0x86, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x79
  31. };
  32.  
  33. unsigned char dataRevice[9];
  34. int temperature;
  35. int CO2PPM;
  36.  
  37. const byte thingPost[] PROGMEM = {
  38. 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
  39. };
  40.  
  41.  
  42.  
  43. void setup() {
  44. Serial.begin(115200);
  45. esp8266.begin(19200);
  46. sensor.begin(9600);
  47.  
  48. Serial.println("test");
  49. Serial.println("SETOC meting");
  50. Serial.println();
  51.  
  52. if (!espConfig()) serialDebug();
  53. else digitalWrite(LED_WLAN, HIGH);
  54. }
  55.  
  56. void loop() {
  57.  
  58.  
  59. if(dataRecieve())
  60. {
  61. // Serial.print("Temperature: ");
  62. // Serial.print(temperature);
  63. Serial.print(" CO2: ");
  64. Serial.print(CO2PPM);
  65. Serial.println("");
  66. }
  67. delay(1000);
  68.  
  69. if (sendThingPost(ThingSpeakKEY, CO2PPM)) debug("Update Send");
  70. delay(15000);
  71. }
  72. //-----------------CO2 functions---------------------------
  73. bool dataRecieve(void)
  74. {
  75. byte data[9];
  76. int i = 0;
  77.  
  78. //transmit command data
  79. for(i=0; i<sizeof(cmd_get_sensor); i++)
  80. {
  81. sensor.write(cmd_get_sensor[i]);
  82. }
  83. delay(10);
  84. //begin reveiceing data
  85. if(sensor.available())
  86. {
  87. while(sensor.available())
  88. {
  89. for(int i=0;i<9; i++)
  90. {
  91. data[i] = sensor.read();
  92. }
  93. }
  94. }
  95.  
  96. #if DEBUG
  97. for(int j=0; j<9; j++)
  98. {
  99. Serial.print(data[j]);
  100. Serial.print(" ");
  101. }
  102. Serial.println("");
  103. #endif
  104.  
  105. if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3]
  106. + data[4] + data[5] + data[6] + data[7]))) != data[8])
  107. {
  108. return false;
  109. }
  110. CO2PPM = (int)data[2] * 256 + (int)data[3];
  111. temperature = (int)data[4] - 40;
  112.  
  113. return true;
  114. }
  115.  
  116.  
  117. //-----------------------------------------ThingsSpeak Functions------------------------------------
  118.  
  119. boolean sendThingPost(String key, int value)
  120. {
  121. boolean succes = true;
  122. String Host = "api.thingspeak.com";
  123. String msg = "field1=" + String(value);
  124. succes &= sendCom("AT+CIPSTART=\"TCP\",\"" + Host + "\",80", "OK");
  125.  
  126. String postRequest = createThingPost("/update", key, msg);
  127.  
  128. if (sendCom("AT+CIPSEND=" + String(postRequest.length()), ">"))
  129. {
  130. esp8266.print(postRequest);
  131. esp8266.find("SEND OK");
  132. if (!esp8266.find("CLOSED")) succes &= sendCom("AT+CIPCLOSE", "OK");
  133. }
  134. else
  135. {
  136. succes = false;
  137. }
  138. return succes;
  139. }
  140.  
  141. String createThingPost(String url, String key, String msg)
  142. {
  143. String xBuffer;
  144.  
  145. for (int i = 0; i <= sizeof(thingPost); i++)
  146. {
  147. char myChar = pgm_read_byte_near(thingPost + i);
  148. xBuffer += myChar;
  149. }
  150.  
  151. String append = "api_key=" + key + "&" + msg;
  152. xBuffer.replace("*URL*", url);
  153. xBuffer.replace("*LEN*", String( append.length()));
  154. xBuffer.replace("*APPEND*", append);
  155.  
  156. return xBuffer;
  157. }
  158.  
  159. String createThingGet(String url, String key)
  160. {
  161. String xBuffer;
  162.  
  163. for (int i = 0; i <= sizeof(thingPost); i++)
  164. {
  165. char myChar = pgm_read_byte_near(thingPost + i);
  166. xBuffer += myChar;
  167. }
  168.  
  169. String append = "api_key=" + key;
  170. xBuffer.replace("POST", "GET");
  171. xBuffer.replace("*URL*", url);
  172. xBuffer.replace("*LEN*", String( append.length()));
  173. xBuffer.replace("*APPEND*", append);
  174.  
  175. return xBuffer;
  176. }
  177.  
  178. String createThingGet(String url, String key, String msg)
  179. {
  180. String xBuffer;
  181.  
  182. for (int i = 0; i <= sizeof(thingPost); i++)
  183. {
  184. char myChar = pgm_read_byte_near(thingPost + i);
  185. xBuffer += myChar;
  186. }
  187.  
  188. String append = "api_key=" + key + "&" + msg;
  189.  
  190. xBuffer.replace("POST", "GET");
  191. xBuffer.replace("*URL*", url);
  192. xBuffer.replace("*LEN*", String( append.length()));
  193. xBuffer.replace("*APPEND*", append);
  194.  
  195. return xBuffer;
  196. }
  197.  
  198. //-----------------------------------------Config ESP8266------------------------------------
  199.  
  200. boolean espConfig()
  201. {
  202. boolean succes = true;
  203. esp8266.setTimeout(5000);
  204. succes &= sendCom("AT+RST", "ready");
  205. esp8266.setTimeout(1000);
  206.  
  207. if (configStation(SSID, PASSWORD)) {
  208. succes &= true;
  209. debug("WLAN Connected");
  210. debug("My IP is:");
  211. debug(sendCom("AT+CIFSR"));
  212. }
  213. else
  214. {
  215. succes &= false;
  216. }
  217. //shorter Timeout for faster wrong UPD-Comands handling
  218. succes &= sendCom("AT+CIPMUX=0", "OK");
  219. succes &= sendCom("AT+CIPMODE=0", "OK");
  220.  
  221. return succes;
  222. }
  223.  
  224. boolean configTCPServer()
  225. {
  226. boolean succes = true;
  227.  
  228. succes &= (sendCom("AT+CIPMUX=1", "OK"));
  229. succes &= (sendCom("AT+CIPSERVER=1,80", "OK"));
  230.  
  231. return succes;
  232.  
  233. }
  234.  
  235. boolean configTCPClient()
  236. {
  237. boolean succes = true;
  238.  
  239. succes &= (sendCom("AT+CIPMUX=0", "OK"));
  240. //succes &= (sendCom("AT+CIPSERVER=1,80", "OK"));
  241.  
  242. return succes;
  243.  
  244. }
  245.  
  246.  
  247. boolean configStation(String vSSID, String vPASSWORT)
  248. {
  249. boolean succes = true;
  250. succes &= (sendCom("AT+CWMODE=1", "OK"));
  251. esp8266.setTimeout(20000);
  252. succes &= (sendCom("AT+CWJAP=\"" + String(vSSID) + "\",\"" + String(vPASSWORT) + "\"", "OK"));
  253. esp8266.setTimeout(1000);
  254. return succes;
  255. }
  256.  
  257. boolean configAP()
  258. {
  259. boolean succes = true;
  260.  
  261. succes &= (sendCom("AT+CWMODE=2", "OK"));
  262. succes &= (sendCom("AT+CWSAP=\"NanoESP\",\"\",5,0", "OK"));
  263.  
  264. return succes;
  265. }
  266.  
  267. boolean configUDP()
  268. {
  269. boolean succes = true;
  270.  
  271. succes &= (sendCom("AT+CIPMODE=0", "OK"));
  272. succes &= (sendCom("AT+CIPMUX=0", "OK"));
  273. succes &= sendCom("AT+CIPSTART=\"UDP\",\"192.168.255.255\",90,91,2", "OK"); //Importand Boradcast...Reconnect IP
  274. return succes;
  275. }
  276.  
  277.  
  278.  
  279.  
  280. //-----------------------------------------------Controll ESP-----------------------------------------------------
  281.  
  282. boolean sendUDP(String Msg)
  283. {
  284. boolean succes = true;
  285.  
  286. succes &= sendCom("AT+CIPSEND=" + String(Msg.length() + 2), ">"); //+",\"192.168.4.2\",90", ">");
  287. if (succes)
  288. {
  289. succes &= sendCom(Msg, "OK");
  290. }
  291. return succes;
  292. }
  293.  
  294.  
  295. boolean sendCom(String command, char respond[])
  296. {
  297. esp8266.println(command);
  298. if (esp8266.findUntil(respond, "ERROR"))
  299. {
  300. return true;
  301. }
  302. else
  303. {
  304. debug("ESP SEND ERROR: " + command);
  305. return false;
  306. }
  307. }
  308.  
  309. String sendCom(String command)
  310. {
  311. esp8266.println(command);
  312. return esp8266.readString();
  313. }
  314.  
  315.  
  316.  
  317. //-------------------------------------------------Debug Functions------------------------------------------------------
  318. void serialDebug() {
  319. while (true)
  320. {
  321. if (esp8266.available())
  322. Serial.write(esp8266.read());
  323. if (Serial.available())
  324. esp8266.write(Serial.read());
  325. }
  326. }
  327.  
  328. void debug(String Msg)
  329. {
  330. if (DEBUG)
  331. {
  332. Serial.println(Msg);
  333. }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement