Advertisement
ViktorVanDarma

ESP_v_3_3

Oct 2nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include <WiFiClient.h>
  2. #include <ESP8266WebServer.h>
  3. #include <ESP8266mDNS.h>
  4. #include <ESP8266WiFiMulti.h>
  5. #include <ESP8266HTTPClient.h>
  6. #include <string.h>
  7.  
  8.  
  9.  
  10.  
  11. const char* ssidS = "" ;
  12. const char* passwordS = "";
  13.  
  14. //String Adres = "http://sag-wataha.org.pl/wataha/dane_zapis.php?";
  15.  
  16. HTTPClient http;
  17.  
  18. int polaczenie = 0 ;
  19.  
  20. String fun = "" ;
  21. String wiadomosc = "" ;
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. void setup() {
  30. WiFi.disconnect();
  31. Serial.begin(9600);
  32. Serial.flush();
  33. Serial.println("");
  34.  
  35. }
  36.  
  37.  
  38.  
  39. void loop() {
  40. if (polaczenie == 1) {
  41. if (WiFi.status() != WL_CONNECTED) szukajwifi(ssidS , passwordS );
  42. }
  43. if (Serial.available())
  44. {
  45. String Dane_atmega = Serial.readString();
  46. if (Dane_atmega.length() > 3 ) {
  47. fun = Dane_atmega[0] ;
  48. fun += Dane_atmega[1];
  49. Serial.println(fun);
  50. }
  51.  
  52. if ( fun == "01") {
  53. //Serial.println("wybieram fun 1");
  54. int ileznakow = Dane_atmega.length() ;
  55. int jaki = 0 ;
  56. String dzielnik = "_" ;
  57. String ssidf = "";
  58. String passf = "";
  59. for (int a = 2 ; a < ileznakow ; a++)
  60. {
  61. if ((Dane_atmega[a]) == dzielnik[0] ) {
  62. jaki = 1 ;
  63. continue ;
  64. }
  65. if (jaki == 0) {
  66. ssidf += Dane_atmega[a] ;
  67. }
  68. if (jaki == 1 ) {
  69. passf += Dane_atmega[a] ;
  70. }
  71.  
  72. }
  73.  
  74. szukajwifi(ssidf , passf ) ;
  75.  
  76. }
  77.  
  78.  
  79. else if ( fun == "02") {
  80. //Serial.println("wybieram fun 1");
  81. int ileznakow = Dane_atmega.length() ;
  82.  
  83. for (int a = 2 ; a < ileznakow ; a++)
  84. {
  85. wiadomosc += Dane_atmega[a] ;
  86. }
  87.  
  88. dodajdobazydanuch (wiadomosc ) ;
  89. wiadomosc = "";
  90. }
  91.  
  92.  
  93. else if ( fun == "03") {
  94. //Serial.println("wybieram fun 1");
  95. int ileznakow = Dane_atmega.length() ;
  96.  
  97. for (int a = 2 ; a < ileznakow ; a++)
  98. {
  99. wiadomosc += Dane_atmega[a] ;
  100. }
  101.  
  102. odczytaja_z_bazy (wiadomosc ) ;
  103. wiadomosc = "";
  104. }
  105.  
  106. else {
  107. Serial.println("funkcja nie zaimplementowana") ;
  108. }
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115. }
  116.  
  117. void szukajwifi( String ssid , String password ) {
  118.  
  119. ssidS = ssid.c_str() ;
  120. passwordS = password.c_str() ;
  121.  
  122. //passwordS = password ;
  123. WiFi.disconnect();
  124. delay(200);
  125. WiFi.mode(WIFI_STA);
  126. WiFi.begin(ssidS, passwordS);
  127. Serial.println("rozlaczony, szukam sieci...");
  128. while (WiFi.status() != WL_CONNECTED) {
  129. delay(200);
  130. // Serial.print(".");
  131. }
  132. Serial.println(" ");
  133. Serial.println("Polaczony z siecia");
  134. Serial.println(WiFi.localIP());
  135. //Serial.println("OK");
  136. fun = "" ;
  137. wiadomosc = "" ;
  138. polaczenie = 1 ;
  139.  
  140. }
  141.  
  142.  
  143. void dodajdobazydanuch ( String Dane_atmega ) {
  144.  
  145. String URL_dodania = Dane_atmega ;
  146. Serial.println(URL_dodania);
  147. http.begin(URL_dodania);
  148. int httpCode = http.GET();
  149. delay (10);
  150. http.end();
  151. URL_dodania = "" ;
  152. }
  153.  
  154.  
  155. void odczytaja_z_bazy ( String Dane_atmega ) {
  156.  
  157. String URL_dodania = Dane_atmega ;
  158. String dzielnik = "&" ;
  159. //Serial.println(URL_dodania);
  160. http.begin(URL_dodania);
  161. int httpCode = http.GET();
  162. String payload = http.getString();
  163.  
  164. /// jesli chcesz przesylac calego stringa danych do atmegi to odkomentuj
  165. Serial.println(payload);
  166. ///////////////////////--------------------------------------------\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  167. delay (5);
  168. http.end();
  169. URL_dodania = "" ;
  170.  
  171. /// jesli chcesz przesylac calego stringa danych do atmegi to usun lub zakomentuj pod spodem wszystko
  172. // int jaki = 0 ;
  173. // String calosc [10];
  174. // int ileznakow = payload.length() ;
  175. // for (int a = 0 ; a < ileznakow ; a++)
  176. // {
  177. // if ((payload[a]) == dzielnik[0] ) {
  178. // Serial.println(calosc[jaki]) ;
  179. // jaki ++ ;
  180. // continue ;
  181. // }
  182. // calosc[jaki] += payload[a] ;
  183. // }
  184. ///////////////////////--------------------------------------------\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement