Advertisement
Guest User

Untitled

a guest
May 25th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 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. /* Check wifi network */
  61. network_check();
  62. /* Led blink */
  63. led_indication();
  64. /* UART bus */
  65. recvWithStartEndMarkers();
  66. if(newData == true) {
  67. strcpy(tempChars, receivedChars);
  68. parseData();
  69. msgData();
  70. newData = false;
  71. }
  72. }
  73.  
  74. // RAPI Message Protocol
  75. void recvWithStartEndMarkers()
  76. {
  77. static boolean recvInProgress = false;
  78. static byte ndx = 0;
  79. char startMarker = '<';
  80. char endMarker = '>';
  81. char rc;
  82.  
  83. while (mySerial.available() > 0 && newData == false) {
  84. rc = mySerial.read();
  85.  
  86. if (recvInProgress == true) {
  87. if (rc != endMarker) {
  88. receivedChars[ndx] = rc;
  89. ndx++;
  90. if (ndx >= numChars) {
  91. ndx = numChars - 1;
  92. }
  93. }
  94. else {
  95. receivedChars[ndx] = '\0'; // terminate the string
  96. recvInProgress = false;
  97. ndx = 0;
  98. newData = true;
  99. }
  100. }
  101. else if (rc == startMarker) {
  102. recvInProgress = true;
  103. }
  104. }
  105. }
  106.  
  107. // RAPI Message Protocol
  108. void parseData()
  109. { // split the data into its parts
  110. char* strtokIndx; // this is used by strtok() as an index
  111. strtokIndx = strtok(tempChars, ","); // get the first part - the string
  112. strcpy(Message, strtokIndx); // copy it to messageFromPC
  113. }
  114.  
  115. void msgData() {
  116. if(Message != "") {
  117. //Serial.println(Message);
  118. }
  119.  
  120. /*if(strcmp("130", Message) == 0) {
  121. digitalWrite(LED_BUILTIN, HIGH);
  122. } else if(strcmp("150", Message) == 0) {
  123. digitalWrite(LED_BUILTIN, LOW);
  124. }*/
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement