Hi, I want to build arduino with 2 servers.
one of them is for web service to change IP address from the web page(port 80)
second server is for snmp agent protocol to send temperature to the MIB manager (port 161)
both of them works great when they are not mixed with each other
but when I want to use both of them none of them works! is it possible to do this?
this is part of my code:
#include "SnmpAgent.h"
#include <TextFinder.h>
#include <EEPROM.h>
#include <avr/pgmspace.h>
#include <UIPEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SimpleTimer.h>
SNMPAgent SnmpAgent;
OneWire oneWire(2);
DallasTemperature Sensors(&oneWire);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192,168,1,15};
byte subnet[] = {255,255,255,0};
byte gateway[] = {192,168,1,1};
byte dnsserver[] = {192,168,1,1};
EthernetServer server(80);
EthernetServer server2(161);
void setup() {
ShieldSetup();
Sensors.begin();
server.begin();
server2.begin();
// Setup default SNMP Agent information
SnmpAgent.SetCommunity(PSTR("public")); // Password/Community (Read-Only)
SnmpAgent.SetDescription(PSTR("Force Sensor")); // 1.3.6.1.2.1.1.1.0
SnmpAgent.SetContact(PSTR("vader@deathstar.com")); // 1.3.6.1.2.1.1.4.0
SnmpAgent.SetLocation(PSTR("Death Star")); // 1.3.6.1.2.1.1.6.0
SnmpAgent.SetSystemName(PSTR("arduino")); // 1.3.6.1.2.1.1.5.0
// Setup custom SNMP values (1.3.6.1.4.1.49701.1.X.0),
// where X is a value between 1 and 5 (defined by MAX_SNMP_VALUES in SnmpAgent.h)
SnmpAgent.SetValue(1, temperature); // 1.3.6.1.4.1.49701.1.1.0
SnmpAgent.SetValue(2, &lastMillis); // 1.3.6.1.4.1.49701.1.2.0
}
void ShieldSetup() {
int idcheck = EEPROM.read(0);
if (idcheck != ID){
}
if (idcheck == ID){
for (int i = 0; i < 4; i++){
ip[i] = EEPROM.read(i+7);
}
for (int i = 0; i < 4; i++){
subnet[i] = EEPROM.read(i+11);
}
for (int i = 0; i < 4; i++){
gateway[i] = EEPROM.read(i+15);
}
}
Ethernet.begin(mac, ip);
}
void loop() {
EthernetClient client = server.available();
if (client) {
TextFinder finder(client );
while (client.connected()) {
if (client.available()) {
if( finder.find("GET /") ) {
if (finder.findUntil("setup", "\n\r")){
if (finder.findUntil("SBM", "\n\r")){
byte SET = finder.getValue();
while(finder.findUntil("DT", "\n\r")){
int val = finder.getValue();
// if val from "DT" is between 1 and 6 the according value must be a MAC value.
if(val >= 1 && val <= 6) {
mac[val - 1] = finder.getValue();
}
// if val from "DT" is between 7 and 10 the according value must be a IP value.
if(val >= 7 && val <= 10) {
ip[val - 7] = finder.getValue();
}
// if val from "DT" is between 11 and 14 the according value must be a MASK value.
if(val >= 11 && val <= 14) {
subnet[val - 11] = finder.getValue();
}
// if val from "DT" is between 15 and 18 the according value must be a GW value.
if(val >= 15 && val <= 18) {
gateway[val - 15] = finder.getValue();
}
}
// Now that we got all the data, we can save it to EEPROM
//for (int i = 0 ; i < 6; i++){
//EEPROM.write(i + 1,mac[i]);
//}
for (int i = 0 ; i < 4; i++){
EEPROM.write(i + 7, ip[i]);
}
for (int i = 0 ; i < 4; i++){
EEPROM.write(i + 11, subnet[i]);
}
for (int i = 0 ; i < 4; i++){
EEPROM.write(i + 15, gateway[i]);
}
EEPROM.write(0, 0x92);
}
// and from this point on, we can start building our setup page
// and show it in the client's browser.
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
for (int i = 0; i < 4; i++)
{
strcpy_P(buffer, (char*)pgm_read_word(&(string_table0[i])));
client.print( buffer );
}
for (int i = 0; i < 3; i++)
{
strcpy_P(buffer, (char*)pgm_read_word(&(string_table1[i])));
client.print( buffer );
}
client.print(mac[0],HEX);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table1[3])));
client.print( buffer );
client.print(mac[1],HEX);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table1[4])));
client.print( buffer );
client.print(mac[2],HEX);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table1[5])));
client.print( buffer );
client.print(mac[3],HEX);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table1[6])));
client.print( buffer );
client.print(mac[4],HEX);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table1[7])));
client.print( buffer );
client.print(mac[5],HEX);
for (int i = 0; i < 4; i++)
{
strcpy_P(buffer, (char*)pgm_read_word(&(string_table2[i])));
client.print( buffer );
}
client.print(ip[0],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table2[4])));
client.print( buffer );
client.print(ip[1],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table2[5])));
client.print( buffer );
client.print(ip[2],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table2[6])));
client.print( buffer );
client.print(ip[3],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table3[0])));
client.print( buffer );
client.print(subnet[0],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table3[1])));
client.print( buffer );
client.print(subnet[1],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table3[2])));
client.print( buffer );
client.print(subnet[2],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table3[3])));
client.print( buffer );
client.print(subnet[3],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table4[0])));
client.print( buffer );
client.print(gateway[0],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table4[1])));
client.print( buffer );
client.print(gateway[1],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table4[2])));
client.print( buffer );
client.print(gateway[2],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table4[3])));
client.print( buffer );
client.print(gateway[3],DEC);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table4[4])));
client.print( buffer );
for (int i = 0; i < 7; i++)
{
strcpy_P(buffer, (char*)pgm_read_word(&(string_table5[i])));
client.print( buffer );
}
strcpy_P(buffer, (char*)pgm_read_word(&(string_table4[5])));
client.print( buffer );
client.println();
client.println();
client.println("<tr><td><font color=\"red\" size=\"2\" face=\"Verdana\"> You can not change the MAC address</font></td></tr></table>
");
}}}}
delay(1);
client.stop();
}
EthernetClient client1 = server2.available();
if (client1) {
while (client1.connected()) {
if (client1.available()) {
SnmpAgent.listen();
if (millis() - lastMillis > 10000) {
Sensors.requestTemperatures(); // Send the command to get temperatures
dtostrf(Sensors.getTempCByIndex(0), 1, 1, temperature);
lastMillis = millis();
}}}
delay(1);
client1.stop();
}
}