Controlling Arduino State Machine via Web
This video shows how to change state of state machine on Arduino via web.
Components and supplies
1
USB Cable 2.0 Type A/B for Arduino Uno
3
7V LED By KINGBRIGHT
1
Breadboard
1
PHPoC Shield for Arduino
1
Arduino UNO
2
RELAY click
1
Jumper wires (generic)
1
Breadboard (generic)
1
Jumper Wires
Project description
Code
Normal code for controlling LED via webpage
arduino
1/* arduino web server - remote control (push button) */ 2 3#include "SPI.h" 4#include "Phpoc.h" 5 6#define STATE_YELLOW 'A' 7#define STATE_GREEN 'B' 8#define STATE_RED 'C' 9 10#define PIN_LED_YELLOW 3 11#define PIN_LED_GREEN 8 12#define PIN_LED_RED 9 13 14PhpocServer server(80); 15 16char current_led = STATE_RED; 17int pins[3] = {PIN_LED_YELLOW, PIN_LED_GREEN, PIN_LED_RED}; 18 19void handleEvent(char stt); 20 21void setup(){ 22 Serial.begin(9600); 23 while(!Serial) 24 ; 25 26 Phpoc.begin(PF_LOG_SPI | PF_LOG_NET); 27 //Phpoc.begin(); 28 29 server.beginWebSocket("remote_push"); 30 31 Serial.print("WebSocket server address : "); 32 Serial.println(Phpoc.localIP()); 33 34 pinMode(PIN_LED_YELLOW, OUTPUT); 35 pinMode(PIN_LED_GREEN, OUTPUT); 36 pinMode(PIN_LED_RED, OUTPUT); 37 38 digitalWrite(PIN_LED_RED, HIGH); 39 40} 41 42void loop() { 43 //Wait for a new client: 44 PhpocClient client = server.available(); 45 46 if (client) { 47 if (client.available() > 0) { 48 //Read the bytes incoming from the client: 49 char event = client.read(); 50 if(event == 'A' || event == 'B' || event == 'C') 51 if(event != current_led) 52 handleEvent(event); 53 } 54 } 55} 56 57void handleEvent(char event){ 58 59 for(int i = 0; i < 5; i++){ 60 digitalWrite(pins[current_led - 'A'], (i%2)); 61 delay(100); 62 } 63 //Turn on green led 64 digitalWrite(pins[event - 'A'], HIGH); 65 66 // update current state 67 current_led = event; 68}
Finite State Machine Code for controlling LED via webpage
arduino
This code have the same functionality as normal code but using Finite State Machine model
1/* arduino web server - remote control (push button) */ 2 3#include 4 "SPI.h" 5#include "Phpoc.h" 6 7#define STATE_YELLOW 'A' 8#define STATE_GREEN 'B' 9#define 10 STATE_RED 'C' 11 12#define PIN_LED_YELLOW 3 13#define PIN_LED_GREEN 8 14#define 15 PIN_LED_RED 9 16 17PhpocServer server(80); 18 19char fsm_state = STATE_RED; 20 21void 22 FSMEvent(char stt); 23 24void setup(){ 25 Serial.begin(9600); 26 while(!Serial) 27 ; 28 29 Phpoc.begin(PF_LOG_SPI 30 | PF_LOG_NET); 31 //Phpoc.begin(); 32 33 server.beginWebSocket("remote_push"); 34 35 Serial.print("WebSocket 36 server address : "); 37 Serial.println(Phpoc.localIP()); 38 39 pinMode(PIN_LED_YELLOW, 40 OUTPUT); 41 pinMode(PIN_LED_GREEN, OUTPUT); 42 pinMode(PIN_LED_RED, OUTPUT); 43 44 digitalWrite(PIN_LED_RED, 45 HIGH); 46} 47 48void loop() { 49 //Wait for a new client: 50 PhpocClient 51 client = server.available(); 52 53 if (client) { 54 if (client.available() 55 > 0) { 56 //Read the bytes incoming from the client: 57 char event = 58 client.read(); 59 if(event == 'A' || event == 'B' || event == 'C') 60 if(event 61 != fsm_state) 62 FSMEvent(event); 63 } 64 } 65} 66 67void FSMEvent(char 68 event){ 69 70 //Action is taken based on current state and event 71 switch(fsm_state){ 72 case 73 STATE_YELLOW: 74 75 if(event == STATE_GREEN){ 76 //Blink yellow 77 led in a second and then turn off 78 for(int i = 0; i < 5; i++){ 79 digitalWrite(PIN_LED_YELLOW, 80 (i%2)); 81 delay(100); 82 } 83 //Turn on green led 84 digitalWrite(PIN_LED_GREEN, 85 HIGH); 86 } 87 else if(event == STATE_RED){ 88 //Blink yellow 89 led in a second and then turn off 90 for(int i = 0; i < 5; i++){ 91 digitalWrite(PIN_LED_YELLOW, 92 (i%2)); 93 delay(100); 94 } 95 //Turn on red led 96 digitalWrite(PIN_LED_RED, 97 HIGH); 98 } 99 100 break; 101 102 case STATE_GREEN: 103 104 if(event 105 == STATE_YELLOW){ 106 //Blink green led in a second and then turn off 107 for(int 108 i = 0; i < 5; i++){ 109 digitalWrite(PIN_LED_GREEN, (i%2)); 110 delay(100); 111 } 112 //Turn 113 on yellow led 114 digitalWrite(PIN_LED_YELLOW, HIGH); 115 } 116 else 117 if(event == STATE_RED){ 118 //Blink green led in a second and then turn off 119 for(int 120 i = 0; i < 5; i++){ 121 digitalWrite(PIN_LED_GREEN, (i%2)); 122 delay(100); 123 } 124 //Turn 125 on red led 126 digitalWrite(PIN_LED_RED, HIGH); 127 } 128 129 break; 130 131 case 132 STATE_RED: 133 134 if(event == STATE_YELLOW){ 135 //Blink red 136 led in a second and then turn off 137 for(int i = 0; i < 5; i++){ 138 digitalWrite(PIN_LED_RED, 139 (i%2)); 140 delay(100); 141 } 142 //Turn on yellow led 143 digitalWrite(PIN_LED_YELLOW, 144 HIGH); 145 } 146 else if(event == STATE_GREEN){ 147 //Blink red 148 led in a second and then turn off 149 for(int i = 0; i < 5; i++){ 150 digitalWrite(PIN_LED_RED, 151 (i%2)); 152 delay(100); 153 } 154 //Turn on green led 155 digitalWrite(PIN_LED_GREEN, 156 HIGH); 157 } 158 159 break; 160 } 161 162 // update current 163 state 164 fsm_state = event; 165}
Finite State Machine Code for controlling LED via webpage
arduino
This code have the same functionality as normal code but using Finite State Machine model
1/* arduino web server - remote control (push button) */ 2 3#include "SPI.h" 4#include "Phpoc.h" 5 6#define STATE_YELLOW 'A' 7#define STATE_GREEN 'B' 8#define STATE_RED 'C' 9 10#define PIN_LED_YELLOW 3 11#define PIN_LED_GREEN 8 12#define PIN_LED_RED 9 13 14PhpocServer server(80); 15 16char fsm_state = STATE_RED; 17 18void FSMEvent(char stt); 19 20void setup(){ 21 Serial.begin(9600); 22 while(!Serial) 23 ; 24 25 Phpoc.begin(PF_LOG_SPI | PF_LOG_NET); 26 //Phpoc.begin(); 27 28 server.beginWebSocket("remote_push"); 29 30 Serial.print("WebSocket server address : "); 31 Serial.println(Phpoc.localIP()); 32 33 pinMode(PIN_LED_YELLOW, OUTPUT); 34 pinMode(PIN_LED_GREEN, OUTPUT); 35 pinMode(PIN_LED_RED, OUTPUT); 36 37 digitalWrite(PIN_LED_RED, HIGH); 38} 39 40void loop() { 41 //Wait for a new client: 42 PhpocClient client = server.available(); 43 44 if (client) { 45 if (client.available() > 0) { 46 //Read the bytes incoming from the client: 47 char event = client.read(); 48 if(event == 'A' || event == 'B' || event == 'C') 49 if(event != fsm_state) 50 FSMEvent(event); 51 } 52 } 53} 54 55void FSMEvent(char event){ 56 57 //Action is taken based on current state and event 58 switch(fsm_state){ 59 case STATE_YELLOW: 60 61 if(event == STATE_GREEN){ 62 //Blink yellow led in a second and then turn off 63 for(int i = 0; i < 5; i++){ 64 digitalWrite(PIN_LED_YELLOW, (i%2)); 65 delay(100); 66 } 67 //Turn on green led 68 digitalWrite(PIN_LED_GREEN, HIGH); 69 } 70 else if(event == STATE_RED){ 71 //Blink yellow led in a second and then turn off 72 for(int i = 0; i < 5; i++){ 73 digitalWrite(PIN_LED_YELLOW, (i%2)); 74 delay(100); 75 } 76 //Turn on red led 77 digitalWrite(PIN_LED_RED, HIGH); 78 } 79 80 break; 81 82 case STATE_GREEN: 83 84 if(event == STATE_YELLOW){ 85 //Blink green led in a second and then turn off 86 for(int i = 0; i < 5; i++){ 87 digitalWrite(PIN_LED_GREEN, (i%2)); 88 delay(100); 89 } 90 //Turn on yellow led 91 digitalWrite(PIN_LED_YELLOW, HIGH); 92 } 93 else if(event == STATE_RED){ 94 //Blink green led in a second and then turn off 95 for(int i = 0; i < 5; i++){ 96 digitalWrite(PIN_LED_GREEN, (i%2)); 97 delay(100); 98 } 99 //Turn on red led 100 digitalWrite(PIN_LED_RED, HIGH); 101 } 102 103 break; 104 105 case STATE_RED: 106 107 if(event == STATE_YELLOW){ 108 //Blink red led in a second and then turn off 109 for(int i = 0; i < 5; i++){ 110 digitalWrite(PIN_LED_RED, (i%2)); 111 delay(100); 112 } 113 //Turn on yellow led 114 digitalWrite(PIN_LED_YELLOW, HIGH); 115 } 116 else if(event == STATE_GREEN){ 117 //Blink red led in a second and then turn off 118 for(int i = 0; i < 5; i++){ 119 digitalWrite(PIN_LED_RED, (i%2)); 120 delay(100); 121 } 122 //Turn on green led 123 digitalWrite(PIN_LED_GREEN, HIGH); 124 } 125 126 break; 127 } 128 129 // update current state 130 fsm_state = event; 131}
Normal code for controlling LED via webpage
arduino
1/* arduino web server - remote control (push button) */ 2 3#include "SPI.h" 4#include "Phpoc.h" 5 6#define STATE_YELLOW 'A' 7#define STATE_GREEN 'B' 8#define STATE_RED 'C' 9 10#define PIN_LED_YELLOW 3 11#define PIN_LED_GREEN 8 12#define PIN_LED_RED 9 13 14PhpocServer server(80); 15 16char current_led = STATE_RED; 17int pins[3] = {PIN_LED_YELLOW, PIN_LED_GREEN, PIN_LED_RED}; 18 19void handleEvent(char stt); 20 21void setup(){ 22 Serial.begin(9600); 23 while(!Serial) 24 ; 25 26 Phpoc.begin(PF_LOG_SPI | PF_LOG_NET); 27 //Phpoc.begin(); 28 29 server.beginWebSocket("remote_push"); 30 31 Serial.print("WebSocket server address : "); 32 Serial.println(Phpoc.localIP()); 33 34 pinMode(PIN_LED_YELLOW, OUTPUT); 35 pinMode(PIN_LED_GREEN, OUTPUT); 36 pinMode(PIN_LED_RED, OUTPUT); 37 38 digitalWrite(PIN_LED_RED, HIGH); 39 40} 41 42void loop() { 43 //Wait for a new client: 44 PhpocClient client = server.available(); 45 46 if (client) { 47 if (client.available() > 0) { 48 //Read the bytes incoming from the client: 49 char event = client.read(); 50 if(event == 'A' || event == 'B' || event == 'C') 51 if(event != current_led) 52 handleEvent(event); 53 } 54 } 55} 56 57void handleEvent(char event){ 58 59 for(int i = 0; i < 5; i++){ 60 digitalWrite(pins[current_led - 'A'], (i%2)); 61 delay(100); 62 } 63 //Turn on green led 64 digitalWrite(pins[event - 'A'], HIGH); 65 66 // update current state 67 current_led = event; 68}
Downloadable files
Wiring
Wiring
Comments
Only logged in users can leave comments