본문
JSON 통신(Arduino/Wifi shield)
사물인터넷(IoT) 2015. 2. 27. 21:44
반응형
# JSON 통신 (Arduino/Wifi shield)
1. What is it
IoT 기반 '스마트 주문기'라는 프로젝트 구현 중 사용했던 Arduino, Wifi shield 기반 JSON 통신 소스
2. Summary
아두이노와 아두이노 와이파이쉴드를 이용하여 JSON 통신으로 서버와 데이터 통신
H/W(아두이노, 아두이노 와이파이쉴드)에 장착된 버튼을 누르면 POST방식으로 서버에 데이터 요청
웹서버는 /insertOrder, /completeOrder 으로 상황에 따른 작업 수행
3. Info
- Source 상 서버 주소 : 117.16.231.212:8010
- LED 동작 로직 :
*No.1 LED ON-
: 프로그램 시작과 동시에 LED ON(손님 착석, But 주문 하지 않은 상태)
No.1 LED OFF -
: 메뉴A,B,C중 버튼 하나라도 누르면 첫번째 LED OFF
*No.2 LED ON -
: 주문 중 / 주문 완료 일 때 LED ON
No.2 LED OFF -
: 서빙을 완료했을 때 LED OFF
Source)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include <SPI.h> #include <WiFi.h> //LED pin //프로그램 시작과 동시에 LED ON ( 손님 착석, But 주문 하지 않은 상태 ) //->메뉴A,B,C중 버튼 하나라도 누르면 첫번째 LED OFF int ledPin09 = 9; int ledPin10 = 10; int ledPin11 = 11; //button for confirm the order int currentButtonState; //input selected buttonPin int buttonPin2 = 2; //menu A int buttonPin3 = 3; //menu B int buttonPin4 = 4; //menu C int buttonPin5 = 5; //Complete Order int buttonPin6 = 6; //Complete Serving(사용자가 주문한 음식이 테이블에 도착했을 때) char ssid[] = "U+village"; // your network SSID (name) char pass[] = "lguplus100"; // your network password int status = WL_IDLE_STATUS; char servername[]="117.16.231.212"; // == IPAddress servernaeme(117,16,231,212); WiFiClient client; void setup() { //LED Section pinMode(ledPin09, OUTPUT); pinMode(ledPin10, OUTPUT); pinMode(ledPin11, OUTPUT); //Button Section pinMode(buttonPin2, INPUT); //Pin 2 pinMode(buttonPin3, INPUT); //Pin 3 pinMode(buttonPin4, INPUT); //Pin 4 pinMode(buttonPin5, INPUT); //Pin 5 pinMode(buttonPin6, INPUT); //Pin 6 Serial.begin(9600); Serial.println("Attempting to connect to WPA2 network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); Serial.println("Connected to wifi"); Serial.println("\nStarting connection..."); if (client.connect(servername, 8010)) { Serial.println("= = = YAHA Project Start = = ="); //Connected digitalWrite(ledPin09, 1); } digitalWrite(ledPin10, 0); } void loop() { String data,complete; int buttonPin_2 = digitalRead(buttonPin2); int buttonPin_3 = digitalRead(buttonPin3); int buttonPin_4 = digitalRead(buttonPin4); int buttonPin_5 = digitalRead(buttonPin5); int buttonPin_6 = digitalRead(buttonPin6); if(buttonPin_2){ currentButtonState = 2; } if(buttonPin_3){ currentButtonState = 3; } if(buttonPin_4){ currentButtonState = 4; } //Complete Order if(buttonPin_5){ complete+="no=66"; client.println("POST /completeOrder HTTP/1.1"); client.println("Host: 117.16.231.212:8010"); client.println("Content-Type: application/x-www-form-urlencoded"); client.print("Content-Length: "); client.println(complete.length()); client.println(); client.print(complete); client.println(); Serial.println("Complete Order of Order-Number 66 "); } //Complete Serving if(buttonPin_6){ digitalWrite(ledPin10, 0); Serial.println("Complete Serving"); } switch(currentButtonState){ case 2: data+="menu=A&tableName=1"; client.println("POST /insertOrder HTTP/1.1"); client.println("Host: 117.16.231.212:8010"); client.println("Content-Type: application/x-www-form-urlencoded"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.print(data); client.println(); Serial.println("Order Menu A"); digitalWrite(ledPin09, 0); buttonPin_2=0; currentButtonState=0; break; case 3: data+="menu=B&tableName=1"; client.println("POST /insertOrder HTTP/1.1"); client.println("Host: 117.16.231.212:8010"); client.println("Content-Type: application/x-www-form-urlencoded"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.print(data); client.println(); Serial.println("Order Menu B"); digitalWrite(ledPin09, 0); buttonPin_3=0; currentButtonState=0; break; case 4: data+="menu=C&tableName=1"; client.println("POST /insertOrder HTTP/1.1"); client.println("Host: 117.16.231.212:8010"); client.println("Content-Type: application/x-www-form-urlencoded"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.print(data); client.println(); Serial.println("Order Menu C"); digitalWrite(ledPin09, 0); buttonPin_4=0; currentButtonState=0; break; } } | cs |
반응형
댓글