Thursday 28 April 2016

Blink LED on Arm Cortex M3 STM32F103c8t6 Dev board + ST-LINK V2



SOFTWARE SETUP

ARDUINO

1. Download and install the Arduino  from the Arduino website 
2. Download the zip of the additional files  from Roger Clark's GitHub repository    https://github.com/rogerclarkmelbourne/Arduino_STM32
3. Assuming you are using Windows .. Copy the files from the zip to My Documents\Arduino\hardware so     that inside the hardware folder there is the Arduino_STM32 folder.
4. Go Arduino Menu then Boards Manager and install Arduino SAM Boards



5. Restart the Arduino IDE.
6. Select the board settings from the Arduino IDE Menu as follows:



























ST-LINK V2

1. Download and install the st-link utility v 3.8.0 and driver from ST.com website.
2. Update the firmware to the latest from the utility menu.




HARDWARE SETUP




hook it up to the STM32 board using jumper wires connected from the ST-Link to the JTAG socket using the following pinout.

ST-Link      <>     STM32

 GND           -> GND (4) [JTAG]
 3V3/VDD  ->  3V3  [BOARD]
 CLK           -> SWCLK (9) [JTAG]
 IO/SWD     -> SWDIO (7) [JTAG]


LED is connected between Board Pin PC13 and GND  in series with 330 Ω resistor.



ARDUINO CODE


#define LED PC13

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

Wednesday 27 April 2016

NodeMCU + PIR sensor HC-SR501 + ThingSpeak = Motion Detection alert Via Twitter





To get API keys register at https://thingspeak.com/  and then on its twitter app on https://thingspeak.com/apps/thingtweet

Code

//nodeMCU v1.0 (black) with Arduino IDE
//nodemcu pinout https://github.com/esp8266/Arduino/issues/584

#include <ESP8266WiFi.h>
#include <pins_arduino.h>
#define myPeriodic 15 //in sec | Thingspeak pub is 15sec

long min=0;
int PIRpin = 14;
int PIRvalue=0;
const char* server = "api.thingspeak.com";
String apiKey ="Your-api-key";
String api_key_ts="your-api-key-ts";
const char* MY_SSID = "wifi=ssid"; 
const char* MY_PWD = "password";

int failedCounter = 0;
long lastConnectionTime = 0; 
boolean lastConnected = false;
int sent = 0;

void setup() {
  pinMode(PIRpin, INPUT);
  Serial.begin(115200);
  connectWifi();
}

void loop() {
  PIRvalue=digitalRead(PIRpin);
if (PIRvalue==HIGH){
  Serial.println(String(sent)+"Motion detected ");
  sendMotion(PIRvalue);
  updateTwitterStatus("My thing is social @thingspeak, Motion detected, possible intruder!");
  int count = myPeriodic;
  while(count--)
  delay(1000);}
  else{
    Serial.println(String(sent)+"Motion NOT detected ");
  sendMotion(PIRvalue);
  int count = myPeriodic;
  while(count--)
  delay(1000);}
    
}

void connectWifi()
{
  Serial.print("Connecting to "+*MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("Connected");
  Serial.println("");  
}//end connect

void sendMotion(int PIRv)
{  
   WiFiClient client;
  
   if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
   Serial.println("WiFi Client connected ");
   String postStr = apiKey;
   postStr += "&field1=";
   postStr += String(PIRv);
   postStr += "\r\n\r\n";
   
   client.print("POST /update HTTP/1.1\n");
   client.print("Host: api.thingspeak.com\n");
   client.print("Connection: close\n");
   client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
   client.print("Content-Type: application/x-www-form-urlencoded\n");
   client.print("Content-Length: ");
   client.print(postStr.length());
   client.print("\n\n");
   client.print(postStr);
   delay(1000);
   
   }//end if
   sent++;
 client.stop();
}//end send
void updateTwitterStatus(String tsData)
{
  
  WiFiClient client;
  if (client.connect(server, 80))
  { Serial.println("WiFi Client connected to thingSpeak ");
    // Create HTTP POST Data
    
    min =millis()/(1000);
    tsData = min+tsData;
    tsData = "api_key="+api_key_ts+"&status="+tsData;
            
    client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");

    client.print(tsData);
    
    if (client.connected())
    {
      Serial.println("Connecting to ThingSpeak/thingtweet...");
      Serial.println();
      
    }
}
}