Advertisement
Guest User

Untitled

a guest
May 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. SoftwareSerial mySerial(D1, D4);
  6.  
  7. const byte numChars = 32;
  8. char receivedChars[numChars];
  9. char tempChars[numChars];
  10. char Message[numChars] = { 0 };
  11. boolean newData = false;
  12.  
  13. /* New Variable`s */
  14. int network_counter;
  15. bool network;
  16. unsigned long led_timer;
  17.  
  18. /* Function`s */
  19. /* LED Blink */
  20. void led_indication() {
  21. if(!network) {
  22. if((millis() - led_timer) >= 500) {
  23. if(digitalRead(LED_BUILTIN) == LOW) {
  24. digitalWrite(LED_BUILTIN, HIGH);
  25. } else {
  26. digitalWrite(LED_BUILTIN, LOW);
  27. }
  28. led_timer = millis();
  29. }
  30. }
  31. if((network) && (digitalRead(LED_BUILTIN)) == HIGH) {
  32. digitalWrite(LED_BUILTIN, LOW);
  33. }
  34. }
  35.  
  36. /* Check network state */
  37. void network_check() {
  38. if((WiFi.status() != WL_CONNECTED) && (network)) {
  39. led_timer = millis();
  40. network = false; // Update network status (Wifi off)
  41. } else if((WiFi.status() == WL_CONNECTED) && (!network)) {
  42. network = true; // Update network status (Wifi on)
  43. }
  44. }
  45.  
  46. void setup()
  47. {
  48. pinMode(LED_BUILTIN, OUTPUT);
  49. mySerial.begin(9600);
  50. digitalWrite(LED_BUILTIN, LOW);
  51. WiFi.begin("Keenetic-8481", "k3wkfCkd");
  52. while((WiFi.status() != WL_CONNECTED) && (network_counter <= 5)) {
  53. delay(300);
  54. network_counter++;
  55. }
  56. }
  57.  
  58. void loop()
  59. {
  60. delay(5000);
  61. HTTPClient http;
  62. http.begin("http://wifitest7410.000webhostapp.com/system.php");
  63. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  64. http.POST("temp1=77");
  65. http.writeToStream(&Serial);
  66. http.end();
  67. /* Check wifi network */
  68. network_check();
  69. /* Led blink */
  70. led_indication();
  71. /* UART bus */
  72. recvWithStartEndMarkers();
  73. if(newData == true) {
  74. strcpy(tempChars, receivedChars);
  75. parseData();
  76. msgData();
  77. newData = false;
  78. }
  79. }
  80.  
  81. // RAPI Message Protocol
  82. void recvWithStartEndMarkers()
  83. {
  84. static boolean recvInProgress = false;
  85. static byte ndx = 0;
  86. char startMarker = '<';
  87. char endMarker = '>';
  88. char rc;
  89.  
  90. while (mySerial.available() > 0 && newData == false) {
  91. rc = mySerial.read();
  92.  
  93. if (recvInProgress == true) {
  94. if (rc != endMarker) {
  95. receivedChars[ndx] = rc;
  96. ndx++;
  97. if (ndx >= numChars) {
  98. ndx = numChars - 1;
  99. }
  100. }
  101. else {
  102. receivedChars[ndx] = '\0'; // terminate the string
  103. recvInProgress = false;
  104. ndx = 0;
  105. newData = true;
  106. }
  107. }
  108. else if (rc == startMarker) {
  109. recvInProgress = true;
  110. }
  111. }
  112. }
  113.  
  114. // RAPI Message Protocol
  115. void parseData()
  116. { // split the data into its parts
  117. char* strtokIndx; // this is used by strtok() as an index
  118. strtokIndx = strtok(tempChars, ","); // get the first part - the string
  119. strcpy(Message, strtokIndx); // copy it to messageFromPC
  120. }
  121.  
  122. void msgData() {
  123. if(Message != "") {
  124. //Serial.println(Message);
  125. /*HTTPClient http;
  126. http.begin("http://wifitest7410.000webhostapp.com/system.php");
  127. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  128. http.POST("temp1=77");
  129. http.writeToStream(&Serial);
  130. http.end();*/
  131. }
  132.  
  133. /*if(strcmp("130", Message) == 0) {
  134. digitalWrite(LED_BUILTIN, HIGH);
  135. } else if(strcmp("150", Message) == 0) {
  136. digitalWrite(LED_BUILTIN, LOW);
  137. }*/
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement