|
| 1 | +/* |
| 2 | + Web Server |
| 3 | + |
| 4 | + A simple web server that shows the value of the analog input pins. |
| 5 | + blinks the built in LED, or toggles a Relay Switch (check the comments) |
| 6 | + |
| 7 | + From: earl@microcontrollerelectronics.com |
| 8 | + Also Demonstrates checking the ENC28J60 and keeping it from freezing. |
| 9 | +
|
| 10 | + https://microcontrollerelectronics.com/how-a-web-server-on-an-stm32f103c8t6-can-be-used-to-control-a-relay-switch/ |
| 11 | + |
| 12 | +ENC28J60 STM32F103 |
| 13 | +SCK PA5 |
| 14 | +SO PA6 |
| 15 | +SI PA7 |
| 16 | +CS PA4 |
| 17 | +RST R |
| 18 | + |
| 19 | +VCC - 5V |
| 20 | +GND - GND |
| 21 | + |
| 22 | +--ENC28J60-- |
| 23 | +CLK INT |
| 24 | +WOL SO |
| 25 | +SI SCK |
| 26 | +CS RESET |
| 27 | +VCC GND |
| 28 | + |
| 29 | + +-----------------[USB]-----------------+ |
| 30 | + [SS2|PB12] | [31] [Gnd] | |
| 31 | + [SCK2|PB13] | [30] +---+ [Gnd] | |
| 32 | +[MISO2|PB14] | [29] +-----+ |0 0| [3V3] | |
| 33 | +[MOSI2|PB15] | [28] |Reset| |x x| [Reset] | |
| 34 | + [PA8] | [27] +-----+ |1 1| [ 0] | [PB11|SDA2|RX3] |
| 35 | + [TX1|PA9] | [26] +---+ [ 1] | [PB10|SCL2|TX3] |
| 36 | + [RX1|PA10] | [25] ^ ^ [33] | [PB1] |
| 37 | + [USB-|PA11] | [24] Boot1--+ | [ 3] | [PB0|A0] |
| 38 | + [USB+|PA12] | [23] Boot0----+ [ 4] | [PA7|A1|MOSI1] |
| 39 | + [PA15] | [20] [ 5] | [PA6|A2|MISO1] |
| 40 | + [PB3] | [19] +-----------+ [ 6] | [PA5|A3|SCK1] |
| 41 | + [PB4] | [18] | STM32F103 | [ 7] | [PA4|A4|SS1] |
| 42 | + [PB5] | [17] | Blue Pill | [ 8] | [PA3|A5|RX2] |
| 43 | + [SCL1|PB6] | [16] +-----------+ [ 9] | [PA2|A6|TX2] |
| 44 | + [SDA1|PB7] | [15] [10] | [PA1|A7] |
| 45 | + [PB8] | [32] [11] | [PA0|A8] |
| 46 | + [PB9] | [PB9] [12] | [PC15] |
| 47 | + | [5V] +---------------+ [13] | [PC14] |
| 48 | + | [Gnd] | ST-Link | [14] | [PC13|LED] |
| 49 | + | [3V3] |3V3 DIO CLK GND| [Vbat]| |
| 50 | + +-------------+---+---+---+-------------+ |
| 51 | + | | | | |
| 52 | +*/ |
| 53 | + |
| 54 | +#include <libmaple/iwdg.h> |
| 55 | +#include <SPI.h> |
| 56 | +#include <UIPEthernet.h> |
| 57 | + |
| 58 | +#define NET_ENC28J60_EIR 0x1C |
| 59 | +#define NET_ENC28J60_ESTAT 0x1D |
| 60 | +#define NET_ENC28J60_ECON1 0x1F |
| 61 | +#define NET_ENC28J60_EIR_RXERIF 0x01 |
| 62 | +#define NET_ENC28J60_ESTAT_BUFFER 0x40 |
| 63 | +#define NET_ENC28J60_ECON1_RXEN 0x04 |
| 64 | +#define NET_ENC28J60_CHECK_PERIOD 5000UL |
| 65 | + |
| 66 | +#define iwdg_init_ms(N) iwdg_init(IWDG_PRE_256,((N)/5)) |
| 67 | + |
| 68 | +#define DHCP |
| 69 | + |
| 70 | +#define pinLED PC13 |
| 71 | +#define pinSwitch PA15 |
| 72 | +#define ETH_RS_PIN PA0 |
| 73 | + |
| 74 | +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
| 75 | + |
| 76 | +#ifndef DHCP |
| 77 | +IPAddress ipdns(192, 168, 1, 1); |
| 78 | +IPAddress ip(192, 168, 1, 7);//IP fixo |
| 79 | +IPAddress gateway(192, 168, 1, 1); |
| 80 | +IPAddress subnet(255, 255, 255, 0); |
| 81 | +#endif |
| 82 | + |
| 83 | +EthernetServer server = EthernetServer(80); |
| 84 | + |
| 85 | +String cdata = ""; |
| 86 | +unsigned long timer; |
| 87 | + |
| 88 | +void setup() |
| 89 | +{ |
| 90 | + disableDebugPorts(); |
| 91 | + |
| 92 | + pinMode(pinLED, OUTPUT); |
| 93 | + pinMode(pinSwitch, OUTPUT); |
| 94 | + digitalWrite(pinLED,LOW); |
| 95 | + digitalWrite(pinSwitch,LOW); |
| 96 | + eth_reset(); |
| 97 | + iwdg_init_ms(4000); |
| 98 | + timer = millis(); |
| 99 | +} |
| 100 | + |
| 101 | +void eth_reset() |
| 102 | +{ |
| 103 | + pinMode(ETH_RS_PIN, OUTPUT); |
| 104 | + digitalWrite(ETH_RS_PIN, LOW); |
| 105 | + delay(100); |
| 106 | + digitalWrite(ETH_RS_PIN, HIGH); |
| 107 | + pinMode(ETH_RS_PIN, INPUT); |
| 108 | + |
| 109 | +#ifdef DHCP |
| 110 | + Ethernet.begin(mac); |
| 111 | +#else |
| 112 | + Ethernet.begin(mac, ip, ipdns, gateway, subnet); |
| 113 | +#endif |
| 114 | + |
| 115 | + server.begin(); |
| 116 | +} |
| 117 | + |
| 118 | +void loop() |
| 119 | +{ |
| 120 | + iwdg_feed(); |
| 121 | + Ethernet.maintain(); |
| 122 | + |
| 123 | + if ((millis() - timer) > NET_ENC28J60_CHECK_PERIOD) |
| 124 | + { |
| 125 | + uint8_t stateEconRxen = Enc28J60.readReg((uint8_t) NET_ENC28J60_ECON1) & NET_ENC28J60_ECON1_RXEN; |
| 126 | + uint8_t stateEstatBuffer = Enc28J60.readReg((uint8_t) NET_ENC28J60_ESTAT) & NET_ENC28J60_ESTAT_BUFFER; |
| 127 | + uint8_t stateEirRxerif = Enc28J60.readReg((uint8_t) NET_ENC28J60_EIR) & NET_ENC28J60_EIR_RXERIF; |
| 128 | + |
| 129 | + if (!stateEconRxen || (stateEstatBuffer && stateEirRxerif)) |
| 130 | + { |
| 131 | + //Enc28J60.init(netConfig->macAddress);///////////////////////////////////// |
| 132 | + eth_reset(); |
| 133 | + } |
| 134 | + timer = millis(); |
| 135 | + } |
| 136 | + |
| 137 | + EthernetClient client = server.available(); |
| 138 | + |
| 139 | + if(client) |
| 140 | + { |
| 141 | + cdata = ""; |
| 142 | + |
| 143 | + while(client.connected()) |
| 144 | + { |
| 145 | + if(client.available()) |
| 146 | + { |
| 147 | + char c = client.read(); |
| 148 | + |
| 149 | + cdata.concat(c); |
| 150 | + if(cdata.indexOf("\r\n\r\n") > 0) |
| 151 | + { |
| 152 | + client.println(F("HTTP/1.1 200 OK")); |
| 153 | + client.println(F("Content-Type: text/html")); |
| 154 | + client.println(F("Connection: close")); |
| 155 | + if(cdata.indexOf("Analog") > 0) client.println(F("Refresh: 5")); |
| 156 | + client.println(); |
| 157 | + client.println(F("<!DOCTYPE HTML>")); |
| 158 | + client.println(F("<html>")); |
| 159 | + client.println(F("<body>")); |
| 160 | + client.println(F("<br><br><center><H1>STM32F103C8T6 WEB Server</H1></center>")); |
| 161 | + |
| 162 | + if((cdata.indexOf("LED") > 0) || (cdata.indexOf("led") > 0)) |
| 163 | + { |
| 164 | + digitalWrite(pinLED,!digitalRead(pinLED)); |
| 165 | + if (digitalRead(pinLED)) client.println(F("<center><br><br><br><br><H1>LED OFF!</H1></center>")); |
| 166 | + else client.println(F("<center><br><br><br><br><H1>LED ON!</H1></center>")); |
| 167 | + client.println(F("</body>")); |
| 168 | + client.println(F("</html>")); |
| 169 | + break; |
| 170 | + } |
| 171 | + |
| 172 | + if (cdata.indexOf("switch") > 0) |
| 173 | + { |
| 174 | + digitalWrite(pinSwitch,!digitalRead(pinSwitch)); |
| 175 | + if (digitalRead(pinSwitch)) client.println(F("<center><br><br><br><br><H1>Switch ON! </H1></center>")); |
| 176 | + else client.println(F("<center><br><br><br><br><H1>Switch OFF!</H1></center>")); |
| 177 | + client.println(F("</body>")); |
| 178 | + client.println(F("</html>")); |
| 179 | + break; |
| 180 | + } |
| 181 | + |
| 182 | + if(cdata.indexOf("Analog") > 0) |
| 183 | + { |
| 184 | + client.println(F("<table border=1 cellspacing=4 cellpadding=4>")); |
| 185 | + client.println(F("<tr><th>Analog</th><th>Value</th></tr>")); |
| 186 | + for (int analogChannel = 0; analogChannel < 4; analogChannel++) |
| 187 | + { |
| 188 | + int sensorReading = analogRead(analogChannel); |
| 189 | + client.print(F("<tr><td>")); |
| 190 | + client.print(analogChannel); |
| 191 | + client.print(F("</td><td>")); |
| 192 | + client.print(sensorReading); |
| 193 | + client.println(F("</td></tr>")); |
| 194 | + } |
| 195 | + client.println(F("</table>")); |
| 196 | + client.println(F("</body>")); |
| 197 | + client.println(F("</html>")); |
| 198 | + break; |
| 199 | + } |
| 200 | + |
| 201 | + client.println(F("<center><br><br><br><br><H1>Unknown Command!</H1></center>")); |
| 202 | + client.println(F("</body>")); |
| 203 | + client.println(F("</html>")); |
| 204 | + break; |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + client.stop(); |
| 209 | + timer = millis(); |
| 210 | + } |
| 211 | +} |
0 commit comments