Advertisement
Guest User

Untitled

a guest
May 25th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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.  
  17. /* Function`s */
  18. /* LED Blink */
  19. void led_indication() {
  20. static unsigned long led_timer;
  21. if(!network) {
  22. if((millis() - led_timer) >= 500) {
  23. if(digitalRead(LED_BUILTIN) == HIGH) {
  24. digitalWrite(LED_BUILTIN, LOW);
  25. } else {
  26. digitalWrite(LED_BUILTIN, HIGH);
  27. }
  28. led_timer = millis();
  29. }
  30. }
  31. if((network) && (digitalRead(LED_BUILTIN)) == LOW) {
  32. digitalWrite(LED_BUILTIN, HIGH);
  33. }
  34. }
  35.  
  36. /* Check network state */
  37. void network_check() {
  38. if((WiFi.status() != WL_CONNECTED) && (network)) {
  39. network = false; // Update network status (Wifi off)
  40. } else if((WiFi.status() == WL_CONNECTED) && (!network)) {
  41. network = true; // Update network status (Wifi on)
  42. }
  43. }
  44.  
  45. void setup()
  46. {
  47. pinMode(LED_BUILTIN, OUTPUT);
  48. mySerial.begin(9600);
  49. WiFi.begin("Keenetic-8481", "k3wkfCkd");
  50. while((WiFi.status() != WL_CONNECTED) && (network_counter <= 5)) {
  51. delay(300);
  52. network_counter++;
  53. }
  54. }
  55.  
  56. void loop()
  57. {
  58. /* Check wifi network */
  59. network_check();
  60. /* Led blink */
  61. led_indication();
  62. /* UART bus */
  63. recvWithStartEndMarkers();
  64. if(newData == true) {
  65. strcpy(tempChars, receivedChars);
  66. parseData();
  67. msgData();
  68. newData = false;
  69. }
  70. }
  71.  
  72. // RAPI Message Protocol
  73. void recvWithStartEndMarkers()
  74. {
  75. static boolean recvInProgress = false;
  76. static byte ndx = 0;
  77. char startMarker = '<';
  78. char endMarker = '>';
  79. char rc;
  80.  
  81. while (mySerial.available() > 0 && newData == false) {
  82. rc = mySerial.read();
  83.  
  84. if (recvInProgress == true) {
  85. if (rc != endMarker) {
  86. receivedChars[ndx] = rc;
  87. ndx++;
  88. if (ndx >= numChars) {
  89. ndx = numChars - 1;
  90. }
  91. }
  92. else {
  93. receivedChars[ndx] = '\0'; // terminate the string
  94. recvInProgress = false;
  95. ndx = 0;
  96. newData = true;
  97. }
  98. }
  99. else if (rc == startMarker) {
  100. recvInProgress = true;
  101. }
  102. }
  103. }
  104.  
  105. // RAPI Message Protocol
  106. void parseData()
  107. { // split the data into its parts
  108. char* strtokIndx; // this is used by strtok() as an index
  109. strtokIndx = strtok(tempChars, ","); // get the first part - the string
  110. strcpy(Message, strtokIndx); // copy it to messageFromPC
  111. }
  112.  
  113. void msgData() {
  114. if(Message != "") {
  115. //Serial.println(Message);
  116. }
  117.  
  118. /*if(strcmp("130", Message) == 0) {
  119. digitalWrite(LED_BUILTIN, HIGH);
  120. } else if(strcmp("150", Message) == 0) {
  121. digitalWrite(LED_BUILTIN, LOW);
  122. }*/
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement