Posted By

icotoi on 12/02/11


Tagged

control arduino Relay AnalogRead


Versions (?)

Relays2Web


 / Published in: C++
 

URL: http://inventeaza.ro

Webserver implemented on arduino Supports persistance trough use of HTTP GET request

  1. #include <EEPROM.h>
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <avr/pgmspace.h>
  5.  
  6. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01};
  7. byte ip[] = {192,168,10,210};
  8.  
  9. byte timeServer[] = {192, 43, 244, 18};
  10. // the router's gateway address:
  11. byte gateway[] = { 192, 168, 1, 1 };
  12. // the subnet:
  13. byte subnet[] = { 255, 255, 255, 0 };
  14.  
  15. unsigned int localPort = 8888;
  16. const int NTP_PKT_SIZE = 48;
  17. byte packetBuffer[NTP_PKT_SIZE];
  18. byte isTimeSynced = 0;
  19. Server server(80);
  20.  
  21. void doWebServerLoop();
  22.  
  23. // Analog Inputs
  24. unsigned int AnalogValues[6];
  25.  
  26. unsigned char RelayPins[] = {4, 5, 6, 7};
  27. unsigned char RelayVals[] = {0, 0, 0, 0};
  28.  
  29. unsigned long uptime;
  30.  
  31. void readEEPROMValues() {
  32. for(int i=0; i<4; i++) {
  33. RelayVals[i] = EEPROM.read(i);
  34. }
  35. }
  36.  
  37. void saveDataToEEPROM() {
  38. for(int i=0; i<4; i++) {
  39. EEPROM.write(i, RelayVals[i]);
  40. }
  41. }
  42.  
  43. void updateRelayState(unsigned char relay, unsigned char state) {
  44. RelayVals[relay] = state;
  45. digitalWrite(RelayPins[relay], state);
  46. };
  47.  
  48. void bounceRelayState(unsigned char relay) {
  49. Serial.print("bounceRelay ");
  50. Serial.println(relay, DEC);
  51. if(RelayVals[relay] == HIGH)
  52. updateRelayState(relay, LOW);
  53. else
  54. updateRelayState(relay, HIGH);
  55. }
  56.  
  57. void setup() {
  58. Serial.begin(9600);
  59.  
  60. Ethernet.begin(mac, ip,gateway, subnet);
  61. server.begin();
  62.  
  63. readEEPROMValues();
  64.  
  65. for (int i=0; i<4; i++) {
  66. pinMode(RelayPins[i], OUTPUT);
  67. digitalWrite(RelayPins[i], RelayVals[i]);
  68. }
  69. Serial.println("Init done");
  70. }
  71.  
  72. void updateData() {
  73. uptime = millis();
  74.  
  75. for(int i=0; i++; i<6) {
  76. AnalogValues[i] = analogRead(i);
  77. }
  78. }
  79.  
  80. void loop()
  81. {
  82. updateData();
  83.  
  84. doWebServerLoop();
  85. }

Report this snippet  

You need to login to post a comment.