Hi all,
Now I am doing a project of MQTT protocol by using Arduino. I have an Arduino board and an Arduino Ethernet Shield. I use the ethernet shield to connect to Internet and the shield is connected to a net wire in my school. A proxy server is set in my school so everyone need to make proxy settings to connect outside networks. It's same to Arduino board. When I use the sample file of MQTT library, I need to modify the code. My code is below:
#include <PubSubClient.h>
#include <SPI.h>
#include <Ethernet.h>
// Update these with values suitable for your network.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xF2, 0x45 };
byte server[] = { 212, 72, 74, 21 }; //MQTT dashboard
//byte ip[] = { 172, 16, 0, 100 };
byte proxy[] = { 10, 204, 18, 202 }; // School Proxy
void callback(char* topic, byte* payload, unsigned int length)
{
// handle message arrived
}
EthernetClient ethClient;
PubSubClient arduino(server, 1883, callback, ethClient);
void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
Serial.println("connecting...");
Serial.println();
delay(500);
int eth=0;
while (eth==0)
{
Serial.println("Proxy Connecting...");
eth = ethClient.connect(proxy, 3128);
}
Serial.println("Proxy Connected!");
Serial.println();
while ((arduino.connect("niunai1221"))==0)
{
Serial.println("MQTT Connecting...");
delay(1000);
}
Serial.println("MQTT Connected!");
arduino.publish("outTopic1221","hello world");
}
void loop()
{
arduino.loop();
}
The code ethClient.connect(proxy, 3128)
could connect Arduino to school proxy successfully, but after that, code arduino.connect("niunai1221")
didn't work.
I am not a English speaker, hope everyone could understand my words. Waiting for your reply~