Advertisement
v12345vtm

berlingoesp

Mar 5th, 2017
148
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. String locationcode = "100";
  4.  
  5.  
  6.  
  7. //wifi at home
  8. const char* ssidthuis = "ssidhome";
  9. const char* passwordthuis = "pass1";
  10. //If you have a 2nd hotspot on other place
  11. const char* ssidlet = "ssid2";
  12. const char* passwordlet = "pass2";
  13. //thingspeak log
  14. String apiKey = "FGGETUDSRFGTE8LNK5YGRHE"; //thingspeak API key,
  15.  
  16. //change pehacommand to your peha command !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  17.  
  18.  
  19.  
  20. String lokatie = "0";
  21. const char* ssid = ""; //temp ssid en password
  22. const char* password = "";
  23. String inData;//seriele recieve buffer
  24. const char* server = "api.thingspeak.com";
  25. String theDate; //Sat, 05 Nov 2016 13:30:29 GMT
  26. WiFiClient client;
  27.  
  28. /////////////////subroutines
  29. int find_text(String needle, String haystack) {
  30. int foundpos = -1;
  31. for (int i = 0; i <= haystack.length() - needle.length(); i++) {
  32. if (haystack.substring(i, needle.length() + i) == needle) {
  33. foundpos = i;
  34. }
  35. }
  36. return foundpos;
  37. }
  38. /////////////////////////peha wifi commando
  39. void pehacommand()
  40. {
  41. inData = "";//reset rs232 buffer
  42. HTTPClient http;
  43. http.begin("http://192.168.1.2:80/postEvent.html?action=input&STM=0&MOD=4&CHA=7&EVT=2"); //far buiten voetpad
  44. int httpCode = http.GET();
  45. if (httpCode > 0) {
  46. // file found at server
  47. if (httpCode == HTTP_CODE_OK) {
  48. String payload = http.getString();
  49. Serial.println(payload);
  50. }
  51. } else {
  52. Serial.printf("[HTTP] peha workt ok but ..GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  53. }
  54. http.end();
  55. }
  56. //////////////////thingspeak log sturen
  57. void thingspeak(String field2data) {
  58. if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
  59. String postStr = apiKey;
  60. postStr += "&field1="; //
  61. postStr += (locationcode);
  62. postStr += "&field2=";
  63. postStr += String(field2data ); //datawaarde
  64. // postStr += "\r\n\r\n";
  65. client.print("POST /update HTTP/1.1\n");
  66. client.print("Host: api.thingspeak.com\n");
  67. client.print("Connection: close\n");
  68. client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  69. client.print("Content-Type: application/x-www-form-urlencoded\n");
  70. client.print("Content-Length: ");
  71. client.print(postStr.length());
  72. client.print("\n\n");
  73. client.print(postStr);
  74. Serial.println(field2data + " send to Thingspeak");
  75. // inData = ""; // Clear recieved buffer
  76. }
  77. client.stop();
  78. }
  79. ////////////////wifi scanner
  80. void wifiscan() {
  81. Serial.println("scan start");
  82. // WiFi.scanNetworks will return the number of networks found
  83. int n = WiFi.scanNetworks();
  84. Serial.println("scan done");
  85. if (n == 0)
  86. Serial.println("no networks found");
  87. else
  88. {
  89. Serial.print(n);
  90. Serial.println(" networks found");
  91. for (int i = 0; i < n; ++i)
  92. {
  93. // Print SSID and RSSI for each network found
  94. Serial.print(i + 1);
  95. Serial.print(": ");
  96. Serial.print(WiFi.SSID(i));
  97. Serial.print(" (");
  98. Serial.print(WiFi.RSSI(i));
  99. Serial.print(")");
  100. Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
  101. delay(10);
  102. }
  103. }
  104. Serial.println("");
  105. // Wait a bit before scanning again
  106. delay(5000);
  107. }
  108.  
  109. ///wifi scanner
  110. void zoekbekendewifis() {
  111. Serial.println("scan bekende wifis start");
  112. // WiFi.scanNetworks will return the number of networks found
  113. int n = WiFi.scanNetworks();
  114. // Serial.println("scan done");
  115. if (n == 0)
  116. // Serial.println("no networks found");
  117. n = n;
  118. else
  119. {
  120. // Serial.print(n);
  121. // Serial.println(" networks found");
  122. for (int i = 0; i < n; ++i)
  123. {
  124. if (WiFi.SSID(i) == ssidthuis )
  125. {
  126. ssid = ssidthuis ;
  127. password = passwordthuis;
  128. digitalWrite(0, HIGH); // led aan
  129. lokatie = "20"; // 20 is homespot in grafiek
  130. initWifi();
  131. pehacommand();
  132. thingspeak(lokatie);
  133. WiFi.disconnect();
  134. Serial.println("disconnected");
  135. }
  136.  
  137.  
  138. if (WiFi.SSID(i) == ssidlet )
  139. {
  140. ssid = ssidlet ;
  141. password = passwordlet;
  142. digitalWrite(2, HIGH); // led aan
  143. lokatie = "10"; // 10=wifi from parents in grafiek
  144. initWifi();
  145. thingspeak(lokatie);
  146. //pehacommand();
  147. WiFi.disconnect();
  148. Serial.println("disconnected");
  149. break ; //we moeten niet weten welke wifis er nog zijn
  150. }
  151. }
  152. }
  153. //Serial.println("---");
  154. }
  155. /////////////uart
  156. void uart() {
  157. while (Serial.available() > 0)
  158. {
  159. char recieved = Serial.read();
  160. inData += recieved;
  161. // Process message when new line character is recieved
  162. if (recieved == '!')
  163. { Serial.print("\nESP Received: "); //
  164. Serial.print(inData);
  165. if (inData == "rolop!") {
  166. pehacommand();
  167. inData = "";//reset rs232 buffer
  168. }
  169. if (inData == "wifi!")
  170. {
  171. Serial.println("\n" + WiFi.SSID()); //Gets the SSID of the current network
  172. inData = "";//reset rs232 buffer
  173. }
  174. if (inData == "thing!")
  175. {
  176. Serial.println("thingspeak");
  177. thingspeak(lokatie);
  178. inData = "";//reset rs232 buffer
  179. }
  180. if (inData == "scan!")
  181. {
  182. // Serial.println("thingspeak");
  183. wifiscan();
  184. inData = "";//reset rs232 buffer
  185. }
  186. if (inData == "zoekbekendewifis!")
  187. {
  188. // Serial.println("thingspeak");
  189. zoekbekendewifis() ;
  190. inData = "";//reset rs232 buffer
  191. }
  192. }
  193. }
  194. }
  195. ////////////
  196. void setup() {
  197. Serial.begin(115200);
  198. pinMode(0, OUTPUT); //1 is de ingebouwde blauwe led op de esp-01 //2 is pin2 //0 is pin1
  199. pinMode(2, OUTPUT); //1 is de ingebouwde blauwe led op de esp-01 //2 is pin2 //0 is pin1
  200. digitalWrite(0, LOW); // led uit
  201. digitalWrite(2, LOW); // led uit
  202. Serial.println("v12345vtm");
  203. zoekbekendewifis();
  204. }
  205.  
  206. void loop() {
  207. uart();
  208. if (lokatie == "20" || lokatie == "10")
  209. {
  210. //we hebben al verbinding gehad , dus niks moet nog
  211. lokatie = "5";
  212. delay(60000); //reset de lokatie waarde en wacht een minuut , zo kan je wegrijden en is de lokatiecode gereset
  213. digitalWrite(0, LOW); // led uit
  214. digitalWrite(2, LOW); // led uit
  215. Serial.println("---");
  216. delay(60000);
  217. Serial.println("-----");
  218. delay(60000);
  219. Serial.println("--------");
  220. delay(60000);
  221. }
  222. else
  223. {
  224. zoekbekendewifis();
  225. }
  226. Serial.println (millis());
  227. delay (1000);
  228. Serial.println("----");
  229. }
  230.  
  231. String getTime() {
  232. WiFiClient client;
  233. while (!!!client.connect("google.com", 80)) {
  234. Serial.println("connection failed, retrying...");
  235. }
  236. client.print("HEAD / HTTP/1.1\r\n\r\n");
  237. while (!!!client.available()) {
  238. yield();
  239. }
  240. while (client.available()) {
  241. if (client.read() == '\n') {
  242. if (client.read() == 'D') {
  243. if (client.read() == 'a') {
  244. if (client.read() == 't') {
  245. if (client.read() == 'e') {
  246. if (client.read() == ':') {
  247. client.read();
  248. theDate = client.readStringUntil('\r');
  249. client.stop();
  250. return theDate; //Sat, 05 Nov 2016 13:30:29 GMT
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259.  
  260. void initWifi() {
  261. //WiFi.status() =
  262. // WL_NO_SHIELD = 255,
  263. //WL_IDLE_STATUS = 0,
  264. //WL_NO_SSID_AVAIL = 1
  265. //WL_SCAN_COMPLETED = 2
  266. //WL_CONNECTED = 3
  267. //WL_CONNECT_FAILED = 4
  268. //WL_CONNECTION_LOST = 5
  269. //WL_DISCONNECTED = 6
  270. Serial.print(ssid);
  271. WiFi.begin(ssid, password);
  272.  
  273. while (WiFi.status() != WL_CONNECTED) {
  274. delay(500);
  275. if (WiFi.status() == 1) //WL_NO_SSID_AVAIL = 1
  276. {
  277. Serial.println("1e ssid not found , try 2nd ssid ");
  278. }
  279. }
  280. Serial.print("\nWiFi connected, IP address: ");
  281. Serial.println(WiFi.localIP());
  282. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement