Tuesday, February 23, 2016

Smart Traffic Signals

MQTT Class effort

a. What I have done in the class:
I have integrated my Raspberry Pi and Arduino through MQTT, to listen and respond on photocell(sensors that allow you to detect light) readings. I used pubsubclient to test request-response on wifi. Arduino - Pubsubclient provides examples of basic tests, so that we can test pre requirements. I was the first person to complete the class task and helped others in doing the same.

Arduino - Hardware
Arduino WiFi Module
Photocell
Resistors (10k, 330 ohm)
LED
Jumping wires
Arduino - Software / MQTT
There are four main tasks the Arduino software needs to take care of for this example:
Gather light sensor readings periodically
Publish sensor readings via MQTT
Listen for commands via MQTT
Control the LED based on a setpoint
An MQTT client is created in the setup function.

Experiment overview:

The MQTT client connects (if it is not already connected).

Based on the “sensing mode” the application decides how to drive the LED.  It could be OFF, ON or SENSE.  In the SENSE case a light reading is taken and the LED is driven according to a hardcoded setpoint.

I was able to operate 3 different LED lights using mobile app with help of MQTT protocol.Below is the video which shows demonstration of the work.

Video:




Connections:





Code:


#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <String.h>

// Update these with values suitable for your network.

const char* ssid = "Pi_AP";
const char* password = "Raspberry";
//const char* mqtt_server = "broker.mqtt-dashboard.com";
const char* mqtt_server = "192.168.0.34";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int timer = 200;
int ar[] = {15, 12, 4, 16}; 


void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void off() {

  for(int i = 0; i < 4 ; i++){
  digitalWrite(ar[i], LOW);
  }
  
}


void go() {
  off();
  digitalWrite(15, HIGH);
}

void stops() {
  off();
  digitalWrite(12, HIGH);
}

void yeild() {
  off();
  digitalWrite(4, HIGH);
}


void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  
  Serial.println();

  String s = "";

  for(int i = 0; i< length; i++) {
    char a = ((char)payload[i]);
    s = s + a;
  }

  if( s == "off") {
    off();
  }
  else if(s == "stop") {
    stops();
  }
  else if(s == "go") { 
    go();
  }
  else if(s == "yeild") { 
    yeild();
  }
  
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      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 setup() {
  //pinMode(12, OUTPUT);     // Initialize the BUILTIN_LED pin as an output

  for(int i = 0; i < 4; i++){
    pinMode(ar[i],OUTPUT);     
  }
  Serial.begin(115200);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
    //callback();
  }

}


b. To whom I helped and What I did for them:

I was the first person to complete task of talking to MQTT server in class. I tried with example from arduino library and was able to communicate using MQTT. I explained this to entire class with help of professor. I told which program to use from code so that everyone can start MQTT communication from arduino.

I helped krishna, pavan and sandeep for executing the code for basic connection through MQTT.

c. Who helped me and What they did for me:
Pavan, Sandeep and Jorge helped to me find the coding and programing help from blogs which explains MQTT programing.


References:

  • http://www.rs-online.com/designspark/electronics/blog/building-distributed-node-red-applications-with-mqtt
  • http://jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi/
  • http://pubsubclient.knolleary.net/
  • https://learn.adafruit.com/photocells/overview
  • http://m2mio.tumblr.com/post/30048662088/a-simple-example-arduino-mqtt-m2mio



No comments:

Post a Comment