Snack dispenser that is triggered by SMS over the internet

Dependencies:   EthernetNetIf TextLCD mbed AX12

Committer:
chris
Date:
Mon Apr 11 23:18:50 2011 +0000
Revision:
1:051c84fbac55
Parent:
0:529c89c58910

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:529c89c58910 1 #include "mbed.h"
chris 0:529c89c58910 2 #include "EthernetNetIf.h"
chris 0:529c89c58910 3 #include "HTTPClient.h"
chris 0:529c89c58910 4 #include "AX12.h"
chris 0:529c89c58910 5 #include "TextLCD.h"
chris 0:529c89c58910 6
chris 0:529c89c58910 7 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
chris 0:529c89c58910 8
chris 0:529c89c58910 9 DigitalOut led1(LED1);
chris 0:529c89c58910 10 DigitalOut led2(LED2);
chris 0:529c89c58910 11 DigitalOut led3(LED3);
chris 0:529c89c58910 12 DigitalOut led4(LED4);
chris 0:529c89c58910 13
chris 0:529c89c58910 14 AX12 sm1 (p9, p10, 1);
chris 0:529c89c58910 15 AX12 sm2 (p9, p10, 2);
chris 0:529c89c58910 16
chris 0:529c89c58910 17 EthernetNetIf eth;
chris 0:529c89c58910 18 HTTPClient http;
chris 0:529c89c58910 19
chris 0:529c89c58910 20 void DeliverSnack1 () {
chris 0:529c89c58910 21
chris 0:529c89c58910 22 float pos = 0.0;
chris 0:529c89c58910 23 float goal = 0.0;
chris 0:529c89c58910 24
chris 0:529c89c58910 25 led1 = 1;
chris 0:529c89c58910 26
chris 0:529c89c58910 27 // read the position of the servo
chris 0:529c89c58910 28 pos = sm1.GetPosition();
chris 0:529c89c58910 29
chris 0:529c89c58910 30 // work out where we need to position the servo
chris 0:529c89c58910 31 // we always align to 60^ boundary
chris 0:529c89c58910 32
chris 0:529c89c58910 33 sm1.SetCRSpeed(0.7);
chris 0:529c89c58910 34 if ((pos > 0.0) && (pos < 60.0)) {
chris 0:529c89c58910 35 goal = 60.0;
chris 0:529c89c58910 36 } else if ((pos > 60.0) && (pos < 120.0)) {
chris 0:529c89c58910 37 goal = 120.0;
chris 0:529c89c58910 38 } else if ((pos > 120.0) && (pos < 180.0)) {
chris 0:529c89c58910 39 goal = 180.0;
chris 0:529c89c58910 40 } else if ((pos > 180.0) && (pos < 240.0)) {
chris 0:529c89c58910 41 goal = 240.0;
chris 0:529c89c58910 42 } else if ((pos > 240.0) && (pos < 295.0)) {
chris 0:529c89c58910 43 goal = 295.0;
chris 0:529c89c58910 44 } else {
chris 0:529c89c58910 45 goal = 2.0;
chris 0:529c89c58910 46 }
chris 0:529c89c58910 47
chris 0:529c89c58910 48 do {
chris 0:529c89c58910 49 pos = sm1.GetPosition();
chris 0:529c89c58910 50 // there is a quirk in the 300-360 dead band of the AX12
chris 0:529c89c58910 51 // where it reads ~120^, briefly
chris 0:529c89c58910 52 if ((goal == 2.0) && (pos > 50.0)) {
chris 0:529c89c58910 53 pos = 0.0;
chris 0:529c89c58910 54 }
chris 0:529c89c58910 55 printf("%.2f\n",pos);
chris 0:529c89c58910 56 } while (pos < goal);
chris 0:529c89c58910 57
chris 0:529c89c58910 58
chris 0:529c89c58910 59 sm1.SetCRSpeed(0.0);
chris 0:529c89c58910 60
chris 0:529c89c58910 61 wait (2.0);
chris 0:529c89c58910 62 led1 = 0;
chris 0:529c89c58910 63 }
chris 0:529c89c58910 64
chris 0:529c89c58910 65 void DeliverSnack2 () {
chris 0:529c89c58910 66
chris 0:529c89c58910 67 float pos = 0.0;
chris 0:529c89c58910 68 float goal = 0.0;
chris 0:529c89c58910 69
chris 0:529c89c58910 70 led2 = 1;
chris 0:529c89c58910 71
chris 0:529c89c58910 72 // read the position of the servo
chris 0:529c89c58910 73 pos = sm2.GetPosition();
chris 0:529c89c58910 74
chris 0:529c89c58910 75 // work out where we need to position the servo
chris 0:529c89c58910 76 // we always align to 60^ boundary
chris 0:529c89c58910 77 sm2.SetCRSpeed(0.7);
chris 0:529c89c58910 78 if ((pos > 0.0) && (pos < 60.0)) {
chris 0:529c89c58910 79 goal = 60.0;
chris 0:529c89c58910 80 } else if ((pos > 60.0) && (pos < 120.0)) {
chris 0:529c89c58910 81 goal = 120.0;
chris 0:529c89c58910 82 } else if ((pos > 120.0) && (pos < 180.0)) {
chris 0:529c89c58910 83 goal = 180.0;
chris 0:529c89c58910 84 } else if ((pos > 180.0) && (pos < 240.0)) {
chris 0:529c89c58910 85 goal = 240.0;
chris 0:529c89c58910 86 } else if ((pos > 240.0) && (pos < 295.0)) {
chris 0:529c89c58910 87 goal = 295.0;
chris 0:529c89c58910 88 } else {
chris 0:529c89c58910 89 goal = 2.0;
chris 0:529c89c58910 90 }
chris 0:529c89c58910 91
chris 0:529c89c58910 92 do {
chris 0:529c89c58910 93 pos = sm2.GetPosition();
chris 0:529c89c58910 94 // there is a quirk in the 300-360 dead band of the AX12
chris 0:529c89c58910 95 // where it reads ~120^, briefly
chris 0:529c89c58910 96 if ((goal == 2.0) && (pos > 50.0)) {
chris 0:529c89c58910 97 pos = 0.0;
chris 0:529c89c58910 98 }
chris 0:529c89c58910 99 printf("%.2f\n",pos);
chris 0:529c89c58910 100 } while (pos < goal);
chris 0:529c89c58910 101
chris 0:529c89c58910 102 sm2.SetCRSpeed(0.0);
chris 0:529c89c58910 103
chris 0:529c89c58910 104 wait (2.0);
chris 0:529c89c58910 105 led2 = 0;
chris 0:529c89c58910 106 }
chris 0:529c89c58910 107
chris 0:529c89c58910 108
chris 0:529c89c58910 109 int main() {
chris 0:529c89c58910 110
chris 0:529c89c58910 111 lcd.cls();
chris 0:529c89c58910 112 lcd.locate(0,0);
chris 0:529c89c58910 113 lcd.printf("Snack-o-tron");
chris 0:529c89c58910 114 lcd.locate(0,1);
chris 0:529c89c58910 115
chris 0:529c89c58910 116 // set Servo to continusous rotation
chris 0:529c89c58910 117 lcd.printf(" Init Motors");
chris 0:529c89c58910 118 //sm1.SetMode(1);
chris 0:529c89c58910 119 wait(0.5);
chris 0:529c89c58910 120
chris 0:529c89c58910 121 lcd.locate(0,1);
chris 0:529c89c58910 122 lcd.printf(" Done ");
chris 0:529c89c58910 123 wait(0.5);
chris 0:529c89c58910 124
chris 0:529c89c58910 125 lcd.locate(0,1);
chris 0:529c89c58910 126 lcd.printf(" Init Network");
chris 0:529c89c58910 127
chris 0:529c89c58910 128 printf("Setting up...\n");
chris 0:529c89c58910 129 EthernetErr ethErr = eth.setup();
chris 0:529c89c58910 130 if (ethErr) {
chris 0:529c89c58910 131 printf("Error %d in setup.\n", ethErr);
chris 0:529c89c58910 132 lcd.locate(0,1);
chris 0:529c89c58910 133 lcd.printf("Network Error %d",ethErr);
chris 0:529c89c58910 134 return -1;
chris 0:529c89c58910 135 }
chris 0:529c89c58910 136
chris 0:529c89c58910 137 // let the world know all is well!
chris 0:529c89c58910 138 printf("Setup OK\n");
chris 0:529c89c58910 139 lcd.locate(0,1);
chris 0:529c89c58910 140 lcd.printf(" Done ");
chris 0:529c89c58910 141 wait (2.0);
chris 0:529c89c58910 142 lcd.cls();
chris 0:529c89c58910 143
chris 0:529c89c58910 144 HTTPText txt;
chris 0:529c89c58910 145
chris 0:529c89c58910 146 while (1) {
chris 0:529c89c58910 147
chris 0:529c89c58910 148 lcd.cls();
chris 0:529c89c58910 149 lcd.locate(0,0);
chris 0:529c89c58910 150 lcd.printf("Ready");
chris 0:529c89c58910 151
chris 0:529c89c58910 152 led4 = 1; // this LED flashed with every HTTP request, for sanity
chris 1:051c84fbac55 153 HTTPResult r = http.get("http://example.com/snackcheck", &txt);
chris 0:529c89c58910 154
chris 0:529c89c58910 155 if (r==HTTP_OK) {
chris 0:529c89c58910 156
chris 0:529c89c58910 157 char sender[20];
chris 0:529c89c58910 158 char snack[20];
chris 0:529c89c58910 159
chris 0:529c89c58910 160 sscanf(txt.gets(),"%s\n%s\n",sender,snack);
chris 0:529c89c58910 161
chris 0:529c89c58910 162 if ( (strcmp ("0",sender)) == 0) {
chris 0:529c89c58910 163 printf("No snacks today\n");
chris 0:529c89c58910 164 } else {
chris 0:529c89c58910 165
chris 0:529c89c58910 166 printf("Sender : %s, Snack : %s\n", sender, snack);
chris 0:529c89c58910 167 lcd.cls();
chris 0:529c89c58910 168 lcd.locate(0,0);
chris 0:529c89c58910 169 lcd.printf("%s",sender);
chris 0:529c89c58910 170 lcd.locate(0,1);
chris 0:529c89c58910 171 lcd.printf("Snack %s",snack);
chris 0:529c89c58910 172
chris 0:529c89c58910 173 // What to do if snack 1 is ordered
chris 0:529c89c58910 174 if ( (strcmp ("1",snack)) == 0) {
chris 0:529c89c58910 175 DeliverSnack1();
chris 0:529c89c58910 176 }
chris 0:529c89c58910 177 // what to do if snack 2 is ordered
chris 0:529c89c58910 178 else if ( (strcmp ("2",snack)) == 0) {
chris 0:529c89c58910 179 DeliverSnack2();
chris 0:529c89c58910 180 }
chris 0:529c89c58910 181 }
chris 0:529c89c58910 182
chris 0:529c89c58910 183 } else {
chris 0:529c89c58910 184 // HTTP error
chris 0:529c89c58910 185 printf("Error %d\n", r);
chris 0:529c89c58910 186 }
chris 0:529c89c58910 187
chris 0:529c89c58910 188 // clear LEDs
chris 0:529c89c58910 189 led4 = 0;
chris 0:529c89c58910 190 wait (1.0);
chris 0:529c89c58910 191 }
chris 0:529c89c58910 192 }