Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Test comunication from Arduino via ESP-01 */
- #include <Arduino.h>
- #include <WiFiEsp.h>
- #include <PubSubClient.h>
- // Emulate Serial1 on pins 6/7 if not present
- #ifndef HAVE_HWSERIAL1
- #include <SoftwareSerial.h>
- SoftwareSerial Serial1(4, 5); // RX, TX
- #endif
- #define BUILTIN_LED 13
- //wifi
- #include <WifiAuth.h> // define ssid and pass bellow
- //char ssid[] = "mm"; // your network SSID (name)
- //char pass[] = ""; // your network password
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- const char* mqtt_server = "aaaaaa.messaging.internetofthings.ibmcloud.com";
- const int mqtt_port = 1883;
- const char* mqtt_clntid = "d:aaaaaa:MQTTclient:atest1";
- const char* mqtt_event = "iot-2/evt/event1/fmt/json";
- const char* mqtt_user = "use-token-auth";
- const char* mqtt_auth = "AAAAAAAAAAAAAAAAAA";
- void callback(char* topic, byte* payload, unsigned int length) {
- // handle message arrived
- }
- // Initialize the Ethernet client object
- WiFiEspClient espClient;
- PubSubClient client(mqtt_server,mqtt_port,callback, espClient);
- long lastMsg = 0;
- char msg[50];
- long int value = 0;
- void setup_wifi()
- {
- WiFi.init(&Serial1);
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- // don't continue
- while (true);
- }
- // attempt to connect to WiFi network
- while ( status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network
- status = WiFi.begin(ssid, pass);
- }
- // you're connected now, so print out the data
- Serial.println("You're connected to the network");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void setup(){
- pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
- Serial.begin(115200);
- Serial1.begin(19203); // ESP must be set to the same speed AT+UART_DEF=9600,8,1,0,1
- // initialize ESP module
- setup_wifi();
- if (client.connect(mqtt_clntid, mqtt_user, mqtt_auth)) {
- client.publish(mqtt_event,"{\"prop1\": \"Hello\",\"prop2\": \"world\",\"prop3\": \"ON\"}");
- //client.subscribe("inTopic");
- }
- //client.setServer(mqtt_server, mqtt_port);
- //client.setCallback(callback);
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(mqtt_clntid, mqtt_user, mqtt_auth)) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- //client.publish("outTopic", "hello world");
- // ... and resubscribe
- //client.subscribe("inTopic");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void loop() {
- if (!client.connected()) {
- Serial.print("cln connected?");
- Serial.println(client.connected());
- reconnect();
- }
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment