Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266HTTPClient.h>
- #include <ESP8266WiFi.h>
- #include <SoftwareSerial.h>
- SoftwareSerial mySerial(D1, D4);
- const byte numChars = 32;
- char receivedChars[numChars];
- char tempChars[numChars];
- char Message[numChars] = { 0 };
- boolean newData = false;
- /* New Variable`s */
- int network_counter;
- bool network;
- unsigned long led_timer;
- /* Function`s */
- /* LED Blink */
- void led_indication() {
- if(!network) {
- if((millis() - led_timer) >= 500) {
- if(digitalRead(LED_BUILTIN) == LOW) {
- digitalWrite(LED_BUILTIN, HIGH);
- } else {
- digitalWrite(LED_BUILTIN, LOW);
- }
- led_timer = millis();
- }
- }
- if((network) && (digitalRead(LED_BUILTIN)) == HIGH) {
- digitalWrite(LED_BUILTIN, LOW);
- }
- }
- /* Check network state */
- void network_check() {
- if((WiFi.status() != WL_CONNECTED) && (network)) {
- led_timer = millis();
- network = false; // Update network status (Wifi off)
- } else if((WiFi.status() == WL_CONNECTED) && (!network)) {
- network = true; // Update network status (Wifi on)
- }
- }
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT);
- mySerial.begin(9600);
- digitalWrite(LED_BUILTIN, LOW);
- WiFi.begin("Keenetic-8481", "k3wkfCkd");
- while((WiFi.status() != WL_CONNECTED) && (network_counter <= 5)) {
- delay(300);
- network_counter++;
- }
- }
- void loop()
- {
- delay(5000);
- HTTPClient http;
- http.begin("http://wifitest7410.000webhostapp.com/system.php");
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- http.POST("temp1=77");
- http.writeToStream(&Serial);
- http.end();
- /* Check wifi network */
- network_check();
- /* Led blink */
- led_indication();
- /* UART bus */
- recvWithStartEndMarkers();
- if(newData == true) {
- strcpy(tempChars, receivedChars);
- parseData();
- msgData();
- newData = false;
- }
- }
- // RAPI Message Protocol
- void recvWithStartEndMarkers()
- {
- static boolean recvInProgress = false;
- static byte ndx = 0;
- char startMarker = '<';
- char endMarker = '>';
- char rc;
- while (mySerial.available() > 0 && newData == false) {
- rc = mySerial.read();
- if (recvInProgress == true) {
- if (rc != endMarker) {
- receivedChars[ndx] = rc;
- ndx++;
- if (ndx >= numChars) {
- ndx = numChars - 1;
- }
- }
- else {
- receivedChars[ndx] = '\0'; // terminate the string
- recvInProgress = false;
- ndx = 0;
- newData = true;
- }
- }
- else if (rc == startMarker) {
- recvInProgress = true;
- }
- }
- }
- // RAPI Message Protocol
- void parseData()
- { // split the data into its parts
- char* strtokIndx; // this is used by strtok() as an index
- strtokIndx = strtok(tempChars, ","); // get the first part - the string
- strcpy(Message, strtokIndx); // copy it to messageFromPC
- }
- void msgData() {
- if(Message != "") {
- //Serial.println(Message);
- /*HTTPClient http;
- http.begin("http://wifitest7410.000webhostapp.com/system.php");
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- http.POST("temp1=77");
- http.writeToStream(&Serial);
- http.end();*/
- }
- /*if(strcmp("130", Message) == 0) {
- digitalWrite(LED_BUILTIN, HIGH);
- } else if(strcmp("150", Message) == 0) {
- digitalWrite(LED_BUILTIN, LOW);
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement